help with error bars in ggpubr

Hi everyone,

I have the values of the error bars, and I want to specify the values in "ggpubr" .
It seems like the "add," and "error.plot" functions have a lot of possibilities (e.g., "mean_sd"), but I couldn't find anything that will allow me to specify the values myself.
I also tried "geom_errorbar," but it doesn't work properly.
I know, next time I will use ggplot2 for flexibility.

example code -

df <- data.frame(stringsAsFactors = FALSE, "pse" = c(40, 42, 41, 40, 60, 61, 62, 60, 39, 38, 40, 39, 59, 58, 60, 59 ))

df[1:4,2]="30 cm"
df[5:8,2]="60 cm"
df[9:12,2]="30 cm"
df[13:16,2]="60 cm"
df[1:8,3] = "3.5 cm"
df[9:16,3] = "6.5 cm"
colnames(df)[2]="Size"
colnames(df)[3]="Distance"

my_comparisons <- list( c("Near", "Far"))
ggbarplot(df, x = "Size", y = "pse", fill ="Distance", color = "Distance", ylim=c(25,75), width = 0.6, add = c("mean_se", "jitter"), palette = c("#000000", "#111111"),
position = position_dodge(0.65))+
theme(legend.position = "top")+ 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)")

I am replying to get this topic up. I hope this is ok.

If you want to improve your chances of getting help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

Thank you!

I have changed the post accordingly.

So in case, someone will come across this question in the future.

Here is the solution I found-

  1. 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)
}
  1. next you add the error bars using geom_errorbar() function

  2. 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))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.