同时,这是由 Ilia Kats 修复的 https://github.com/thomasp85/patchwork/pull/140.
Using & theme(legend.justification = "left")
(当保持图例分离/与情节对齐时)或+ plot_layout(guides = 'collect')
(合并情节中的传说)现在可以了。
Use remotes::install_github("thomasp85/patchwork")
更新patchwork
(如果在 2020 年之前)。
详细示例:
library(ggplot2)
library(patchwork)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp, colour = mpg, size = wt)) +
guides(size = guide_legend(title = "Size - long title for the purpose of this example")) +
ggtitle('Plot 1')
p2 <- ggplot(mtcars) +
geom_boxplot(aes(gear, disp, group = gear)) +
ggtitle('Plot 2')
p3 <- ggplot(mtcars) +
geom_point(aes(hp, wt, colour = mpg)) +
ggtitle('Plot 3')
OP的情况是固定的:
(p1 | (p2 / p3)) + plot_layout(guides = 'collect')
但默认情况下仍然没有对齐:
(p1 / p3)
通过合并参考线对齐(现已修复):
(p1 / p3) + plot_layout(guides = 'collect')
按左对齐(现已修复):
(p1 / p3) & theme(legend.justification = "left")
(This would be desirable if there are no shared legends between the two plots ( so this just an bad example for the use))
Created on 2020-09-10 by the reprex package https://reprex.tidyverse.org (v0.3.0)
# Session info:
R version 4.0.1 (2020-06-06),
ggplot2 * 3.3.1 2020-05-28 [1] CRAN (R 4.0.0)
patchwork * 1.0.1.9000 2020-09-10 [1] Github (thomasp85/patchwork@82a5e03)