An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110609/f10b11ae/attachment.pl>
scatterplot3d - help assign colors based on multiple conditions
6 messages · Karthik Kota, Uwe Ligges, Jim Lemon
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 at 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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110609/cbb46cde/attachment.pl>
On 09.06.2011 22:40, Karthik Kota wrote:
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")
In that case go for it with your original way by, e.g., replacing
col[grepl("_Anterior_nares", xlabels) & grepl("_Anterior_nares",
ylabels)] <- mycols[4]
Uwe
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 at 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.
On 06/10/2011 06:40 AM, Karthik Kota wrote:
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")
Hi Karthik,
Your problem is that you are assigning all of the values to "col" twice.
If you reverse the order of the statements you will get the red/black
color set. Try this:
col<-rep("black",dim(cdh1)[1])
col[grepl("_Anterior_nares", xlabels) &
grepl("_Anterior_nares", ylabels)]<-"red"
col[grepl("_Tongue_dorsum", xlabels) &
grepl("_Tongue_dorsum", ylabels)]<-"blue"
If your two conditions specify disjunct sets, that is, no case satisfies
both conditions, you should have the correct vector of colors.
Jim
Thanks! That did the trick.
On Jun 10, 2011, at 3:16 AM, Jim Lemon wrote:
On 06/10/2011 06:40 AM, Karthik Kota wrote:
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")
Hi Karthik,
Your problem is that you are assigning all of the values to "col" twice. If you reverse the order of the statements you will get the red/black color set. Try this:
col<-rep("black",dim(cdh1)[1])
col[grepl("_Anterior_nares", xlabels) &
grepl("_Anterior_nares", ylabels)]<-"red"
col[grepl("_Tongue_dorsum", xlabels) &
grepl("_Tongue_dorsum", ylabels)]<-"blue"
If your two conditions specify disjunct sets, that is, no case satisfies both conditions, you should have the correct vector of colors.
Jim