1
votes

I am using the ggbarplot function from the ggpubr package to generate a bar plot. I would like to keep using ggpubr instead of ggplot2 because it offers a nice way to add grouped significance bars (http://www.sthda.com/english/articles/24-ggpubr-publication-ready-plots/76-add-p-values-and-significance-levels-to-ggplots/).

I would like to add my own manual error bars, as one would normally do with the ymax and ymin arguments in ggplot2:

geom_errorbar(aes(ymax = upper_errorbar, ymin = lower_errorbar))

Is there a way to manually add the equivalent ymin and ymax values for an error bar withing ggpubr? From a look through the manual I don't see a way but would like to be sure.

1

1 Answers

0
votes

Yes, you can add geom's to a ggpubr plot the same way you add geom's to a ggplot.

Minimal reproducible example:

library(tidyverse)
library(ggpubr)

data("ToothGrowth")
df <- ToothGrowth %>%
  group_by(dose, supp) %>%
  mutate(mean_len = mean(len)) %>%
  mutate(upper_ci = mean_len + sd(len)/sqrt(length(len)),
         lower_ci = mean_len - sd(len)/sqrt(length(len)))

ggbarplot(df, x = "dose", y = "len", fill = "supp", merge = TRUE, add = "mean") +
  geom_errorbar(aes(group = supp, ymax = upper_ci, ymin = lower_ci),
                position = position_dodge(width = 0.8), width = 0.25)

Created on 2021-07-21 by the reprex package (v2.0.0)