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

求助:ggplot中stat_compare_means计算失败,无法显示Mann-Whitney检验结果

问题与解决方案

问题背景

使用compare_means对筛选后(ros_stimulated == "yes")的数据进行Wilcoxon秩和检验,得到了p=0.00067的显著差异结果,但在ggplot中调用stat_compare_means添加显著性标记时,提示计算失败,无法正常显示显著性结果。

问题原因

  1. 主ggplot的aes中包含color = donor_experiment_number,导致stat_compare_means默认按该字段分组,而非预期的tissue_origin分组,检验逻辑出错。
  2. 重复在stat_compare_means中指定data和完整aes,增加了冗余且容易引发分组冲突。

修正后的代码

library(ggpubr)
library(tidyverse)

# 原数据集(保持不变)
dummy_data <- structure(list(donor_experiment_number = c("NIB920", "NIB916", 
"NIB917", "NIB916", "NIB918", "NIB918", "NIB921", "NIB923", "NIB921", 
"NIB920", "NIB922", "NIB921", "NIB923", "NIB917", "NIB920", "NIB918", 
"NIB922", "NIB917", "NIB916", "CE027", "CE023", "CE030", "CE031", 
"CE034", "CE034", "CE030", "CE033", "CE020", "CE023", "CE025", 
"CE029", "CE024", "CE030", "CE034", "CE020", "CE031", "CE034", 
"CE024", "CE027", "CE023", "CE025", "CE033", "CE033", "CE027", 
"CE029", "CE020", "CE025", "CE029", "CE024", "CE029", "CE031"
), ros_stimulated = c("yes", "yes", "yes", "only cells", "no", 
"yes", "no", "no", "yes", "no", "no", "only cells", "only cells", 
"no", "only cells", "only cells", "only cells", "only cells", 
"no", "no", "no", "no", "yes", "only cells", "TBHP", "only cells", 
"yes", "no", "yes", "yes", "no", "only cells", "yes", "no", "only cells", 
"only cells", "yes", "no", "yes", "only cells", "only cells", 
"no", "only cells", "only cells", "TBHP", "yes", "no", "only cells", 
"yes", "yes", "no"), tissue_origin = c("healthy blood", "healthy blood", 
"healthy blood", "healthy blood", "healthy blood", "healthy blood", 
"healthy blood", "healthy blood", "healthy blood", "healthy blood", 
"healthy blood", "healthy blood", "healthy blood", "healthy blood", 
"healthy blood", "healthy blood", "healthy blood", "healthy blood", 
"healthy blood", "eschar", "eschar", "eschar", "eschar", "eschar", 
"eschar", "eschar", "eschar", "eschar", "eschar", "eschar", "eschar", 
"eschar", "eschar", "eschar", "eschar", "eschar", "eschar", "eschar", 
"eschar", "eschar", "eschar", "eschar", "eschar", "eschar", "eschar", 
"eschar", "eschar", "eschar", "eschar", "eschar", "eschar"), 
    results = c(260.11, 123.35, 132.08, 0.28, 115.46, 119.23, 
    346.64, 129.44, 328.27, 331.88, 145.5, 1.17, 0.53, 79.58, 
    0.53, 0.33, 0.46, 0.3, 88.16, 47.83, 15.99, 20.53, 13.68, 
    0.22, 25.7, 5.36, 27.94, 54.58, 11.52, 40.5, 60.16, 1.69, 
    19.63, 27.16, 0.62, 0.37, 28.75, 14.1, 39.89, 0.19, 0.79, 
    32.8, 0.52, 0.59, 68.86, 45.81, 39.13, 0.75, 11.47, 34.57, 
    12.4)), row.names = c(NA, -51L), class = "data.frame")

# 修正后的绘图代码
p <- ggplot(data = filter(dummy_data, ros_stimulated == "yes")) +
  aes(x = tissue_origin, y = results, color = donor_experiment_number) +
  geom_point(size = 3, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1)) +
  stat_summary(fun = mean, geom = "crossbar", color = "black", width = 0.3) +
  # 若theme_vsbn为自定义主题需确保已定义,否则替换为theme_pubr()
  theme_pubr() +
  ylim(0, 350) +
  labs(
    title = "",
    y = "MFI",
    x = "Simulation"
  ) +
  stat_compare_means(
    aes(group = tissue_origin), # 明确指定检验分组,覆盖color的分组影响
    method = "wilcox.test",
    paired = FALSE,
    label = "p.signif",
    ref.group = "eschar"
  )

plot(p)

关键修正点

  • 指定检验分组:通过aes(group = tissue_origin)明确告诉stat_compare_means按组织来源进行分组检验,避免主图中color字段的分组干扰。
  • 继承主图数据:移除重复的data参数,直接继承主图中已筛选的数据,减少冗余。
  • 主题兼容:如果theme_vsbn是未定义的自定义主题,替换为ggpubr自带的theme_pubr()保证绘图正常。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 04:11:11