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

R绘图显著性标记不显示及Tukey比较筛选问题求助

问题1:ANOVA/Tukey检验显著但图表无显著性星号

这类问题大多是因为显著性标记未正确关联到目标对比组,或是p值未正确传递给绘图函数。以下是具体解决步骤:

  • 先确保正确提取Tukey检验的配对p值,以emmeans包为例:
# 拟合ANOVA模型
model <- aov(Value ~ Concentration, data = df)
# 计算各组均值并执行Tukey事后检验
library(emmeans)
emm <- emmeans(model, ~ Concentration)
tukey_res <- pairs(emm, adjust = "tukey")
# 转换为数据框方便后续处理
tukey_df <- as.data.frame(tukey_res)
  • 若使用ggplot2+ggsignif绘图,需手动指定对比组并传入对应p值:
library(ggplot2)
library(ggsignif)

# 提前筛选出与Concentration_0的对比(对应问题2的解决方案)
target_comp <- tukey_df[grepl("Concentration_0", tukey_df$contrast), ]
# 整理对比组格式,拆分为两两组合
comparisons <- lapply(strsplit(target_comp$contrast, " vs "), function(x) x)

ggplot(df, aes(x = Concentration, y = Value)) +
  geom_boxplot() +
  geom_signif(
    comparisons = comparisons,
    y_position = seq(max(df$Value)*1.1, max(df$Value)*1.3, length.out = nrow(target_comp)),
    annotations = ifelse(target_comp$p.value < 0.001, "***", 
                         ifelse(target_comp$p.value < 0.01, "**", 
                                ifelse(target_comp$p.value < 0.05, "*", "ns"))),
    tip_length = 0.01
  )

若使用ggpubr包会更简便,可直接指定参考组:

library(ggpubr)
ggboxplot(df, x = "Concentration", y = "Value") +
  stat_compare_means(method = "anova") + # 添加ANOVA整体显著性
  stat_compare_means(
    method = "t.test", 
    ref.group = "Concentration_0", # 指定与Concentration_0对比
    label = "p.signif",
    label.y = max(df$Value)*1.1
  )
问题2:筛选Tukey结果仅保留与Concentration_0的对比

核心是从检验结果数据框中筛选出包含Concentration_0的对比行,以下两种常用方法:

方法1:基于TukeyHSD结果筛选

model <- aov(Value ~ Concentration, data = df)
tukey_res <- TukeyHSD(model)
# 转换为数据框并添加对比列
tukey_df <- as.data.frame(tukey_res$Concentration)
tukey_df$contrast <- rownames(tukey_df)
# 筛选包含Concentration_0的对比
tukey_vs_0 <- tukey_df[grepl("Concentration_0", tukey_df$contrast), ]

方法2:用emmeans直接指定对比(更高效)

无需后续筛选,直接生成仅与Concentration_0的对比结果:

library(emmeans)
model <- aov(Value ~ Concentration, data = df)
emm <- emmeans(model, ~ Concentration)
# 仅比较所有组与Concentration_0的差异,自动调整多重检验
tukey_vs_0 <- pairs(emm, ref = "Concentration_0", adjust = "tukey")
# 转换为数据框查看结果
as.data.frame(tukey_vs_0)

注意:若浓度列是因子类型,需确保Concentration_0的因子水平名称完全匹配,避免筛选出错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:22:15