[R] aes and parameter evaluation problems

Geoffrey lordgeoffrey at optusnet.com.au
Tue Dec 31 01:46:53 CET 2013


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.



More information about the R-help mailing list