[R] aes and parameter evaluation problems

Geoffrey lordgeoffrey at optusnet.com.au
Tue Dec 31 14:07:30 CET 2013


Thanks A.K. and Jeff, both answers helped me.
(and of course gave me more homework!)

On 31/12/13 16:04, Jeff Newmiller wrote:
> 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:<jdnewmil at 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 <smartpink111 at 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
>> <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.
>



More information about the R-help mailing list