【问题标题】:How to incorporate legend as plot of plot size when arranging multiple plots using patchwork?使用拼凑排列多个地块时,如何将图例合并为地块大小的图?
【发布时间】:2021-11-15 22:05:55
【问题描述】:

虚拟代码:

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

我不是在寻找一个组合的传说,而是看起来像这样的东西:

传说是情节的“考虑”部分。我尝试使用theme()plot_spacer() 添加边距,但这并不是我想要的。这是我得到的:

【问题讨论】:

    标签: r ggplot2 patchwork


    【解决方案1】:

    对于您的示例代码,一种选择是像这样使用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 创建

    【讨论】:

      猜你喜欢
      • 2021-09-23
      • 1970-01-01
      • 2019-09-30
      • 2020-04-09
      • 2019-05-02
      • 2019-08-31
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多