On 09/09/2010 05:34 PM, Yohan Sutjandra wrote:
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/
fred = "depth_cm ~ ."
ggplot(soil) + aes(bulk_density_gm_cc,value,color=variable) +
geom_point() + geom_smooth() + facet_grid(fred)
I'm batting somewhere around .110 I think.
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>
Good to know, thanks for taking the time to mention it!
--Josh