If you prefer not to receive future e-mail offers for products or services from Key
send an e-mail to mailto:DNERe...@key.com with 'No Promotional E-mails' in the SUBJECT line.
In this case, you don't even need to invoke stat_summary:
ggplot(data.x, aes(x)) +
geom_density() +
geom_vline(aes(xintercept = mean(x)))
> Thanks very much for the help.
>
> Adam Loveland
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University
No, it doesn't. That wasn't part of the original question :)
I don't think you can get it to work with stat_summary either.
stat_summary will "Summarise y values at every unique x", not allow a
summary of each facet/group. (I'd love to see a more general version
that would, though.) For this, you will have to pre-compute the means.
# smaller data, to better see mean differences
data.x = data.frame(x = rnorm(20), set = rep(c("A","B"), each=10))
# this gives the overall mean in each facet, not a per-facet mean
ggplot(data.x, aes(x=x)) +
geom_density() +
geom_vline(aes(xintercept = mean(x), group=set)) +
facet_grid(set~.)
# pre-compute the means
means <- ddply(data.x, .(set), summarise, xmean=mean(x))
# supply the alternate data set to just the geom_vline layer.
ggplot(data.x, aes(x=x)) +
geom_density() +
geom_vline(data=means, aes(xintercept = xmean)) +
facet_grid(set~.)
> Adam Loveland
>
>
>
>
>
> From: Brian Diggs<diggsb-k1...@public.gmane.org>
> To: ggplot2<ggplot2-/JYPxA39Uh5...@public.gmane.org>
> Date: 10/03/2011 01:29 PM
> Subject: Re: vline using stat_summary
> Sent by: ggplot2-/JYPxA39Uh5...@public.gmane.org
>
>
>
> On 10/3/2011 8:24 AM,
I tried the same process as you did, Brian, but your first method
doesn't work on my system (Win 7, 2.13.1, ggplot 0.8.9). I needed to
summarize the means into a data frame with ddply and pass those means
to geom_vline() (method 2) before it would work. I tried using group =
both in the ggplot() statement and in geom_vline(), but neither
worked, and offhand I don't see why they shouldn't.
My example:
d <- data.frame(g = factor(rep(paste('Group', 1:3), each = 100)),
y = rnorm(300))
# produces same overall mean in each panel
ggplot(d, aes(x = y)) +
geom_density() +
geom_vline(aes(xintercept = mean(y), group = g)) +
facet_grid(g ~ .)
dm <- ddply(d, .(g), summarise, m = mean(y))
# produces different means in each panel
ggplot(d, aes(x = y)) +
geom_density() +
geom_vline(data = dm, aes(xintercept = m)) +
facet_wrap( ~ g, ncol = 1)
Dennis
It's called ddply ;)
Hadley
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/