Somewhere I'm making a very simple mistake. Thank you for pointing out where this mistake is happening...
I'd like to create a violin plot with overlaying confidence intervals and a group means. Much like the example at this terrific new package.
The generic violin plot without any CI's works fine:
p <- ggplot(data=diamonds, aes(x=cut, y=price))
p + geom_violin(aes(x=cut, y=price)) +
geom_jitter(height = 0, width = 0.1, alpha = 0.05) +
geom_crossbar(stat="summary", fun.y=mean, fun.ymax=mean, fun.ymin=mean,
fatten=2, width=.5)
My issues is incorporating the confidence intervals.
I start by creating the necessary summary stats:
errbar_lims <- group_by(diamonds, cut) %>%
summarize(mean=mean(price), se=sd(price)/sqrt(n()),
upper=mean+(2*se), lower=mean-(2*se))
Then I thought to add to the plot with geom_errorbar
:
p <- ggplot(data=diamonds, aes(x=cut, y=price))
p + geom_violin(aes(x=cut, y=price)) +
geom_jitter(height = 0, width = 0.1, alpha = 0.05) +
geom_crossbar(stat="summary", fun.y=mean, fun.ymax=mean, fun.ymin=mean,
fatten=2, width=.5) +
geom_errorbar(aes(x= ymax=errbar_lims$upper, ymin=errbar_lims$lower),
stat='identity', width=.25)
But ggplot's error keeps informing me that I'm mapping to a single group aesthetic instead of the 5 groups that I'm trying to plot.
Perhaps my mistake is how I've set the mapping at the very beginning?
UPDATE
After reviewing a few of the comments after posting initially, the full script now looks as follows:
errbar_lims <- group_by(diamonds, cut) %>%
summarize(mean=mean(price), se=sd(price)/sqrt(n()),
upper=mean+(2*se), lower=mean-(2*se))
p <- ggplot(data=diamonds, aes(x=cut, y=price))
p + geom_violin(aes(x=cut, y=price)) +
geom_jitter(height = 0, width = 0.1, alpha = 0.05) +
geom_crossbar(stat="summary", fun.y=mean, fun.ymax=mean, fun.ymin=mean,
fatten=2, width=.5) +
geom_errorbar(aes(x = cut, ymin = lower, ymax = upper), errbar_lims, inherit = FALSE)
Yet an error persists:
Error in FUN(X[[i]], ...) : object 'price' not found