多面图中的 R ggplot 多级 x 轴标签

R ggplot multilevel x-axis labels in faceted plots

我尝试使用两次调用 annotate() 的技巧来获得 2 级嵌套刻度标签 (here),但我想结合 facet_wrap() 执行此操作

df <- tribble(
  ~action, ~gend, ~status, ~cellMean,
  "P",     "M",   "A",     1,
  "P",     "M",   "B",     2,
  "P",     "F",   "A",     1.4,
  "P",     "F",   "B",     2.6,
  "V",     "M",   "A",     2,
  "V",     "M",   "B",     3,
  "V",     "F",   "A",     2.2,
  "V",     "F",   "B",     3.8
) %>%
  mutate(action=factor(action,levels=c("P","V")),
         gend  =factor(gend,  levels=c("F","M")),
         status=factor(status,levels=c("A","B")))

df

# action obscured/overlaid
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  theme_light()

# action used as facet
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  theme_light() +
  facet_wrap(~action)

# annotate hack on unfaceted
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  annotate(geom = "text", x = 1:4, y = .7, label = rep(c("A","B"), times=2) ) +
  annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males")) +
  coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off") +
  theme_light() +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank() )

# annotate hack on FACETED fails saying it wants 8 labels
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  annotate(geom = "text", x = 1:4, y = .7, label = rep(c("A","B"), times=2) ) +
  annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males")) +
  coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off") +
  theme_light() +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank() ) +
  facet_wrap(~action)

产生:“错误:美学必须是长度 1 或与数据相同 (8):标签”,这似乎需要 8 个标签跨越两个面,每个面都有4 个标签。

但是,尝试供应 8 只然后需求 16。

# annotate hack on FACETED with length 8 vectors fails saying 16
df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  annotate(geom = "text", x = rep(1:4, times=2), y = .7, label = rep(c("A","B"), times=4) ) +
  annotate(geom = "text", x = c(1.5,3.5), y = .6, label = c("Females","Males")) +
  coord_cartesian(ylim = c(.8, 4), xlim=c(.5,4.5), expand = FALSE, clip = "off") +
  theme_light() +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank() ) +
  facet_wrap(~action)

产生:“错误:美学必须是长度1或与数据相同(16):标签

有没有办法在 facet_wrap() 中使用 annotate() 技巧?

我想知道我是否需要制作 2 个图并将它们并排放置以模拟小平面。

我也常常难以 annotate() 很好地处理 facets。我无法让它工作,但您可以改用 geom_text()。需要对剪辑、x-label 格式和主题设置进行一些修改才能使其正常工作。我使用 vjust = 3, y = -Inf 而不是 hard-coding 和 y-position,这样人们就可以更轻松地将其推广到他们的情节中。

df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  geom_text(data = data.frame(z = logical(2)),
            aes(x = rep(c(1.5, 3.5), 2), y = -Inf,
                label = rep(c("Females", "Males"), 2)),
            inherit.aes = FALSE, vjust = 3) +
  theme_light() +
  coord_cartesian(clip = "off") +
  facet_wrap(~action) +
  scale_x_discrete(labels = ~ substr(.x, 1, nchar(.x) - 2)) +
  theme(axis.title.x.bottom = element_text(margin = margin(t = 20)))

另一种选择是使用 ggh4x::guide_axis_nested() 显示 interaction()ed 因素。您需要重新编码您的 M/F 级别以读取 Male/Female 以获得与上述类似的结果。

df %>%
  ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
                             color=status, shape=gend)) +
  geom_point(size=3.5) +
  theme_light() +
  facet_wrap(~action) +
  guides(x = ggh4x::guide_axis_nested(delim = ".", extend = -1))

reprex package (v2.0.1)

于 2022-03-30 创建

免责声明:我写了 ggh4x。