You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在复杂分组柱状图中添加指定组间的显著性统计标记?

解决方案:给分组柱状图添加组内显著性标记

一、代码问题分析

你之前的代码核心问题是未指定在每个浓度分组内进行组间比较,且显著性标记的位置未与柱状图的dodge布局对齐,导致标记无法正确显示。

二、修正后的geom_signif实现方法

使用geom_signif时,需明确指定在每个concentrations分组内对比samples,并设置position匹配柱状图的dodge宽度:

# 定义需要的对比组:SNCA分别与CTRL、SNCB对比
my_comparisons <- list(c("SNCA", "CTRL"), c("SNCA", "SNCB"))

barre <- ggplot(tgallto2, aes(x = factor(concentrations, levels = level_order), 
                              y = value, 
                              fill = samples,  # 用fill区分三组,提升可视化辨识度
                              group = samples)) + 
  scale_fill_manual(values = c("CTRL" = "blue", "SNCB" = "darkblue", "SNCA" = "lightblue")) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9), color = "black") +
  geom_errorbar(aes(ymin = value - se, ymax = value + se), 
                position = position_dodge(width = 0.9), width = 0.3) +
  theme_classic() +
  # 关键配置:在每个x分组内执行对比,位置匹配dodge布局
  geom_signif(comparisons = my_comparisons,
              map_signif_level = TRUE,
              y_position = c(6, 6.8),  # 调整垂直位置避免标记重叠
              position = position_dodge(width = 0.9),
              test = "t.test",
              tip_length = 0.01) +
  theme(axis.title.y = element_text(size=15, vjust = 2),
        axis.title.x = element_text(size=15, vjust = -0.5),
        axis.text = element_text(size=15, color = "#000000"))

barre + ylim(0, 8) + 
  ylab("OD600 nm") + 
  xlab("Thiamine concentrations uM") + 
  ggtitle("Thiamine effect")

三、修正后的stat_compare_means实现方法

stat_compare_means需指定group = samples,并设置position_dodge对齐柱状图,同时指定目标对比组:

my_comparisons <- list(c("SNCA", "CTRL"), c("SNCA", "SNCB"))

barre <- ggplot(tgallto2, aes(x = factor(concentrations, levels = level_order), 
                              y = value, 
                              fill = samples,
                              group = samples)) + 
  scale_fill_manual(values = c("CTRL" = "blue", "SNCB" = "darkblue", "SNCA" = "lightblue")) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9), color = "black") +
  geom_errorbar(aes(ymin = value - se, ymax = value + se), 
                position = position_dodge(width = 0.9), width = 0.3) +
  theme_classic() +
  # 关键配置:在每个x分组内比较samples,位置匹配dodge布局
  stat_compare_means(comparisons = my_comparisons,
                     label = "p.signif",
                     method = "t.test",
                     position = position_dodge(width = 0.9),
                     tip.length = 0.01) +
  theme(axis.title.y = element_text(size=15, vjust = 2),
        axis.title.x = element_text(size=15, vjust = -0.5),
        axis.text = element_text(size=15, color = "#000000"))

barre + ylim(0, 8) + 
  ylab("OD600 nm") + 
  xlab("Thiamine concentrations uM") + 
  ggtitle("Thiamine effect")

四、统计方法建议

每个浓度下有3组样本,直接多次使用t-test会增加假阳性率,更严谨的做法是:

  1. 先对每个浓度组做单因素ANOVA,检验三组间是否存在显著差异;
  2. 若ANOVA结果显著,再使用TukeyHSD事后检验(或Dunnett检验,以CTRL为对照组)进行组间两两比较。

修改代码中的统计方法即可实现:

# 使用ANOVA+TukeyHSD多重比较(自动校正p值)
geom_signif(comparisons = my_comparisons,
            map_signif_level = TRUE,
            y_position = c(6, 6.8),
            position = position_dodge(width = 0.9),
            test = "pairwise.t.test",
            p.adjust.method = "holm",  # 选择p值校正方法
            tip_length = 0.01)

内容的提问来源于stack exchange,提问作者Chiara Romano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 00:55:28