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

R语言中geom_pwc等函数的NS显著性括号隐藏及对比设置问题

解决ggpubr中显著性标记的两个问题:移除NS括号与指定对比组

问题背景

  • 数据集包含三个变量:willingness(Y轴,0-100连续值)、gender(X轴,含Men/Women/Non-binary/general四类,需排除general组)、Risk(分面变量,含0.1%/2%/10%三个水平)
  • 遇到两个核心问题:
    1. stat_compare_means中设置hide.ns=TRUE仅移除NS标签,残留空括号
    2. geom_pwc/stat_pvalue_manual设置hide.ns=TRUE后所有显著性标记消失,且未指定目标对比组

解决方案

方案1:修复stat_compare_means的NS括号问题

通过自定义标签函数,直接将NS对应的标记(包括括号)完全隐藏,替代默认的hide.ns逻辑:

# 定义自定义标签函数:NS时返回空字符串,否则保留显著性标记
custom_label <- function(p_signif) {
  ifelse(p_signif == "ns", "", p_signif)
}

# 定义目标对比组
my_comparisons <- list(c("Men", "Women"), c("Men", "Non-binary"), c("Women", "Non-binary"))

# 绘图代码(先排除general组)
ggboxplot(combined_data[combined_data$gender != "general", ],
          x = "gender", y = "willingness",
          color = "black", fill = "gender",
          palette = c("coral4","#BBD1EA","#FFBB99"), # 移除general对应的颜色
          facet.by = "Risk", short.panel.labs = FALSE, outlier.shape = NA) +
  geom_point(aes(x=gender,y=willingness,fill=gender),
             position=position_jitterdodge(), alpha=0.3, size=1, shape=20) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        legend.position = "none",
        axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
  stat_compare_means(comparisons = my_comparisons,
                     label = function(x) custom_label(x$p.signif), # 应用自定义函数
                     hide.ns = TRUE) # 双重保障,可省略

方案2:修复geom_pwc/stat_pvalue_manual的问题

核心是先排除general组,并明确指定对比组,避免默认全组对比导致的统计异常:

方法A:使用geom_pwc指定对比组

# 过滤掉general组
filtered_data <- combined_data[combined_data$gender != "general", ]

ggboxplot(filtered_data, x = "gender", y = "willingness",
          color = "black", fill = "gender",
          palette = c("coral4","#BBD1EA","#FFBB99"),
          facet.by = "Risk", short.panel.labs = FALSE, outlier.shape = NA) +
  geom_point(aes(x=gender,y=willingness,fill=gender),
             position=position_jitterdodge(), alpha=0.3, size=1, shape=20) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        legend.position = "none",
        axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
  geom_pwc(comparisons = my_comparisons, # 指定目标对比组
           label = "p.signif", hide.ns = TRUE,
           group.by = "Risk") # 按分面变量分组统计

方法B:使用stat_pvalue_manual指定对比组

# 过滤数据并指定对比组做统计检验
filtered_data <- combined_data[combined_data$gender != "general", ]
stat.test <- compare_means(willingness ~ gender, data = filtered_data,
                           group.by = "Risk",
                           comparisons = my_comparisons) # 必须指定对比组

ggboxplot(filtered_data, x = "gender", y = "willingness",
          color = "black", fill = "gender",
          palette = c("coral4","#BBD1EA","#FFBB99"),
          facet.by = "Risk", short.panel.labs = FALSE, outlier.shape = NA) +
  geom_point(aes(x=gender,y=willingness,fill=gender),
             position=position_jitterdodge(), alpha=0.3, size=1, shape=20) +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust=1),
        legend.position = "none",
        axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.title.x = element_blank()) +
  stat_pvalue_manual(stat.test, label = "p.signif", hide.ns = TRUE)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 08:38:13