PAYATHON 2026

legendry 在仅有一个标签时无法嵌套轴 guide

数据小陈

结论:object 'decor' not found 更可能是 legendry::guide_axis_nested() 和当前 ggplot2/legendry 版本配合时的内部错误,并非 aes() 映射写错。另外,你已经用 facet_wrap(~sample, ...) 按 sample 分面,每个分面内的 x 轴只剩一个顶层 sample 标签,嵌套轴没有第二层可展开。比较稳妥的做法是把 sample 交给分面 strip,x 轴只保留 experiment;如果确实要用嵌套轴,就去掉按 sample 分面,保留 interaction(sample, experiment),再启用 guide_axis_nested()。

为什么会报错

这个报错不像列名或图层参数出错,更像是 guide 构建时缺少内部对象 decor。遇到这类错误,可以先检查版本兼容性:legendry 的 guide 实现依赖 ggplot2 的内部 guide 结构,两个包版本不匹配时可能触发这个提示。分面层级也容易被忽略。facet_wrap(~sample) 之后,每个 panel 的离散 x 轴只有一个 sample 顶层值,嵌套轴没有实际层级,某些实现会进入异常分支。所以不要同时把 sample 放进分面和嵌套 x 轴,除非每个分面内部仍有多组顶层标签。

推荐方案:用分面表达 sample 层级

把 x 轴改成 experiment,让 sample 显示在分面 strip 上。这样每张分面图里都是同一 sample 下的不同 experiment,层级关系由分面体现,不需要 guide_axis_nested()。

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

ggplot(uf_vcf_m |> filter(filters == 'none')) +
  geom_col(
    aes(x = experiment, y = count, fill = sample, alpha = experiment),
    color = 'black',
    position = position_dodge2(preserve = 'single')
  ) +
  theme_classic2() +
  scale_fill_manual(values = adjust_ltc(fernande)) +
  scale_alpha_manual(values = c(.2, .4, .6, .8, 1)) +
  geom_col_pattern(
    data = uf_vcf_m |> filter(filters != 'none'),
    aes(x = experiment, y = count, pattern = 'stripe'),
    pattern_angle = 30, fill = NA, color = 'black',
    pattern_size = .075,
    position = position_dodge2(preserve = 'single'),
    pattern_fill = 'black',
    pattern_spacing = .0075,
    pattern_color = 'black'
  ) +
  scale_pattern_manual(name = 'filters', labels = 'QUAL > 0', values = 'stripe') +
  facet_wrap(~ sample, strip.position = 'bottom', nrow = 1, scales = 'free_x') +
  guides(
    fill = guide_legend(title.position = 'top', title.hjust = .5, position = 'inside', ncol = 2, order = 1),
    alpha = guide_legend(title.position = 'top', title.hjust = .5, position = 'inside', ncol = 3, order = 2),
    pattern = guide_legend(title.position = 'top', title.hjust = .5, position = 'inside', ncol = 1, order = 3)
  ) +
  theme(
    legend.background = element_rect(fill = 'transparent'),
    legend.box = 'vertical',
    panel.grid = element_blank(),
    plot.margin = unit(c(.2, .2, .2, .2), 'cm'),
    legend.title = element_text(face = 'italic', size = 16),
    legend.justification = c('left', 'top'),
    legend.text = element_text(size = 12),
    axis.title = element_text(size = 14),
    axis.text = element_text(size = 10),
    legend.position = c(.265, .98),
    legend.box.just = 'center'
  ) +
  scale_y_continuous(expand = expansion(mult = 0, add = c(0, 100000))) +
  xlab(NULL) + ylab(NULL)

不要加 strip.text.x = element_blank(),否则 sample 的分面标签会被隐藏。scales = 'free_x' 可以保留,不过在这个方案里各分面的 experiment 水平相同,一般不是必须的。

如果必须使用 guide_axis_nested

去掉 facet_wrap(~sample),让同一个 x 轴里出现多个 sample 顶层标签。两层标签需要 interaction(sample, experiment),两个 geom_col 的 x 映射也要一致:

ggplot(uf_vcf_m |> filter(filters == 'none')) +
  geom_col(
    aes(x = interaction(sample, experiment, lex.order = TRUE),
        y = count, fill = sample, alpha = experiment),
    color = 'black',
    position = position_dodge2(preserve = 'single')
  ) +
  # 其余图层沿用原图,但 x 映射同样使用 interaction(sample, experiment, lex.order = TRUE)
  guides(
    x = legendry::guide_axis_nested(
      levels_text = list(element_text(), element_text()),
      levels_brackets = list(element_line(linewidth = .375),
                             element_line(linewidth = .125))
    ),
    fill = guide_legend(title.position = 'top', title.hjust = .5, position = 'inside', ncol = 2, order = 1),
    alpha = guide_legend(title.position = 'top', title.hjust = .5, position = 'inside', ncol = 3, order = 2),
    pattern = guide_legend(title.position = 'top', title.hjust = .5, position = 'inside', ncol = 1, order = 3)
  ) +
  theme(...)  # 其余主题设置沿用原图

如果这样仍然报 object 'decor' not found,先运行 packageVersion('ggplot2') 和 packageVersion('legendry'),更新两个包并重启 R 会话后再试。如果问题仍能复现,保留最小可复现示例和 sessionInfo();这通常属于包兼容问题,继续改图层语法一般解决不了。

注意事项

  • 不要同时把 sample 放在 facet_wrap() 和嵌套 x 轴里。你的数据每个分面只有一个 sample,嵌套轴没有实际意义。
  • interaction() 的标签顺序由因子水平决定。需要固定顺序时,用 lex.order = TRUE 或显式构造因子。
  • 两个柱层的 position 要一致,否则过滤前和过滤后的柱子会错位。
  • legend.position = c(.265, .98) 是相对坐标。分面标签或图例变化后,它可能压住图形,必要时微调。

备注:内容仅供参考。