[R] use ggplot in a function to which a column name is given

jiho jo.irisson at gmail.com
Thu Dec 13 16:24:55 CET 2007


On 2007-December-13  , at 15:56 , hadley wickham wrote:
> Hi Jiho,
>
> The key to solving this problem is to use aes_string instead of aes.
> Instead of the complicated munging that aes does to get the names of
> the variables, aes_string works directly with strings, so that:
>
> aes_string(x = "mpg", y = "wt") == aes(x = mpg, y = wt)
>
> So your function would look like:
>
> foo4 <- function(uv="u") {
>  ggplot(A, aes_string(x = "x", y= "y", fill = uv)) + geom_tile()
> }
>
> Or
>
> ggplot(A, aes(x=x, y=y)) + aes_string(fill=uv) + geom_tile()
>
> Hope that helps!  (And I've made a note to better document aes_string
> so you can discover after looking at aes)

great! I knew you would have thought this through. That's perfect. As  
always there's the trade-off between writing code and documenting the  
code already written. In this case the trade-off turned toward the  
code part I guess.

Autodetection of strings by aes would be even greater but that would  
prevent me to assign the actual strings "u", "x", "y" to an aes  
element, which I don't see as a problem for non text related functions  
though...

Thanks again.

JiHO
---
http://jo.irisson.free.fr/



More information about the R-help mailing list