我使用拼凑拼凑用 ggplot2 制作的图。我还使用拼凑的plot_annotation()
命令来自动注释组装的地块——例如,自动将“A”、“B”等放置在每个地块的右上角。在大多数情况下,这很容易做到。但是当绘图在不同位置有轴标签时,很难在正确的位置获得注释。这是该问题的最小说明:
library(ggplot2)
library(patchwork)
pLeft <- ggplot(mtcars) +
geom_point(aes(mpg, disp)) +
scale_y_continuous("Left-hand panel", position = "left")
pRight <- pLeft +
scale_y_continuous("Right-hand panel", position = "right")
pLeft + pRight +
plot_layout(nrow = 1) &
plot_annotation(tag_levels = "A") &
theme(plot.tag.position = c(.935, .96))
该代码生成一个两面板图。第一个面板的标签“A”位于正确的位置。但是第二个面板的标签“B”不是:
有几种方法可以解决问题。例如,我可以plot.tag.position
为每个面板单独指定:
pLeft + theme(plot.tag.position = c(.935, .96)) +
pRight + theme(plot.tag.position = c(.785, .96)) +
plot_layout(nrow = 1) &
plot_annotation(tag_levels = "A")
或者我可以完全跳过plot_annotation()
,只annotation_custom()
用于每个面板。但这些都是不好的解决方案。问题是他们需要为每个面板分别指定标签坐标。有时我会在组装图中有很多面板,比如两列图中的N × 2 面板。在这种情况下,我只想指定两次坐标:一次用于左侧列,一次用于右侧列。这可能吗?
我查看了拼凑网站和 Stack Overflow 上的相关帖子,但我没有找到任何解决方案。