如何将数据点添加到具有 p 值的分组条形图

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

提问人:Trinh Phan-Canh 提问时间:9/28/2023 最后编辑:Trinh Phan-Canh 更新时间:9/28/2023 访问量:28

问:

当我尝试使用本教程制作条形图时,我遇到了问题 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
)

我正在尝试添加数据点,但点不能与列很好地对齐。

enter image description here非常感谢您的帮助!

r ggplot2 ggpubr rstatix

评论

2赞 Gregor Thomas 9/28/2023
请直接在问题中分享一些示例数据和您的代码。
0赞 M-- 9/28/2023
@GregorThomasdatasets::ToothGrowth

答:

0赞 Trinh Phan-Canh 9/28/2023 #1

我终于找到了方法:

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"))

谢谢