throwing variable to facet_wrap

65 views
Skip to first unread message

Yohan Sutjandra

unread,
Sep 9, 2010, 5:34:08 PM9/9/10
to ggplot2
Can I create a function that does qplot and facet_wrap based on the
passed variable.

For example I want fn("color") will create ggplot object with
facet_wrap(~color)
and fn("size") will create ggplot object with facet_wrap(~size)

I can always do an if then, but just wondering if something like
aes_string is available for facet

Thanks in advance,

Mark Connolly

unread,
Sep 9, 2010, 5:50:04 PM9/9/10
to Yohan Sutjandra, ggplot2
yes, with aes_string

Mark Connolly

unread,
Sep 9, 2010, 5:55:28 PM9/9/10
to Yohan Sutjandra, ggplot2
I am lying to you. Reflex response. Sorry about that.

On 09/09/2010 05:34 PM, Yohan Sutjandra wrote:

Joshua Wiley

unread,
Sep 9, 2010, 6:03:18 PM9/9/10
to Yohan Sutjandra, ggplot2
Do you mean something like this?

fn <- function(wrap) {
myformula <- formula(paste("~", wrap, sep=''))
return(facet_wrap(facets = myformula))
}

dat <- data.frame(a = 1:10L, b = runif(10), c = factor(rep(0:1, each = 5)))

qplot(x = a, y = b, data = dat) + fn("c")

I was not quite sure if you wanted fn() to have arguments to handle
the call to qplot() also.

Josh

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2


--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/

Mark Connolly

unread,
Sep 9, 2010, 6:08:57 PM9/9/10
to Joshua Wiley, Yohan Sutjandra, ggplot2
It even seems to "just work" without a formula creation:

fred = "depth_cm ~ ."
ggplot(soil) + aes(bulk_density_gm_cc,value,color=variable) +
geom_point() + geom_smooth() + facet_grid(fred)

Mark Connolly

unread,
Sep 9, 2010, 6:17:49 PM9/9/10
to Joshua Wiley, Yohan Sutjandra, ggplot2
Lying again... facet_grid does not seem to require creating a
formula. facet_wrap does.

I'm batting somewhere around .110 I think.

Yohan Sutjandra

unread,
Sep 9, 2010, 9:23:20 PM9/9/10
to ggplot2
Using formula works. Thanks all for your support.

On Sep 9, 6:17 pm, Mark Connolly <mark_conno...@acm.org> wrote:
>   Lying again... facet_grid does not seem to require creating a
> formula.  facet_wrap does.
>
> I'm batting somewhere around .110 I think.
>
> On 09/09/2010 06:08 PM, Mark Connolly wrote:
>
>
>
> >  It even seems to "just work" without a formula creation:
>
> > fred = "depth_cm ~ ."
> > ggplot(soil) + aes(bulk_density_gm_cc,value,color=variable) +
> >   geom_point() + geom_smooth() + facet_grid(fred)
>
> > On 09/09/2010 06:03 PM, Joshua Wiley wrote:
> >> Do you mean something like this?
>
> >> fn<- function(wrap) {
> >>    myformula<- formula(paste("~", wrap, sep=''))
> >>    return(facet_wrap(facets = myformula))
> >> }
>
> >> dat<- data.frame(a = 1:10L, b = runif(10), c = factor(rep(0:1, each =
> >> 5)))
>
> >> qplot(x = a, y = b, data = dat) + fn("c")
>
> >> I was not quite sure if you wanted fn() to have arguments to handle
> >> the call to qplot() also.
>
> >> Josh
>
> >> On Thu, Sep 9, 2010 at 2:34 PM, Yohan
> >> Sutjandra<yohan.deb...@gmail.com>  wrote:

Douglas Bates

unread,
Sep 10, 2010, 1:48:34 PM9/10/10
to Joshua Wiley, Yohan Sutjandra, ggplot2
On Thu, Sep 9, 2010 at 5:03 PM, Joshua Wiley <jwiley...@gmail.com> wrote:
> Do you mean something like this?
>
> fn <- function(wrap) {
>  myformula <- formula(paste("~", wrap, sep=''))
>  return(facet_wrap(facets = myformula))
> }

Generally using "paste" to create a formula is not the best approach,
although it is straightforward and, most of the time, it works.

A preferred idiom is substitute(~ foo, list(foo=as.name(wrap)), as in

> wrap <- "bar"
> str(substitute(~ foo, list(foo=as.name(wrap))))
language ~bar

If you want to be sure that it ends up being a formula, you evaluate
the expression, but that shouldn't be necessary here. In any case

> str(eval(substitute(~ foo, list(foo=as.name(wrap)))))
Class 'formula' length 2 ~bar
..- attr(*, ".Environment")=<environment: R_GlobalEnv>

Joshua Wiley

unread,
Sep 10, 2010, 2:01:03 PM9/10/10
to Douglas Bates, ggplot2
On Fri, Sep 10, 2010 at 10:48 AM, Douglas Bates <ba...@stat.wisc.edu> wrote:
> On Thu, Sep 9, 2010 at 5:03 PM, Joshua Wiley <jwiley...@gmail.com> wrote:
>> Do you mean something like this?
>>
>> fn <- function(wrap) {
>>  myformula <- formula(paste("~", wrap, sep=''))
>>  return(facet_wrap(facets = myformula))
>> }
>
> Generally using "paste" to create a formula is not the best approach,
> although it is straightforward and, most of the time, it works.
>
> A preferred idiom is substitute(~ foo, list(foo=as.name(wrap)), as in
>
>> wrap <- "bar"
>> str(substitute(~ foo, list(foo=as.name(wrap))))
>  language ~bar

Good to know, thanks for taking the time to mention it!
--Josh

Yohan Sutjandra

unread,
Sep 10, 2010, 3:26:52 PM9/10/10
to Douglas Bates, Joshua Wiley, ggplot2
It's probably more of a general R question, but just curious, what would be the difference between the two approach?

approach 1:
fn <- function(wrap)
return( formula(paste("~", wrap)) )

approach 2:
fn <- function(wrap)
return( substitute(~ foo, list(foo=as.name(wrap) ) ) )

I noticed the first approach is returning formula, and the second approach is returning call. Can you suggest resources to understand the difference between the two?

Thanks,
-- Yohan
Reply all
Reply to author
Forward
0 new messages