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

已尝试ggpubr/rstatix未果,如何在ggplot2分面柱状图添加显著性星号注释?

给分面柱状图添加显著性星号的解决方案

问题描述

需要在facet_wrap生成的每个分面中,为DP_type两组柱子之间添加显著性星号注释,尝试过ggpubr和rstatix但未成功,求可行方法。

数据集与基础绘图代码

library(tidyverse)
library(ggpubr)
library(rstatix)

overtcovert_longeng <- structure(list(Participant = c("S1", "S1", "S1", "S1",
"S2", "S2", "S2", "S2", "S3", "S3", "S3", "S3", "S4", "S4", "S4", "S4", "S5",
"S5", "S5", "S5", "S6", "S6", "S6", "S6", "S7", "S7", "S7", "S7", "S8", "S8",
"S8", "S8", "S9", "S9", "S9", "S9", "S10", "S10", "S10", "S10", "S11", "S11",
"S11", "S11", "S12", "S12", "S12", "S12", "S13", "S13", "S13", "S13", "S14",
"S14", "S14", "S14", "S15", "S15", "S15", "S15", "S16", "S16", "S16", "S16",
"S17", "S17", "S17", "S17", "S18", "S18", "S18", "S18", "S19", "S19", "S19",
"S19", "S20", "S20", "S20", "S20", "S21", "S21", "S21", "S21", "S22", "S22",
"S22", "S22", "S23", "S23", "S23", "S23", "S24", "S24", "S24", "S24", "S25",
"S25", "S25", "S25", "S26", "S26", "S26", "S26"), Condition = structure(c(1L,
3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L,
3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L,
3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L,
3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L,
3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L,
3L, 2L, 4L), levels = c("two trucks will leave today", "we repaired two trucks", "three will leave\ntoday", "we repaired two" ), class = "factor"),
Judgment = c(2, 3, 5, 5, 4, 4, 3, 4, 5, 5, 4, 4, 3, 3, 3, 3, 2, 3, 2, 5, 1, 1,
5, 5, 4, 5, 4, 5, 3, 5, 2, 3, 4, 2, 3, 4, 3, 3, 3, 3, 5, 4, 5, 5, 3, 4, 5, 5,
3, 3, 2, 5, 5, 5, 4, 5, 4, 2, 5, 2, 4, 4, 4, 5, 1, 2, 2, 4, 4, 5, 5, 4, 5, 5,
5, 5, 4, 5, 3, 5, 3, 4, 4, 5, 3, 3, 3, 4, 3, 5, 2, 5, 4, 4, 3, 4, 3, 3, 2, 5,
4, 4, 5, 5), DP_position = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), levels = c("subject",
"object"), class = "factor"), DP_type = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), levels =
c("overt NP", "covert NP"), class = "factor")), row.names = c(NA, -104L), class
= c("tbl_df", "tbl", "data.frame"))

基础绘图代码:

overtcovert_longeng.plot <- overtcovert_longeng  %>% 
  ggplot(aes(Condition, Judgment, fill = DP_type)) +
  stat_summary(fun = "mean", geom = "bar", position = "dodge") +
  stat_summary(fun.data = mean_se, geom = "errorbar", width = .5, position = position_dodge(.9)) +
  scale_y_continuous("", expand = expansion(mult = c(0, 0.1)), limits = c(0, 5)) +
  facet_wrap(~DP_position, scales = "free_x") 

解决方案步骤

1. 按分面和条件分组做统计检验

由于是同一被试的重复测量数据,采用配对t检验,按DP_position(分面)和Condition分组,比较DP_type的两个水平:

# 统计检验并生成标注位置信息
stat_results <- overtcovert_longeng %>%
  group_by(DP_position, Condition) %>%
  pairwise_t_test(Judgment ~ DP_type, paired = TRUE, p.adjust.method = "bonferroni") %>%
  mutate(
    x = 1.5, # 两组柱子中间的x轴位置
    y = max(overtcovert_longeng$Judgment) * 1.05 # 柱子上方的y轴位置,预留空间避免重叠
  )

2. 在绘图中添加显著性标注

使用ggpubr的stat_pvalue_manual函数,指定分面变量自动匹配对应标注:

# 生成带显著性标注的图表
overtcovert_longeng.plot +
  stat_pvalue_manual(
    stat_results,
    x = "x", y = "y",
    label = "p.signif", # 用星号展示显著性
    facet.by = "DP_position",
    inherit.aes = FALSE
  ) +
  theme_bw() # 可选:优化图表主题

替代方法:使用ggsignif包

如果偏好ggsignif工具,可采用以下代码:

library(ggsignif)

overtcovert_longeng.plot +
  geom_signif(
    data = stat_results,
    aes(xmin = 1, xmax = 2, annotations = p.signif, y_position = y),
    textsize = 5,
    tip_length = 0.01,
    facet.by = "DP_position",
    inherit.aes = FALSE
  )

关键注意事项

  • 确保统计检验的分组规则与分面变量完全匹配,保证标注对应到正确分面
  • 调整y值可修改标注的垂直位置,避免与误差线或柱子重叠
  • 如需自定义星号规则,可使用rstatix的add_significance函数修改p.signif列

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 05:55:55