Hey, I have a dataset like this: ID Var1 Var2 Group A1 1 1 BB A2 1 2 AA B1 2 1 CC B2 1 3 DD C1 1 2 EE I would like to plot the points of Var1 and Var2, use "ID" as X-axis, but color the points by "Group". I can only manage to color the points by "ID" after transform the dataset to "tall" using "reshape" package. Thanks for your help! [[alternative HTML version deleted]]
On May 3, 2013, at 1:37 PM, Ye Lin wrote:> Hey, > > I have a dataset like this: > > ID Var1 Var2 Group > A1 1 1 BB > A2 1 2 AA > B1 2 1 CC > B2 1 3 DD > C1 1 2 EE > > I would like to plot the points of Var1 and Var2, use "ID" as X-axis, but > color the points by "Group". I can only manage to color the points by "ID" > after transform the dataset to "tall" using "reshape" package.If I were given the task of designing a plotting system that would "decide" what to do with a categorical "x-axis" request, it would probably deliver a barplot. My guess is that you do not want that. But what do you mean by a "point" whose x-value is "A1"? -- David Winsemius Alameda, CA, USA
HI, May be this helps: dat1<- read.table(text=" ID??? Var1????? Var2??? Group A1??????? 1??????????? 1??????????? BB A2??????? 1????????? 2??????????? AA B1????????? 2????????? 1??????????? CC B2??????? 1??????????? 3??????????? DD C1????????? 1??????????? 2??????????? EE ",sep="",header=TRUE) library(reshape2) dat2<-melt(dat1,id.var=c("ID","Group")) library(ggplot2) ggplot(dat2,aes(x=ID,y=value,group=Group,colour=Group))+geom_point() A.K. ----- Original Message ----- From: Ye Lin <yelin at lbl.gov> To: R help <r-help at r-project.org> Cc: Sent: Friday, May 3, 2013 4:37 PM Subject: [R] color by group in ggplot Hey, I have a dataset like this: ID? ? Var1? ? ? Var2? ? Group A1? ? ? ? 1? ? ? ? ? ? 1? ? ? ? ? ? BB A2? ? ? ? 1? ? ? ? ? 2? ? ? ? ? ? AA B1? ? ? ? ? 2? ? ? ? ? 1? ? ? ? ? ? CC B2? ? ? ? 1? ? ? ? ? ? 3? ? ? ? ? ? DD C1? ? ? ? ? 1? ? ? ? ? ? 2? ? ? ? ? ? EE I would like to plot the points of Var1 and Var2, use "ID" as X-axis, but color the points by "Group". I can only manage to color the points by "ID" after transform the dataset to "tall" using "reshape" package. Thanks for your help! ??? [[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 May 3, 2013, at 1:57 PM, Ye Lin wrote:> I want to plot the values of "Var1" and "Var2" on the same plot, with x-axis labeling as the list of IDs. Sth like this: > <image.png> > > But I want to color the points based on the category in "Group", I dont know how to do it with ggplot.You didn't say what class the ID variable was, but if it were a factor ( as is most likely), then: plot( as.numeric(dfrm$ID), Var1) points( as.numeric(dfrm$ID), Var2) With whatever means of disiguishing overlapping points (pch, col, jittering) might suit you. -- David.> Thanks! > > > On Fri, May 3, 2013 at 1:49 PM, David Winsemius <dwinsemius at comcast.net> wrote: > > On May 3, 2013, at 1:37 PM, Ye Lin wrote: > > > Hey, > > > > I have a dataset like this: > > > > ID Var1 Var2 Group > > A1 1 1 BB > > A2 1 2 AA > > B1 2 1 CC > > B2 1 3 DD > > C1 1 2 EE > > > > I would like to plot the points of Var1 and Var2, use "ID" as X-axis, but > > color the points by "Group". I can only manage to color the points by "ID" > > after transform the dataset to "tall" using "reshape" package. > > If I were given the task of designing a plotting system that would "decide" what to do with a categorical "x-axis" request, it would probably deliver a barplot. My guess is that you do not want that. But what do you mean by a "point" whose x-value is "A1"? > > -- > > David Winsemius > Alameda, CA, USA > >David Winsemius Alameda, CA, USA