Hi, I am a complete R rookie so this question is probably really simple but I haven't found an answer on the web that I can understand. My data frame has 3 columns, A, B and C. A and B have numbers (about 8000 rows), C is a factor which is either true or false. So I can plot A vs B with plot(dataframe$A, dataframe$B). However, I would like the points to be different colors depending on whether C is True or False. Even better, if C had more than 2 groups, like group 1,2,3 can I make all 3 different colors, etc? It would be really helpful to give an example using my scenario since I have a lot of trouble understanding other examples on the web because the data is not set up like my data. Thanks so much!!! Amanda [[alternative HTML version deleted]]
See argument 'col' to plot()/points(). Setup a 'col' vector of length equal to the number of data points and have the 'C' variable specify the colors of the individual elements. Then call plot()/points() with argument 'col'. My $.02 /HB On Fri, Sep 12, 2008 at 3:29 PM, Amanda Young <amandag at mit.edu> wrote:> Hi, > I am a complete R rookie so this question is probably really simple > but I haven't found an answer on the web that I can understand. > > My data frame has 3 columns, A, B and C. A and B have numbers (about > 8000 rows), C is a factor which is either true or false. So I can > plot A vs B with plot(dataframe$A, dataframe$B). However, I would > like the points to be different colors depending on whether C is True > or False. Even better, if C had more than 2 groups, like group 1,2,3 > can I make all 3 different colors, etc? It would be really helpful to > give an example using my scenario since I have a lot of trouble > understanding other examples on the web because the data is not set > up like my data. > > Thanks so much!!! > Amanda > > > > > [[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. >
Dear Amanda,
Try this:
# Data set
set.seed(123)
DF=data.frame(A=rnorm(10),B=rpois(10,10),C=sample(1:3,10,replace=T))
attach(DF)
# Plot
plot(A,B,pch=16,col=C)
legend('topleft',paste('Group',1:3,sep="
"),col=1:3,pch=16)
HTH,
Jorge
On Fri, Sep 12, 2008 at 6:29 PM, Amanda Young <amandag@mit.edu> wrote:
> Hi,
> I am a complete R rookie so this question is probably really simple
> but I haven't found an answer on the web that I can understand.
>
> My data frame has 3 columns, A, B and C. A and B have numbers (about
> 8000 rows), C is a factor which is either true or false. So I can
> plot A vs B with plot(dataframe$A, dataframe$B). However, I would
> like the points to be different colors depending on whether C is True
> or False. Even better, if C had more than 2 groups, like group 1,2,3
> can I make all 3 different colors, etc? It would be really helpful to
> give an example using my scenario since I have a lot of trouble
> understanding other examples on the web because the data is not set
> up like my data.
>
> Thanks so much!!!
> Amanda
>
>
>
>
> [[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]]
On Fri, Sep 12, 2008 at 5:29 PM, Amanda Young <amandag at mit.edu> wrote:> Hi, > I am a complete R rookie so this question is probably really simple > but I haven't found an answer on the web that I can understand. > > My data frame has 3 columns, A, B and C. A and B have numbers (about > 8000 rows), C is a factor which is either true or false. So I can > plot A vs B with plot(dataframe$A, dataframe$B). However, I would > like the points to be different colors depending on whether C is True > or False. Even better, if C had more than 2 groups, like group 1,2,3 > can I make all 3 different colors, etc? It would be really helpful to > give an example using my scenario since I have a lot of trouble > understanding other examples on the web because the data is not set > up like my data.install.packages("ggplot2") library(ggplot2) qplot(A, B, data = dataframe, color = C) That will also automatically draw a legend for you. See http://had.co.nz/ggplot2 for more info. Hadley -- http://had.co.nz/
Amanda Young wrote:> Hi, > I am a complete R rookie so this question is probably really simple > but I haven't found an answer on the web that I can understand. > > My data frame has 3 columns, A, B and C. A and B have numbers (about > 8000 rows), C is a factor which is either true or false. So I can > plot A vs B with plot(dataframe$A, dataframe$B). However, I would > like the points to be different colors depending on whether C is True > or False. Even better, if C had more than 2 groups, like group 1,2,3 > can I make all 3 different colors, etc? It would be really helpful to > give an example using my scenario since I have a lot of trouble > understanding other examples on the web because the data is not set > up like my data. > >Hi Amanda, Have a look at the example for the color.scale function in the plotrix package. This will display the points in as many different colors as you like. Jim