[R] Problem with ggplot2 - facet_wrap and boxplot

hadley wickham h.wickham at gmail.com
Wed Jan 7 18:04:49 CET 2009


Hi David,

> Here the problem :
>
> library(ggplot2)
>
> df <- data.frame(id = 1:100, x1 = c(rnorm(50), rnorm(50, 1)), x2 =
> c(rnorm(50), rnorm(50, 1.5)), x3 = c(rnorm(50, 0.5), rnorm(50, 2.5)), group
> = as.factor(rep(c("a", "b"), each = 50)))
> df.melt <- melt(df, id = c("id", "group"))
> head(df.melt)
>
> p <- ggplot(df.melt, aes(variable, value))
> p + geom_boxplot(aes(fill = group))
> # This graph is great, but I would like to have two panels, one for group
> 'a', and one for group 'b'
> # With this code, I can't have one boxplot for each variable x1, x2 and x3
> p + geom_boxplot(aes(fill = group)) + facet_wrap(~group)
> # but it's working with geom_point
> p + geom_point(aes(color = group)) + facet_wrap(~group)

This is an annoying bug I haven't figure out how to fix yet.  The
basic problem is that if you facet by a variable that has the same
name as an aesthetic, it overrides that aesthetic (group in this
case).  Unfortunately, currently the only work around is call the
variable something other than group.

Regards,

Hadley

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




More information about the R-help mailing list