Plot mean and sd of dataset per x value using ggplot2
ggplot2, r
Solution
You could try writing a summary function as suggested by Hadley Wickham on the website for `ggplot2`: http://had.co.nz/ggplot2/stat_summary.html. Applying his suggestion to your code:
p <- qplot(x, y, data=a)
stat_sum_df <- function(fun, geom="crossbar", ...) {
stat_summary(fun.data=fun, colour="blue", geom=geom, width=0.2, ...)
}
p + stat_sum_df("mean_cl_normal", geom = "smooth")
This results in this graphic:
Problem
I have a dataset that looks a little like this: ``` a <- data.frame(x=rep(c(1,2,3,5,7,10,15,20), 5), y=rnorm(40, sd=2) + rep(c(4,3.5,3,2.5,2,1.5,1,0.5), 5)) ggplot(a, aes(x=x,y=y)) + geom_point() +geom_smooth() ``` I want the same output as that plot, but instead of smooth curve, I just want to take line segments between the mean/sd values for each set of x values. The graph should look similar to the above graph, but jagged, instead of curved. I tried this, but it fails, even though the x values aren't unique: ``` ggplot(a, aes(x=x,y=y)) + geom_point() +stat_smooth(aes(group=x, y=y, x=x)) geom_smooth: Only one unique x value each group.Maybe you want aes(group = 1)? ```