[R] aes and parameter evaluation problems

arun smartpink111 at yahoo.com
Tue Dec 31 04:29:00 CET 2013



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 <lordgeoffrey at 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 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.




More information about the R-help mailing list