The following code produces bar plots with standard error bars using Hmisc, ddply and ggplot:
means_se
However, implementing the above using helper functions such as mean_sdl seems much better. For example the following code produces a plot with 95% CI error bars:
ggplot(mtcars, aes(cyl, qsec)) + stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = mean_sdl, geom = "errorbar")
My question is how to use the stat_summary implementation for standard error bars. The problem is that to calculate SE you need the number of observations per condition and this must be accessed in mean_sdl's multiplier.
How do I access this information within ggplot? Is there a neat non-hacky solution for this?
Well, I can't tell you how to get a multiplier by group into stat_summary
.
However, it looks like your goal is to plot means and error bars that represent one standard error from the mean in ggplot
without summarizing the dataset before plotting. Here is an easy way to do that, using the mean_cl_normal
function from Hmisc
and changing the multiplier to 1 so you get one standard error from the mean.
ggplot(mtcars, aes(cyl, qsec)) + stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", mult = 1)
EDIT Update for ggplot_2.0.0
Starting in ggplot2 version 2.0.0, arguments that you need to pass to the summary function you are using needs to be given as a list to the fun.args
argument. The mult
argument is an argument for mean_cl_normal
.
ggplot(mtcars, aes(cyl, qsec)) + stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = mean_cl_normal, geom = "errorbar", fun.args = list(mult = 1))
However, there is now a mean_se
function in ggplot2 that we can use instead of mean_cl_normal
from Hmisc. The mean_se
function has a multiplier of 1 as the default so we don't need to pass any extra arguments if we want standard error bars.
ggplot(mtcars, aes(cyl, qsec)) + stat_summary(fun.y = mean, geom = "bar") + stat_summary(fun.data = mean_se, geom = "errorbar")