How to facet a box plot with binning

282 views
Skip to first unread message

Timothy Lau

unread,
Jun 6, 2015, 10:10:09 PM6/6/15
to ggp...@googlegroups.com
I have a ratio that I'd like broken up by state I kept getting errors about not having a "y" variable in my aes.

Here's what I have:
ggplot(data = d0[d0$year == 2010,], aes(state, totalmds/poptotal)) + geom_boxplot(outlier.colour = "red") + theme(axis.text.x = element_text(angle = 90, hjust = 1))


Here's what I'm trying that's not working:

ggplot(data = d0[d0$year == 2010,], aes(totalmds/poptotal)) + geom_boxplot(outlier.colour = "red") + facet_wrap( ~ state)

> Error: stat_boxplot requires the following missing aesthetics: y

If I try the stat = "bin" within the geom, 
ggplot(data = d0[d0$year == 2010,], aes(totalmds/poptotal)) + geom_boxplot(stat = "bin", outlier.colour = "red") #+ facet_wrap( ~ state)

I get:
Error: geom_boxplot requires the following missing aesthetics: lower, upper, middle, ymin





Does anyone have a suggestion on how I can get rid of the missing aesthetics error to make those individual box plots in the above graphic split up individually?

Thanks,
Timothy

John Rauser

unread,
Jun 8, 2015, 10:03:25 PM6/8/15
to Timothy Lau, ggplot2 mailing list
It might be useful to know that the first two arguments to aes() are x= and y= by default, so the two ggplot() calls below are equivalent.

library(ggplot2)
dat<-data.frame(state=rep(letters,100),value=rnorm(26*100))
ggplot(dat,aes(state,value))+geom_boxplot()
ggplot(dat,aes(x=state,y=value))+geom_boxplot()

You can get what I think you're after by mapping the x-axis position to a dummy variable.

ggplot(dat,aes(x=1,y=value))+geom_boxplot()+facet_wrap(~state)

I hope that's helpful,

-J





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

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Timothy Lau

unread,
Jun 12, 2015, 2:58:50 PM6/12/15
to ggp...@googlegroups.com, timoth...@gmail.com
@John Rauser,

The dummy variable suggestion nailed it. Thank you.
Reply all
Reply to author
Forward
0 new messages