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

使用geom_signif为3组对比图表添加第二层显著性标记问题

实现代码

首先补充原代码缺失的position_dodge对象定义,再新增一层geom_signif图层实现C4电极三个子分组的第二层显著性对比,可直接运行以下完整代码:

library(ggplot2)
library(ggthemes)
library(ggsignif)

# 原代码缺失的位置偏移参数定义
pd <- position_dodge(0.7)

tgc4 <- structure(list(Condition = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L), .Label = c("CEN", "IPS", 
"CTL"), class = "factor"), Electrode = structure(c(1L, 2L, 3L, 
4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("C4", 
"CP2", "CP6", "P4", "P8"), class = "factor"), N = c(24, 24, 24, 
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24), measurement = c(0.475900166666667, 
0.733097333333333, 1.089481625, 2.01497575, 2.16174375, 0.434101083333333, 
0.701428416666667, 1.01162383333333, 1.92838604166667, 2.05454458333333, 
0.524649375, 0.7666545, 1.10670179166667, 2.021725125, 2.14652179166667
), sd = c(0.690171324253539, 1.06910371259459, 1.68860310942122, 
3.4014857218051, 4.43699113062584, 0.683641055049957, 1.01566311510833, 
1.65013550853766, 3.32008309477149, 4.36699670813656, 0.721378974999592, 
1.08818214299657, 1.7267893116772, 3.42645198458195, 4.41596190187272
), se = c(0.140880631626844, 0.218229881497655, 0.344684666346589, 
0.694325365482085, 0.905697021940661, 0.139547646007528, 0.207321365215089, 
0.336832500197108, 0.677709123819216, 0.891409470279034, 0.147250866660079, 
0.222124249795826, 0.352479392241743, 0.699421582531046, 0.901404448596543
), ci = c(0.291433790775513, 0.451442905180972, 0.713034558232622, 
1.4363214514115, 1.87357703717142, 0.288676299929537, 0.428876919954661, 
0.696791114969224, 1.40194813663905, 1.84402098469234, 0.304611625957172, 
0.459499019798758, 0.729159177276763, 1.44686377959418, 1.86469717265482
)), row.names = c(NA, -15L), class = "data.frame")

p4 <- ggplot(tgc4, aes(fill=Condition, x = Electrode, y = measurement)) + 
  geom_errorbar(aes(ymin=measurement-se, ymax=measurement+se), width=.7,position=pd) + 
  scale_fill_manual(name = "Condition", 
                    values=c("red", "blue", "black")) +
  geom_bar(stat = "identity", width = 0.7, color = "black", position = pd)  + 
  theme_bw() + 
  ylim(0,3.5) + 
  theme(
    axis.text.x = element_text(size = 16,face="bold"),
    axis.text.y = element_text(size = 16, face = "bold"),
    axis.title.y = element_text(vjust= 1.8, size = 20),
    axis.title.x = element_text(vjust= -0.5, size = 18),
    axis.title = element_text(face = "bold")) + 
  xlab("Electrode") + 
  ylab("Change in alpha activity (\U00B5V)") +
  theme(aspect.ratio = 5/5) + 
  theme(legend.text=element_text(size=14), legend.title=element_text(size=16))

# 叠加两层显著性标记
p4 + 
  # 原有第一层全电极对比标记
  geom_signif(
    y_position = c(0.8, 1.1, 1.6, 2.85, 3.2), 
    xmin = c(1, 2, 3, 4, 5), 
    xmax = c(1.4, 2.4, 3.4, 4.4,5.4),
    annotation = c("***", "*", "*", "ns", "ns"), 
    tip_length = 0.005, textsize = 7, size = 1
  ) +
  # 新增第二层C4电极子分组对比标记
  geom_signif(
    y_position = c(0.95, 1.15),
    xmin = c(1, 0.77),
    xmax = c(1.23, 1.23),
    annotation = c("ns", "*"), # 可根据实际统计结果修改显著性符号
    tip_length = 0.005, textsize = 7, size =1
  )

效果展示

运行代码生成的效果和要求的示例一致:
示例效果图

调整说明

如果出现标记重叠,可自行调高第二层geom_signif中y_position的数值即可。


内容的提问来源于stack exchange,提问作者Reuben Newton Addison

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 17:27:03