Thanks a lot! This is very helpful. If I have to extend this to one more condition say assign "blue" if both the corresponding labels have "_Tongue_dorsum", is there a straight forward function. Doing something like below is overriding the colors assigned by the first statement.
col <- ifelse(grepl("_Anterior_nares", xlabels) & grepl("_Anterior_nares", ylabels), "red", "black") col <- ifelse(grepl("_Tongue_dorsum", xlabels) & grepl("_Tongue_dorsum", ylabels), "blue", "black") Again, your time is really appreciated. Karthik On Jun 9, 2011, at 1:56 PM, Uwe Ligges wrote: > > > On 09.06.2011 16:51, Karthik Kota wrote: >> Hi >> >> I am relatively new to R and am trying to figure out to plot 3d scatter plot >> using defined colors based on x-axis and y-axis values. Right now in the >> code below, I assign colors based on certain values in the names of the >> x-axis. Now if I want to extend the condition to assign a color based on >> the names of both x-axis and y-axis values, what should I be doing? Any help >> or ideas would be greatly appreciated. >> >> For e.g. in my 3 column matrix below, if I want to assign "red" to all the >> values whose first column and second column contain "Anterior_nares" and >> assign black to any other combination. > > > Both question and answer are not really scatterplot3d related: You probably > want > > col <- ifelse(grepl("_Anterior_nares", xlabels) & grepl("_Anterior_nares", > ylabels), "red", "black") > > Best, > Uwe Ligges > > > >> Thanks! >> Karthik >> >> library(scatterplot3d) >> >> chd1=read.table(file="test.out", header=F, sep="\t") >> col=as.vector(chd1[,1]) >> xlabels=as.vector(chd1[,1]) >> ylabels=as.vector(chd1[,2]) >> >> mycols<-c("red","blue","green","chocolate","orange", "brown") >> col[grep("_Stool", xlabels) ]<-mycols[1] >> #col[grep("_Stool", xlabels)&& grep("_Stool", ylabels) ]<-mycols[1] >> col[grep("_Tongue_dorsum", xlabels) ]<-mycols[2] >> col[grep("_Posterior_fornix", xlabels) ]<-mycols[3] >> col[grep("_Anterior_nares", xlabels) ]<-mycols[4] >> col[grep("_Buccal_mucosa", xlabels) ]<-mycols[5] >> col[grep("_Supragingival_plaque", xlabels) ]<-mycols[6] >> >> >> png(file="3dplot_test.png", w=700,h=700) >> >> scatterplot3d(chd1[, 1], chd1[, 2], chd1[, 3], main="test", xlab="sample", >> ylab="sample", zlab="kmers", color=col,type="p") >> dev.off () >> >> >> my test.out matrix looks something like this: >> >> A011132_Anterior_nares A011263_Anterior_nares 50130 >> A011132_Anterior_nares A011397_Stool 34748 >> A011132_Anterior_nares A012291_Tongue 40859 >> A011132_Anterior_nares A012663_Buccal_mucosa 76213 >> A011132_Anterior_nares A013155_Anterior_nares 36841 >> A011132_Anterior_nares A013269_Anterior_nares 45619 >> A011132_Anterior_nares A013637_Anterior_nares 56995 >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >> and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.