【问题标题】:Add legend between patchwork main title and plot titles在拼凑的主标题和情节标题之间添加图例
【发布时间】:2021-01-15 09:36:16
【问题描述】:

是否可以在“主”标题下方和情节标题上方使用拼凑而成的图例?

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(disp, wt, color = factor(gear))) + 
    geom_point() + 
    labs(title = "first plot title")
p2 <- ggplot(mtcars, aes(drat, qsec, color = factor(gear))) + 
    geom_point() + 
    labs(title = "second plot title")

(p1+p2) + 
    plot_layout(guides = "collect") + 
    plot_annotation(title = "main title") & 
    theme(legend.position = "top")

【问题讨论】:

    标签: r ggplot2 patchwork


    【解决方案1】:

    有点hack,提取图例然后将其绘制在顶部:

    # get the legend
    l <- cowplot::get_legend(p1 + theme(legend.position = "top"))
    
    (cowplot::plot_grid(l) / (p1 + p2)) + 
      plot_layout(heights = unit(c(1, 5), "cm")) +
      plot_annotation(title = "main title") &
      theme(legend.position = "none")
    

    【讨论】:

    • 希望可能有一些“直截了当”的方法而不需要单独绘制,但是任何解决方案都比没有好,tnx。
    猜你喜欢
    • 2020-11-27
    • 1970-01-01
    • 2018-10-14
    • 2021-11-17
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    相关资源
    最近更新 更多