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

From: jiho <>
Date: Thu, 13 Dec 2007 16:24:55 +0100

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.



______________________________________________ mailing list
PLEASE do read the posting guide
and provide commented, minimal, self-contained, reproducible code.
Received on Thu 13 Dec 2007 - 15:33:18 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 13 Dec 2007 - 16:30:18 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.