在这个拼凑的小插曲中解释了如何组合多个 ggplots。我遇到的一个困难是收集图例并在它们的标题字符数非常不同时正确对齐/证明它们。
下面是一个示例 - 我希望“mpg”图例也左对齐/对齐,而不是在“大小”图例下方居中。有什么建议么?请注意,添加theme(legend.justification = "left")
并不能解决问题。
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')
(p1 | (p2 / p3)) + plot_layout(guides = 'collect')
由reprex 包(v0.3.0)于 2019 年 12 月 16 日创建