Facetted boxplots

12 views
Skip to first unread message

Rob James

unread,
Sep 29, 2009, 12:21:03 PM9/29/09
to ggp...@googlegroups.com
I'm looking for syntax suggestions, so I don't have a reproducible example. The question is the interaction of stat_summary, facet and geom_boxplot.

I want to generate a horizontally-facetted set of boxplots, with each facet defined by a separate variable,e g. (from diamonds, depth and price)
Within each facet, I'd like to condition the distribution on a categorical variable, e.g. cut.  The simple idea is to be able to compare set of continuous values and look for distributional changes across the categorical variable within each facet.
In my case I want to show that height, weight and bmi across "pre" and "post" periods.  Therefore, it makes sense to have the "pre" and "post" boxplots in the same facet, and then to have the variables change across facets.

So, there must be some incantation of stat_summary or its ilk that will assist with this, but it evades me.

What I am looking for should do something like:

geom_boxplot(aes(x=cut, y=?)+facet_grid(c(depth, price) ~.) where variable is c(depth, price)

but, without having a clear way to specify the  "y" across the two continuous variables depth and price, I'm lost.

Suggestions?

(I recognize that I'll also have to use some sort of scales_free option as well.)


hadley wickham

unread,
Sep 29, 2009, 1:17:26 PM9/29/09
to r...@aetiologic.ca, ggp...@googlegroups.com
Hi Rob,

You'll need to melt your data (using the reshape package) so that you
get a variable with values height, weight and bmi. Then you can facet
by that.

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

Tim Triche

unread,
Sep 29, 2009, 1:36:06 PM9/29/09
to hadley wickham, r...@aetiologic.ca, ggp...@googlegroups.com
out of curiosity, are there some general guidelines for when a dataset is too big for 'reshape'?

I was fussing with some DNA microarrays in a twin study and wanted to plot correlations within pairs... melt() took about 30 minutes on a box with 64GB of RAM, and cast() took another 20 minutes.

> dim(combined)
[1] 19575   288

If I reduce it to, say, the first 10 principal components then of course it's not so bad.  But that's no good for what I need to do.

I ended up using 'sqldf' and variations on the theme of subset() to manually reshape the data for ggplot2.  Reshape would be an awful lot nicer, though.  I guess I could try parallelizing the lapply() and tapply() calls in melt() and cast(), or swapping in the relevant bits from 'plyr'... ?

What are the guidelines that you have for determining when to use 'melt' and when to do it yourself?  The functions I wrote to rearrange the dataframes runs in a few seconds or less, but it's nowhere near as flexible as using a molten dataset in qplot().  Even if I ran the *apply() loops across 32 worker processes, it seems like melt() and cast() would still be slower than sqldf.

Maybe that's just a reflection of what SQLite is optimized to do -- in which case, maybe there's a way to use SQLite/sqldf instead of 'reshape' for some of these tasks?  Hmmmm.
--
Statisticians, like artists, have a bad habit of falling in love with their models.
--George Box

hadley wickham

unread,
Sep 29, 2009, 2:07:01 PM9/29/09
to ttr...@usc.edu, r...@aetiologic.ca, ggp...@googlegroups.com
> out of curiosity, are there some general guidelines for when a dataset is
> too big for 'reshape'?

Only when it takes too long ;)

> I was fussing with some DNA microarrays in a twin study and wanted to plot
> correlations within pairs... melt() took about 30 minutes on a box with 64GB
> of RAM, and cast() took another 20 minutes.

Wow - that is really slow for melt. Is there someway you could
provide me with the data? Or the same basic structure but with the
data replaced by random values? I'm fairly certain it should be
possible to do much better for melt.

With respect to cast, I have a half rewritten version of reshape based
on the ideas from plyr that is 10-15 times faster for pure reshaping
and takes up much less memory. Even better, it actually has a decent
theoretical underpinning so I can actually understand how it works
(it's all about figuring out how to move the data around and then
doing it all at once). Unfortunately I don't currently have the time
to finish off the work and make it completely compatible with the
existing version of reshape.

Hadley

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

Reply all
Reply to author
Forward
0 new messages