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

如何在ggplot中调整Mann-Whitney统计注释至Day6组上方

问题描述

用户使用如下数据集:

structure(list(sample = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 
12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54), day = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L), levels = c("2", "6"), class = "factor"), 
    treatment = c("a", "a", "a", "a", "a", "a", "a", "b", "b", 
    "b", "b", "b", "b", "c", "c", "c", "c", "c", "c", "d", "d", 
    "d", "d", "d", "d", "d", "d", "a", "a", "a", "a", "a", "a", 
    "a", "b", "b", "b", "b", "b", "b", "c", "c", "c", "c", "c", 
    "c", "d", "d", "d", "d", "d", "d", "d", "d"), group = c("count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count", "count", "count", "count", 
    "count", "count", "count", "count"), result = c(1.94000836127381, 
    2.07418521661429, 0.812685661255674, 0.199532997122114, 0.720956738045624, 
    2.25080298569014, 1.72685286125659, 1.1066027850052, 4.1487948134003, 
    5.06163333946851, 8.45581635201957, 1.39519183814535, 5.22744057847467, 
    77.578763434025, 81.5688787451947, 57.7998831807876, 72.5246292216229, 
    53.7941684202605, 18.1902377363129, 7.2040245328528, 18.7399963681316, 
    12.0408266827075, 16.9381875648501, 4.94300230430152, 7.7656112238281, 
    3.62337602915357, 9.29131381820146, 17.3474341955159, 0.654156425021601, 
    18.2284156894217, 8.1096329723588, 2.59461805212543, 11.635214608248, 
    10.338591963394, 17.096512619713, 2.29518753169706, 13.6312931040208, 
    2.40586814654832, 18.2260582559852, 0.813121453291961, 86.1680580200406, 
    86.1245441941217, 75.7365169486812, 51.2171310942499, 62.4301976210013, 
    38.1836807429795, 31.2339903053221, 7.92988025761869, 8.27444313173916, 
    30.062084984576, 44.8857368621187, 15.0021164008775, 23.395907046137, 
    45.0063042518017)), row.names = c(NA, -54L), class = c("tbl_df", 
"tbl", "data.frame"))

编写了如下ggplot代码添加Mann-Whitney检验注释:

stat_test <- list(c("a","b"), c("a","c"))

ggplot(data = filter(dummy_data))+
  aes(x = treatment, y = result, color = day)+
    geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1))+
  stat_summary(fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = day),
        position=position_dodge(0.5))+
  stat_compare_means(data = filter(dummy_data, day == "6"),
                     aes(x = treatment, y = result),
                     comparisons = stat_test,
                     method = "wilcox.test", 
                     paired = FALSE,
                     label = "p.signif")+
  theme_classic()+
  theme(legend.position="bottom")+
  labs(
    y = "Dummy data", 
    x = "Treatment" 
  )

ggsave("Dummy.png")

问题:生成的图表中,针对Day6的统计注释线默认位于Day2和Day6组的中间位置,需要将注释线调整到Day6组的上方。

解决方案

问题根源是stat_compare_means没有匹配图表中position_dodge的偏移量,导致注释线默认对齐x轴分组的中心,而非Day6的分组位置。可以通过两个关键修改解决:

  • 给stat_compare_means添加position = position_dodge(0.5),匹配之前geom_point和stat_summary使用的dodge.width,让注释线对齐Day6的分组;
  • 通过y.position参数手动设置注释线的y轴高度,确保它位于Day6数据点的上方,避免与数据重叠。

修改后的完整代码:

stat_test <- list(c("a","b"), c("a","c"))

# 动态计算Day6数据的最大y值,用来设置注释线的位置(也可以手动指定固定值)
max_y_day6 <- dummy_data %>% 
  filter(day == "6") %>% 
  pull(result) %>% 
  max()

ggplot(data = dummy_data)+
  aes(x = treatment, y = result, color = day)+
  geom_point(shape = 1, position = position_jitterdodge(dodge.width = 0.5, jitter.width = 0.1))+
  stat_summary(fun = mean, geom = "crossbar", width = 0.3, mapping = aes(group = day),
               position=position_dodge(0.5))+
  stat_compare_means(data = filter(dummy_data, day == "6"),
                     aes(x = treatment, y = result),
                     comparisons = stat_test,
                     method = "wilcox.test", 
                     paired = FALSE,
                     label = "p.signif",
                     # 匹配dodge偏移,对齐Day6组
                     position = position_dodge(0.5),
                     # 设置注释线在Day6数据上方,这里用最大值加5作为偏移
                     y.position = max_y_day6 + 5)+
  theme_classic()+
  theme(legend.position="bottom")+
  labs(
    y = "Dummy data", 
    x = "Treatment" 
  )

ggsave("Dummy.png")

如果不想动态计算最大y值,也可以直接手动指定y.position为固定数值(比如y.position = 100),根据你的数据范围调整即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 16:10:26