So in case, someone will come across this question in the future.
Here is the solution I found-
- first you summarize the data, I used this function:
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
-
next you add the error bars using geom_errorbar() function
-
And if its not obvious change add = c("mean_se", "jitter") to add = c("mean", "jitter")
SO in my example:
data_summary <- function(data, varname, groupnames){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, groupnames, .fun=summary_func,
varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
Summarize the data :
df2 <- data_summary(x, varname="PSE",
groupnames=c("Size", "Distance"))
df2$Size=as.factor(df2$Size)
my_comparisons <- list( c("Near", "Far"))
ggbarplot(x, x = "Size", y = "PSE", fill ="Distance", color = "Distance", ylim=c(25,75), width = 0.6, add = c( "mean", "jitter"), palette = c("#000000", "#111111"),
position = position_dodge(0.65))+ theme_bw() +theme(axis.text=element_text(size=14),axis.title=element_text(size=14))+ scale_fill_grey(start=0.8, end=0.95)+ theme(legend.position = "top")+ ylab ("PSE (mm)")+geom_errorbar(data=df2, mapping=aes(x=Size, y=PSE, color=Distance, ymin=PSE-0.32, ymax=PSE+0.32), width=.15, position=position_dodge(.6))