I thought this was already the case: if you use stat_summary and specify a colour or a group to aes, it should compute the summary per group (stat_qq does at least).
> Alternativelty, I'd like to see function calculations in aes be
> applied per group:
>
> for instance, I want to be able to plot a mean hline and vline on a
> scatter plot:
>
>> mc=mutate(mtcars,cyl=factor(cyl)
>
>> ggplot(data=mc,x=cyl,y=mpg) + geom_point() +
>> geom_hline(aes(y=mean(mpg),group=cyl))
>
> another for instance, if I have grouped data, I might want to
> summarise and add a text label for each, in an appropriate location:
>
>> qplot(data=data.frame(x=rnorm(100),cat=sample(letters[1:4],
> 100,T)),
> x=x)+geom_text(aes(group=cat,y=10,label=10,x=mean(x,y=max(x))))
>
> Where x=mean is calculated per group.
The answer is probably: compute your summaries first and plot afterwards. plyr is there to help you compute such per-group summaries in one line. ggplot can't do everything.
> This does not currently
> operate like this. It's odd that I can put a trendline or smoothed
> kernel line but not a mean line.
You can in those instances because the data is fed through a statistic which computes things per group (hence my remark above). In the syntax you present here, the data is fed to stat_identify which does not compute anything (and actually, the data fed to stat_identify is probably the result of the computation specified in the aes call already).
Jean-Olivier Irisson
---
Observatoire Océanologique
Station Zoologique, B.P. 28, Chemin du Lazaret
06230 Villefranche-sur-Mer
Tel: +33 04 93 76 38 04
Mob: +33 06 21 05 19 90
http://jo.irisson.com/
-Alex Brown
On Mar 16, 2012, at 5:41 AM, Jean-Olivier Irisson <iri...@normalesup.org> wrote:
> On 2012-Mar-16, at 07:20 , Alex@775 wrote:
>>
>>
>> I'd like to see a stat_group or similar that allows simple stat
>> calculation per group.
>
> I thought this was already the case: if you use stat_summary and specify a colour or a group to aes, it should compute the summary per group (stat_qq does at least).
Stat summary computes stats per group per unique x. I want just per group.
>
> The answer is probably: compute your summaries first and plot afterwards. plyr is there to help you compute such per-group summaries in one line. ggplot can't do everything.
>
That's what I do today. However, I say again, it's odd ggplot is happy to compute smoothing kernels and per x stats but not something so clean amd simple as mean per group.
I want convenience methods sometimes. And it would be so easy.
Also there's a problem in .89 at least with pre-computed stats, if nrow subset data for one facet is 0, ggplot fails.
-Alex
ggplot(mtcars, aes(x = cyl, y=mpg)) + geom_point() +
geom_hline(aes(yintercept=mean(mpg)))
2012年3月16日23:20 Alex Brown <alexb...@gmail.com>:
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
--
--
Kohske Takahashi <takahash...@gmail.com>
Research Center for Advanced Science and Technology,
The University of Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html
That made me think that this:
library("ggplot2")
d <- data.frame(x=runif(100), y=c(runif(50), 1.5*runif(50)), a=rep(c("a", "b"), each=50))
ggplot(d, aes(x=x, y=y, colour=a)) + geom_point() + geom_smooth(se=FALSE, method="lm", formula=y~1)
does what you want (one mean per group)… but that's probably cheating ;)
NB: lm~1 is a linear model without slope.
Then I understood your point when trying with a continuous x. stat_summary is obviously meant for discrete x and I don't know how to overcome that, sorry.