我想将图例(所有绘图共有)放置在拼凑布局的空白区域中。从我在网上可以找到的内容来看,我无法使用手动定位图例legend.position
如果我也使用guides="collect"
(但可以使用左、右等)。
我尝试过使用l <- get.legend
进而+ inset_element(l, 0.6, 0.6, 1, 1)
但它不明白l
。我也尝试过混入+ inset_element(gridExtra::tableGrob(l))
没有运气。
我的目标是将图例放在空白处。我的实际拼凑图更复杂,但有两个空白空间,我希望图例位于其中。
MWE
library(patchwork)
library(ggplot2)
p1 <- ggplot(mtcars) +
geom_point(aes(mpg, disp, color = mpg)) +
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')
design <- "
1111
223#
"
p1 + p2 + p3 + plot_layout(guides = 'collect') + plot_layout(design=design, guides = "collect") &
theme(legend.position = 'right',
legend.direction = 'vertical')
改变你的design
对象包含第四个元素并使用guide_area()
放置指南。
library(patchwork)
library(ggplot2)
design <- "
1111
2234
"
p1 + p2 + p3 + guide_area() + plot_layout(design=design, guides = "collect")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)