Altaweel, Mark R.
2009-Mar-20 19:55 UTC
[R] plotting two variables with a third used for color
I have a problem where I have two columns of data that I can simply plot using: plot(wV[0:15,3],wY[0:15,3]). This produces my desired plot. Now, say I have a third variable that I would like to introduce and use that variable to set different colors in the plot In this case, say I wanted values greater than 0 to be "blue" and values less than 0 to be "red" Basically, my question is how can one plot something like the function shown above, but with the added functionality of indicating color for a third variable Something to the effect: if(weightComply[0:15,3]>0) //if the values from 0 to 15 in column 3 are greater than 0 plot(wV[0:15,3],wY[0:15,3],col="blue") //then plot the dots as blue else plot(wV[0:15,3],wY[0:15,3],col="red") //otherwise plot the dots red I know this syntax is wrong, but I think it shows what I generally want to do. Mark
David Winsemius
2009-Mar-20 20:23 UTC
[R] plotting two variables with a third used for color
On Mar 20, 2009, at 3:55 PM, Altaweel, Mark R. wrote:> I have a problem where I have two columns of data that I can simply > plot using: > > plot(wV[0:15,3],wY[0:15,3]). >Perhaps: plot(wV[0:15,3],wY[0:15,3], col = ifelse(wY[0:15,3]>0, "blue","red") )> This produces my desired plot. > > Now, say I have a third variable that I would like to introduce and > use that variable to set different colors in the plot > > In this case, say I wanted values greater than 0 to be "blue" and > values less than 0 to be "red"And if they are zero? In my example those become red. Basically you create a vector that is as long as the y-values with values of "red" or "blue" that depend on your criterion.> > > Basically, my question is how can one plot something like the > function shown above, but with the added functionality of indicating > color for a third variable > > Something to the effect: > > if(weightComply[0:15,3]>0) //if the values from 0 to 15 in column 3 > are greater than 0 > plot(wV[0:15,3],wY[0:15,3],col="blue") //then plot the dots as blue > > else > plot(wV[0:15,3],wY[0:15,3],col="red") //otherwise plot the dots red > > I know this syntax is wrong, but I think it shows what I generally > want to do. > > MarkDavid Winsemius, MD Heritage Laboratories West Hartford, CT
On 2009-March-20 , at 16:23 , David Winsemius wrote:> On Mar 20, 2009, at 3:55 PM, Altaweel, Mark R. wrote: > >> I have a problem where I have two columns of data that I can simply >> plot using: >> >> plot(wV[0:15,3],wY[0:15,3]). >> > Perhaps: > plot(wV[0:15,3],wY[0:15,3], col = ifelse(wY[0:15,3]>0, "blue","red") )And you could look into the package ggplot2 which gives you a legend and is well suited for these things. # quick version: qplot(wV[0:15,3], wY[0:15,3], colour=ifelse(wY[0:15,3]>0,">0","<0")) # more explicit version, using a data.frame dat = data.frame(v=wV[0:15,3], y=wY[0:15,3], sign=ifelse(wY[0:15,3]>0,">0","<0")) ggplot(data=dat) + geom_point(aes(x=v,y=y,colour=sign)) JiHO --- http://jo.irisson.free.fr/