使用拼凑排列多个地块时,如何将图例合并为地块大小图?

How to incorporate legend as plot of plot size when arranging multiple plots using patchwork?

虚拟代码:

library(ggplot2)
library(patchwork)

plot1 <- mpg %>% ggplot()
plot2 <- mpg %>% ggplot()
plot3 <- mpg %>% ggplot(aes(cyl, displ)) +
  geom_point(aes(colour = manufacturer)) + 
  guides(colour=guide_legend(ncol=4))

(plot1 + plot2) / plot3

我不是在寻找组合图例,而是看起来像这样的东西:

图例是 'considered' 情节的一部分。我试过用 theme()plot_spacer() 添加边距,但这并不是我想要的。这是我得到的:

对于您的示例代码,一种选择是像这样使用 guide_area()

library(ggplot2)
library(patchwork)
library(magrittr)

plot1 <- mpg %>% ggplot()
plot2 <- mpg %>% ggplot()
plot3 <- mpg %>% ggplot(aes(cyl, displ)) +
  geom_point(aes(colour = manufacturer)) + 
  guides(colour = guide_legend(ncol=3))

plot1 + plot2 + plot3 + guide_area() + 
  plot_layout(guides = 'collect')

另一种选择是通过 cowplot::get_legend 提取指南并将其添加到拼凑作品中,如下所示:

(plot1 + plot2) / (plot3 + guides(color = "none") + cowplot::get_legend(plot3))

reprex package (v2.0.1)

于 2021-09-22 创建