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

分组箱线图的Wilcox检验与P值标注报错求助

分组箱线图组间统计检验标注问题解决

需求与报错

需求:计算各Cell_type(20_p1、4_p1等)在不同ROI(Reg1与Reg2、Reg1与Reg3、Reg2与Reg3)间的统计量,并在分组箱线图上标注显著P值。

运行代码时报错:

Error in mutate():
! Problem while computing data = map(.data$data, .f, ...).
Caused by error in pull():
! Can't extract columns that don't exist.
✖ Column ROI doesn't exist.

报错原因

错误出在统计检验的分组逻辑:代码中同时按Cell_type和ROI分组,导致每个子数据框仅包含单一ROI的样本,后续pairwise_wilcox_test无法找到用于组间比较的ROI列。正确的分组应该仅按Cell_type,确保每个分组内包含所有ROI的样本,才能进行组间差异检验。

修正后的完整代码

# 加载依赖包
library(ggplot2)
library(rstatix)
library(dplyr)
library(ggsignif)

# 用户提供的数据框
Data <- structure(list(Value = c(4.621072089, 12.59398496, 0, 0, 10.16507385, 23.37278107, 8.474576271, 0, 7.397959184, 11.02803738, 1.694915254, 4.347826087, 9.6024006, 27.98053528, 8.450704225, 0, 8.78112713, 19.33471933, 0, 0, 20.44534413, 27.79809802, 20.51282051, 50, 28.00150546, 45.01811594, 26.47058824, 50, 0, 0.113765643, 0.266666667, 0, 0.520833333, 0.393700787, 0.595238095, 0, 0.875912409, 0.965517241, 1.731601732, 0), Cell_type = c("20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1", "20_p1", "4_p1", "8_p1", "68_p1"), Area = c("GC1", "GC1", "GC1", "GC1", "GC2", "GC2", "GC2", "GC2", "GC3", "GC3", "GC3", "GC3", "GC4", "GC4", "GC4", "GC4", "GC5", "GC5", "GC5", "GC5", "GC_sc1", "GC_sc1", "GC_sc1", "GC_sc1", "GC_sc2", "GC_sc2", "GC_sc2", "GC_sc2", "Foll1", "Foll1", "Foll1", "Foll1", "Foll2", "Foll2", "Foll2", "Foll2", "Foll3", "Foll3", "Foll3", "Foll3"), ROI = c("Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg1", "Reg2", "Reg2", "Reg2", "Reg2", "Reg2", "Reg2", "Reg2", "Reg2", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3", "Reg3"), Site = c("Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal", "Normal")), class = "data.frame", row.names = c(NA, -40L))

# 绘制基础分组箱线图
p <- ggplot(Data, aes(x=ROI, y=Value, fill=ROI)) +
  geom_boxplot() +
  geom_point() +
  theme_classic() +
  ylim(0, 100) +
  facet_grid(cols=vars(Cell_type), rows=vars(Site)) +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))  # 替换rotate_x_text,兼容更多环境

# 计算组间差异(仅按Cell_type分组)
df_wilcox <- Data %>%
  group_by(Cell_type) %>%
  pairwise_wilcox_test(Value ~ ROI) %>%
  add_y_position(step.increase = 0.02)

# 在图上标注显著P值
p + geom_signif(
  data = df_wilcox,
  aes(xmin = group1, xmax = group2, annotations = p.signif, y_position = y.position),
  manual = TRUE
)

关键修正点

  • 移除group_by中的ROI参数,确保每个Cell_type分组内包含所有ROI的样本,让检验函数能正常进行组间比较
  • 将rotate_x_text替换为标准ggplot2的theme设置,避免依赖第三方扩展包的特定函数
  • 使用geom_signif将检验得到的显著性标记添加到对应子图的指定位置

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 09:45:28