[R] ggplot2 boxplot confusion
hadley wickham
h.wickham at gmail.com
Thu Feb 28 02:32:47 CET 2008
> I noticed the coord flip problem during my ggplot investigations. Is this
> something I can override by getting into the code?
The basic problem is all the geoms/stats in ggplot are based around
the assumption that we are interested in Y | X, rather than X | Y. I
don't think this is an unreasonable as it simplify much of the code
and makes most common plots easier to specify. However, it is rather
restrictive in your case - what you are trying to do make sense, but
the parameterisation of the prebuild layers in ggplot makes it very
difficult.
However, there is one geom that is parameterised in the opposite
direction: geom_vline. So your second option "just draw the density
plots with vertical lines drawn at the median, IQR etc), is fairly
easy in the development version of ggplot:
q5 <- function(x) unname(quantile(x, c(0.05, 0.25, 0.5, 0.75, 0.95)))
qplot(carat, data=diamonds, geom="density") + geom_vline(intercept="q5")
(this won't work in the current version because I've only just allow
geom_vline to use a function to calculate the intercept). I can send
you a copy of the development version if you let me know your OS.
Alternatively, you could write your own versions of stat_boxplot and
geom_boxplot (or stat_density and geom_area) that work in the opposite
direction to usual. This probably isn't too hard if you just take the
code and change x's to y's (and vice versa), but it's currently
completely undocumented.
Hadley
> On the coord flipping problem I was thinking to grab the density data
> explicitly, swap x and y and then plot as a scatter plot with a box plot
> overlaid.
>
> Or perhaps just draw the density plots with vertical lines drawn at the
> median, IQR etc
>
> Or perhaps draw the density plots and fake a boxplot by drawing bars
> explicitly.
>
> I'm hoping you can at least advise which of any of these routes is likely to
> be a dead end.
>
> regards
>
> Chris
>
>
>
>
>
>
>
> hadley wrote:
> >
> >> Now I think I understand want you want. I'm affraid that won't be easy
> >> because you're trying to mix continuous variables with categorical ones
> >> on the same scale. A density plot has two continuous scales: VALUE and
> >> it's density. The boxplot has a continuous scale (VALUE) and the other
> >> is categorical. Maybe Hadley knows a solution for your problem.
> >
> > Well one idea is:
> >
> > ggplot(diamonds, aes(x = price)) + geom_density(aes(min =
> > -..density.., adjust= 0.5),fill="grey50", colour=NA) + facet_grid(. ~
> > cut) + coord_flip()
> >
> > which looks like it would naturally fit with a boxplot overlaid on top
> > of it. However, it's currently not possible because the boxplot is
> > parameterised so that it is always horizontal, while the density is
> > vertical - in the above example I have flipped the coordinate system,
> > but that flips both density plot and boxplot.
> >
> > Hadly
> >
> > --
> > http://had.co.nz/
> >
> >
>
> --
> View this message in context: http://www.nabble.com/ggplot2-boxplot-confusion-tp15706116p15725753.html
>
>
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
http://had.co.nz/
More information about the R-help
mailing list