I usually use mean +/- se (standard error) for error bar, though
mean+-SE is old-fashioned.
Could you please consider to add a brief function for this in the next
version of ggplot2?
mean_se<-function (x, ...)
{
x<-na.omit(x)
se<-function(x)sqrt(var(x)/length(x))
data.frame(y=mean(x), ymin=mean(x)-se(x), ymax=mean(x)+se(x))
}
you can use like this:
qplot(cyl, mpg, data=mtcars)+stat_summary(fun.data = mean_se, colour = "red")
actually it is easy to define by myself, but... i wrote this function
for many times.
thanks in advance.
Hadley
> --
> 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
>
--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/
Thanks Hadley, I'm happy to hear your agreement.
As for Elaine's proposal, probably there are two approach.
One is full-parametarized:
mean_se<-function (n=1)
{
return(function(x){
x<-na.omit(x)
se<-function(x)sqrt(var(x)/length(x))*n
data.frame(y=mean(x), ymin=mean(x)-se(x), ymax=mean(x)+se(x))
})
}
usage:
qplot(cyl, mpg, data=mtcars)+stat_summary(fun.data = mean_se(), colour = "red")
qplot(cyl, mpg, data=mtcars)+stat_summary(fun.data = mean_se(2), colour = "red")
this is flexible, but inconsistent to the other mean_*, e.g.,
mean_cl_boot, because they are not function apparently.
another approach is that functionized one:
mean_2se<-function(x){
x<-na.omit(x)
se<-function(x)sqrt(var(x)/length(x))*2
data.frame(y=mean(x), ymin=mean(x)-se(x), ymax=mean(x)+se(x))
}
qplot(cyl, mpg, data=mtcars)+stat_summary(fun.data = mean_2se, colour = "red")
this maybe too ad-hoc, but is consistent to the others. In addition,
2se or 3se is enough for this purpose.
I don't know which is better approach. anyway, the implementation is easy.
Thanks.
mean_se <- function(x, mult = 1) {
x <- na.omit(x)
se <- mult * sqrt(var(x) / length(x))
mean <- mean(x)
data.frame(y = mean, ymin = mean - se, ymax = mean + se)
}
qplot(cyl, mpg, data=mtcars) +
stat_summary(fun.data = mean_se, colour = "red", mult = 2)
Hadley