PAYATHON 2026

facet_wrap 与 ggsignif 分面显著性标注失效

数据小陈

结论:可以。facet_wrap() 本身一般没问题,麻烦通常出在把所有比较都放进同一个 geom_signif() 层。scales = 'free_x' 会让每个 sample 分面有独立的 x 轴水平;一个 geom_signif() 里的 comparisons 是全局字符串,在不对应的分面里找不到相应 x 水平,结果只有第一个能匹配的组被画出来。跨分面的比较,geom_signif() 也没法自动画括号。

推荐做法:按 sample 拆成多个 geom_signif 层

保留原来的 x 轴写法时,每个 sample 分面只放该 sample 的比较。每个 geom_signif() 都要用过滤后的数据,并设置 inherit.aes = FALSE,这样它就不会继承 fill、pattern 等映射。

library(dplyr)
library(ggplot2)
library(ggpubr)
library(ggpattern)
library(legendry)

base_df <- test_ggsig %>%
  mutate(xgrp = interaction(experiment, subsample, sample))

p <- ggplot(
  base_df %>% filter(filters == 'none'),
  aes(x = xgrp, y = count, fill = variant_type)
) +
  geom_col(color = 'black', position = 'dodge2') +
  theme_classic2() +
  geom_col_pattern(
    data = base_df %>% filter(filters != 'none'),
    aes(x = xgrp, y = count, pattern = 'stripe'),
    pattern_angle = 30, fill = NA, color = 'black',
    pattern_size = .075, position = 'dodge2',
    pattern_fill = 'black', pattern_spacing = .0075,
    pattern_color = 'black'
  ) +
  scale_pattern_manual(name = 'filters', labels = 'QUAL > 0', values = 'stripe') +
  guides(
    x = legendry::guide_axis_nested(
      levels_text = list(
        element_text(angle = 30, hjust = .5, face = 'bold'),
        element_text(),
        element_text()
      ),
      levels_brackets = list(
        element_line(linewidth = .375),
        element_line(linewidth = .125)
      ),
      drop_zero = FALSE
    ),
    fill = guide_legend(
      title = 'variant type', byrow = TRUE, title.position = 'top',
      title.hjust = .5, position = 'inside', ncol = 3, order = 1
    ),
    pattern = guide_legend(
      title.position = 'top', title.hjust = .5,
      position = 'inside', ncol = 1, order = 3
    )
  ) +
  scale_y_continuous(expand = expansion(mult = 0, add = c(0, 300000))) +
  xlab(NULL) + ylab(NULL) +
  facet_wrap(~sample, strip.position = 'bottom', nrow = 1, scales = 'free_x') +
  theme(panel.spacing = unit(0.6, 'cm'))

p +
  geom_signif(
    data = base_df %>% filter(sample == 'irradiated', filters == 'none'),
    aes(x = xgrp, y = count),
    comparisons = list(c('prt.no.irradiated', '24h.sub.irradiated')),
    map_signif_level = TRUE,
    test = 'wilcox.test',
    step_increase = .02, tip_length = .005,
    size = .4, textsize = 4, vjust = .6,
    inherit.aes = FALSE
  ) +
  geom_signif(
    data = base_df %>% filter(sample == 'clone4', filters == 'none'),
    aes(x = xgrp, y = count),
    comparisons = list(
      c('prt.no.clone4', '12w.sub.clone4'),
      c('4w.no.clone4', '12w.sub.clone4'),
      c('8w.no.clone4', '12w.sub.clone4')
    ),
    map_signif_level = TRUE,
    test = 'wilcox.test',
    step_increase = .02, tip_length = .005,
    size = .4, textsize = 4, vjust = .6,
    inherit.aes = FALSE
  )

如果还是只画第一个分面,可以把 sample 从 x 轴上去掉,改用 xgrp = interaction(experiment, subsample),sample 只留给 facet_wrap()。这样每个分面的 x 水平更干净,comparisons 里也不用再带 sample 后缀,例如 c('prt.no', '24h.sub')、c('prt.no', '12w.sub')。这时 guide_axis_nested() 的 levels_text 和 levels_brackets 要同步改成两层。

annotation_df 方式

如果确实想手动提供注释,可以用 manual = TRUE 的 geom_signif()。标签可以按 Wilcox 检验 p 值自动映射,常用规则是:p < 0.001 为 ***,p < 0.01 为 **,p < 0.05 为 *,否则为 ns。

sig_label <- function(p) {
  if (is.na(p)) return('ns')
  if (p < 0.001) return('***')
  if (p < 0.01) return('**')
  if (p < 0.05) return('*')
  'ns'
}

先按每个分面分别计算 p 值,再组装 annotation_df。下面只是结构示例,xmin、xmax 和 y_position 要按分面 x 轴位置和柱高调整:

ann <- data.frame(
  sample = c('irradiated', 'clone4'),
  xmin = c('prt.no.irradiated', 'prt.no.clone4'),
  xmax = c('24h.sub.irradiated', '12w.sub.clone4'),
  y_position = c(7500000, 8500000),
  annotations = c('***', '**')
)

p +
  geom_signif(
    data = ann,
    aes(xmin = xmin, xmax = xmax,
        y_position = y_position, annotations = annotations),
    manual = TRUE,
    inherit.aes = FALSE,
    tip_length = .005,
    size = .4, textsize = 4, vjust = .6
  )

注意:不同 ggsignif 版本对 manual = TRUE 下离散 x 的 xmin/xmax 支持略有差异。如果直接传字符水平报错,就改成该分面内的数值位置,比如把 xgrp 转成 as.numeric(factor(xgrp)) 后再取对应位置。annotation_df 必须包含 sample 列,或包含与分面变量同名的列,否则注释不会落到正确分面。

注意事项

  • comparisons 里的字符串必须和 interaction() 生成的标签完全一致,默认分隔符是 .,顺序由 factor levels 决定。
  • 每个 geom_signif() 层的数据最好只包含该分面要比较的行,并且和主图使用同一套 filters == 'none' 数据,免得 p 值基于不同数据。
  • 原代码没有显式按 variant_type 分组做 Wilcox 检验;geom_signif() 默认比较每个 x 组里的 count 汇总。如果希望在每个 variant_type 内部分别比较,需要额外加 group = variant_type,或者分别算好再传入 annotation。
  • 分面间距用 theme(panel.spacing = unit(...)) 调整,strip.position = 'bottom' 只控制分面标签位置。
  • 跨分面比较不能靠单个 geom_signif() 自动完成;要么把比较限制在同一 sample 分面内,要么拆图后用 patchwork 拼接,并在每张子图内单独加显著性标注。

备注:内容仅供参考。