[R] Scatterplot with data grouped by color
Martin Maechler
maechler at stat.math.ethz.ch
Sat Sep 13 15:36:50 CEST 2008
>>>>> "JIV" == Jorge Ivan Velez <jorgeivanvelez at gmail.com>
>>>>> on Fri, 12 Sep 2008 18:39:04 -0400 writes:
JIV> Dear Amanda,
JIV> Try this:
JIV> # Data set
JIV> set.seed(123)
JIV> DF=data.frame(A=rnorm(10),B=rpois(10,10),C=sample(1:3,10,replace=T))
JIV> attach(DF)
JIV> # Plot
JIV> plot(A,B,pch=16,col=C)
JIV> legend('topleft',paste('Group',1:3,sep=" "),col=1:3,pch=16)
JIV> HTH,
Yes, that's a fine small example.
To become flawless (in the eyes of the R-stylepolice :-)),
you'd have to *NOT* attach() the data frame
{= a bad habit from earlier implementations of the S language and R},
but rather either use with(.) , or often (as in this case),
just specify a 'data= . ' :
# Data set
set.seed(123)
DF <- data.frame(A=rnorm(10),B=rpois(10,10),C=sample(1:3,10,replace=T))
# Plot
plot(B ~ A, pch=16, col=C, data = DF)
legend('topleft',paste('Group',1:3,sep=" "),col=1:3,pch=16)
Regards,
Martin
JIV> On Fri, Sep 12, 2008 at 6: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.
>>
JIV> [[alternative HTML version deleted]]
JIV> ______________________________________________
JIV> R-help at r-project.org mailing list
JIV> https://stat.ethz.ch/mailman/listinfo/r-help
JIV> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
JIV> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list