我一直在尝试收集循环创建的图例。但是,这无法产生预期的结果,因为没有收集图例。我试过
(res[[1]] / res[[2]]) + plot_layout(guides = "collect") + theme(legend.position = "bottom")
whereres[[1]]
和res[[2]]
是在循环中创建的列表元素。
任何建议将不胜感激。
删除+ theme(legend.position = "bottom")
:
library(ggplot2)
library(patchwork)
p1 <- ggplot(data = mtcars, aes(x = disp, y = cyl, color = as.factor(vs))) +
geom_point()
p2 <- ggplot(data = mtcars, aes(x = disp, y = cyl, color = as.factor(vs))) +
geom_point()
p1 + p2 + plot_layout(guides = "collect")
或者,如果您想将图例放在中心:
p1 + p2 + plot_layout(guides = "collect") & theme(legend.position = 'bottom')