Ralf B
2010-May-10 19:29 UTC
[R] graphics::plot Organizing line types, line colors and generating matching legends...
Lets say I have a generated data frame with variables that follow a naming convention: title,a1,a2,b1,b2,b3,c1,c2,c3,c4... I am plotting every column (starting from a1) as a line in a plot. That works. However my diagram becomes very unorganized. Creating legends is nice, but trying out different combinations requires me to adjust my legend since it is generally disconnected from the data. Is there an elegant way where R generates legends for its variables so that the legend will fit the line and uses the column name as in the legend? I guess I am asking for the basic Excel thing. I understand that in the standard graphics package, this is not really intended. Perhaps somebody can point me into a direction where this more easily possible? Is it for example easier in gplot or lattice? Ralf
baptiste auguie
2010-May-10 19:39 UTC
[R] graphics::plot Organizing line types, line colors and generating matching legends...
Hi, Lattice and ggplot2 are both ideally suited for this task. Consider this example, library(ggplot2) d = data.frame(x=1:10, a1=rnorm(10), b1=rnorm(10)) m = melt(d, id ="x") # reshape into long format qplot(x, value, data=m, geom="path", colour=variable) library(lattice) xyplot(value~x, data=m, type="l", group=variable, auto.key=TRUE) HTH, baptiste On 10 May 2010 21:29, Ralf B <ralf.bierig at gmail.com> wrote:> Lets say I have a generated data frame with variables that follow a > naming convention: > > title,a1,a2,b1,b2,b3,c1,c2,c3,c4... > > I am plotting every column (starting from a1) as a line in a plot. > That works. However my diagram becomes very unorganized. Creating > legends is nice, but trying out different combinations requires me to > adjust my legend since it is generally disconnected from the data. > > Is there an elegant way where R generates legends for its variables so > that the legend will fit the line and uses the column name as in the > legend? I guess I am asking for the basic Excel thing. I understand > that in the standard graphics package, this is not really intended. > Perhaps somebody can point me into a direction where this more easily > possible? Is it for example easier in gplot or lattice? > > Ralf > > ______________________________________________ > 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. >