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

From: hadley wickham <h.wickham_at_gmail.com>
Date: Thu, 13 Dec 2007 08:56:50 -0600

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)

Hadley

On 12/13/07, jiho <jo.irisson_at_gmail.com> wrote:
> Hi everyone, Hi ggplot users in particular,
>
> ggplot makes it very easy to plot things given their names when you
> use it interactively (and therefore can provide the names of the
> columns).
> qplot(x,foo,data=A) where A has columns (x,y,foo,bar) for example
>
> but I would like to use this from inside a function to which the name
> of the column is given. I cannot find an elegant way to make this
> work. Here are my attempts:
>
> #------------------------------------------------------------
>
> library(ggplot2)
>
> A = data.frame(x=rep(1:10,10), y=rep(1:10,each=10), u=runif(100),
> v=rnorm(100))
>
> # goal: extract values for y<=5 and plot them, either for u or v
>
> foo1 <- function(uv="u")
> {
> # solution 1: do not use the data argument at all
> # (forces the use of qplot, could be more elegant)
> B = A[A$y<=5,]
> qplot(B$x, B$y, fill=B[[uv]], geom="tile")
> }
>
> foo2 <- function(uv="u")
> {
> # solution 2: extract and rename the colums, then use the data argument
> # (enables ggplot but could be shorter)
> B = A[A$y<=5,c("x","y",uv)]
> names(B)[3] = "value"
> # rem: cannot use rename since rename(B,c(uv="value")) would not work
> qplot(x, y, fill=value, data=B, geom="tile")
> # or
> # ggplot(B,aes(x=x,y=y,fill=value)) + geom_tile()
> }
>
> foo3 <- function(uv="u")
> {
> # solution 3: use the data argument and perform the extraction
> directly in it
> # (elegant and powerful but can't make it work)
> ggplot(A[A$y<=5,c("x","y",uv)],aes(x=x,y=y,fill=???)) + geom_tile()
> # or
> ggplot(A[A$y<=5,],aes(x=x,y=y,fill=???)) + geom_tile()
> # or ...
> }
>
> print(foo1("u"))
> print(foo1("v"))
> print(foo2("u"))
> print(foo3("u"))
>
> #------------------------------------------------------------
>
> Any help in making foo3 work would be appreciated. Thanks in advance
> for your expertise.
>
> JiHO
> ---
> http://jo.irisson.free.fr/
>
> ______________________________________________
> 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.
>

-- 
http://had.co.nz/

______________________________________________
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.
Received on Thu 13 Dec 2007 - 14:59:55 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.