How to add data points to a grouped barplot with p value

45 Views Asked by At

I have a problem when I try to make my bar plot with this tutorial https://www.datanovia.com/en/blog/how-to-add-p-values-onto-a-grouped-ggplot-using-the-ggpubr-r-package/

library(ggpubr)
library(rstatix)
#data
df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df, 3)
#pvalue
stat.test <- df %>%
  group_by(dose) %>%
  t_test(len ~ supp) %>%
  adjust_pvalue(method = "bonferroni") %>%
  add_significance("p.adj")
stat.test


# Create a bar plot with error bars (mean +/- sd)
bp <- ggbarplot(
  df, x = "dose", y = "len", add = "mean_sd", 
  color= "supp", palette = c("#00AFBB", "#E7B800"),
  position = position_dodge(0.8)
) + geom_point()
# Add p-values onto the bar plots
stat.test <- stat.test %>%
  add_xy_position(fun = "mean_sd", x = "dose", dodge = 0.8) 
bp + stat_pvalue_manual(
  stat.test,  label = "p.adj.signif", tip.length = 0.01
)

# Move down the brackets using `bracket.nudge.y`
bp + stat_pvalue_manual(
  stat.test,  label = "p.adj.signif", tip.length = 0,
  bracket.nudge.y = -2
)

I am trying to add data points, but the dots cannot align well with the columns.

enter image description here Thank you very much for your help!

1

There are 1 best solutions below

0
Trinh Phan-Canh On

I finally found the way:

bp <- ggbarplot(
  df, x = "strains", y = "survival", add = "mean_sd", 
  fill= "condition", palette = c("#00AFBB", "#E7B800"),
  position = position_dodge(0.8)
) + 
geom_point(size=2,aes(col=condition), alpha=.5, position = position_dodge(0.8)) +   scale_color_manual(values = c("black", "black"))

Thank you very much