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

无法在小提琴图中展示Games-Howell检验的显著性差异求修复

问题:小提琴图无法标注Games-Howell检验的显著性差异

我用R绘制小提琴图,同时完成了Games-Howell ANOVA事后检验,结果显示A、B、C、D亚基因组间存在显著性差异,但没法在图中展示这些差异,只需要修复代码里「标注显著性差异」的部分。

事后检验显著性结果

需在图中标注A与B、A与C、A与D之间的***显著性:

Comparison between treatments means
difference pvalue signif. LCL UCL
A - B -0.037101857 0.0000 *** -0.046188341 -0.02801537
A - C -0.028211022 0.0000 *** -0.037394783 -0.01902726
A - D -0.030163234 0.0000 *** -0.039466699 -0.02085977
B - C 0.008890835 0.1059 -0.001186665 0.01896834
B - D 0.006938623 0.2979 -0.003248084 0.01712533
C - D -0.001952212 0.9618 -0.012225785 0.00832136

遇到的警告信息

调整ymax限制时出现以下警告:

Warning messages:
1: Removed 1 row containing missing values or values outside the scale range
(geom_text()).
2: Removed 1 row containing missing values or values outside the scale range
(geom_text()).
3: Removed 1 row containing missing values or values outside the scale range
(geom_text()).

原代码

library(dplyr)
library(agricolae)
library(stringr)
library(ggplot2)
library(Hmisc)

# 读取数据
df <- read.table('ABCD-meth-r1.tsv', header = TRUE, sep = "\t")
df$subgenome <- factor(df$subgenome)

# 创建x轴标签
my_xlab <- paste(levels(df$subgenome), "\n ", table(df$subgenome), sep="")

# 执行ANOVA检验
anova_result <- aov(value ~ subgenome, data = df)
summary(anova_result)

# 初始化sig_diff数据框,避免无显著结果时的作用域问题
sig_diff <- data.frame(group1 = character(), group2 = character(), pvalue = numeric())

# 若ANOVA显著则执行Games-Howell检验
if (summary(anova_result)[[1]][["Pr(>F)"]][1] < 0.05) {
    print("检测到显著差异,执行Games-Howell检验")
    games_howell_result <- HSD.test(anova_result, "subgenome", group=FALSE, console=TRUE)

# 提取有显著差异的配对
if (nrow(games_howell_result$comparison) > 0) {
    sig_diff <- games_howell_result$comparison[games_howell_result$comparison$pvalue < 0.05,]
       }
    }

# 计算标注用的ymax
ymax <- max(df$value, na.rm = TRUE) * 1.1
print(paste("标注用的ymax:", ymax))

# 绘图代码
p <- ggplot(df, aes(x = subgenome, y = value, fill = subgenome)) +
      geom_violin() + 
      geom_boxplot(width=0.08, fill="white")+
      stat_summary(fun=mean, geom="point", shape=20, size=1, color="darkgreen") +
      scale_x_discrete(labels=my_xlab)+
      xlab("") +
      ylab("CDS甲基化水平") +
      theme_bw() +
      ylim(0, ymax)

# 标注显著性差异
if (nrow(sig_diff) > 0) {
    for(i in 1:nrow(sig_diff)) {
        sub1_index <- which(levels(df$subgenome) == sig_diff$group1[i])
        sub2_index <- which(levels(df$subgenome) == sig_diff$group2[i])
        mid_x <- mean(c(sub1_index, sub2_index))
        print(paste("标注配对:", sig_diff$group1[i], "和", sig_diff$group2[i], ",位置:", mid_x))
        p <- p + annotate("text", label = "*", x = mid_x, y = ymax * 0.95, size = 5, vjust = 0)
        p <- p + annotate("segment", x = sub1_index, xend = sub2_index, y = ymax, yend = ymax, color = "red", linewidth = 1)
    }
}

# 输出图
print(p)

初始输出图

初始图为包含箱线和均值点的小提琴图,无任何显著性标注。

我做的修改

调整了绘图部分的坐标设置,并修改了sig_diff的初始化逻辑:

# 绘图代码
p <- ggplot(df, aes(x = subgenome, y = value, fill = subgenome)) +
  geom_violin() + 
  geom_boxplot(width=0.08, fill="white")+
  stat_summary(fun=mean, geom="point", shape=20, size=1, color="darkgreen") +
  scale_x_discrete(labels=my_xlab)+
  xlab("") +
  ylab("CDS甲基化水平") +
  theme_bw()+
  coord_cartesian(ylim = c(0, ymax), clip = "off")

# 修改sig_diff部分
# 初始化sig_diff数据框,避免无显著结果时的作用域问题
sig_diff <- data.frame(group1 = character(), group2 = character(), pvalue = numeric())
sig_diff$group2 <- sub(".+ \\- (.+)", "\\1", row.names(sig_diff))
sig_diff$group1 <- sub("(.+) \\- .+", "\\1", row.names(sig_diff))

修改后的输出图

修改后的图仍未显示任何显著性标注。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 20:25:54