A.K. answered your question 1, but since you did say as question 2 that you wanted it done right...
library(reshape2) ex3 <- function() { d <- data.frame(x=1:5,a=1:5,b=2:6,c=3:7) dl <- melt( d, id.vars="x" ) ggplot(dl,aes(x=x,y=value,color=variable))+ geom_line() } --------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnew...@dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- Sent from my phone. Please excuse my brevity. arun <smartpink...@yahoo.com> wrote: > > >Hi, >Try: > >ex2 <- function() { > d <- data.frame(x=1:5,a=1:5,b=2:6,c=3:7) > g <- ggplot(d, aes(x)) > for (n in c("a","b","c")) { > g <- g + geom_line(aes_string(y=n,colour=n)) > } > return(g) >} > >A.K. > > >On Monday, December 30, 2013 7:49 PM, Geoffrey ><lordgeoff...@optusnet.com.au> wrote: >I am trying add geom_line's using a loop but the nature of unevaluated >parameters is causing me problems. > >This code works: >ex <- function() { > d <- data.frame(x=1:5,a=1:5,b=2:6,c=3:7) > g <- ggplot(d, aes(x)) > g <- g + > geom_line(aes(y=a,colour=a)) + > geom_line(aes(y=b,colour=b)) + > geom_line(aes(y=c,colour=c)) > return(g) >} > >This code (not surprisingly) fails: >ex2 <- function() { > d <- data.frame(x=1:5,a=1:5,b=2:6,c=3:7) > g <- ggplot(d, aes(x)) > for (n in c("a","b","c")) { > g <- g + geom_line(aes(y=n,colour=n)) > } > return(g) >} > >I believe i want something like the failing code, but done right. > >I have two problems (at least in this code): >#1 how do handle the parameter evaluation >#2 is this the right thing to be even doing with geom_line() & aes() ? > >Geoff. > >______________________________________________ >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. > > >______________________________________________ >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. ______________________________________________ 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.