[R] scatterplot3d - help assign colors based on multiple conditions

Jim Lemon jim at bitwrit.com.au
Fri Jun 10 10:16:13 CEST 2011


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



More information about the R-help mailing list