拼图图例采集失败

Patchwork legend collecting fails

我一直在尝试收集循环中创建的情节的传说。但是,这无法产生预期的结果,因为未收集图例。我试过了

(res[[1]] / res[[2]]) + plot_layout(guides = "collect") + theme(legend.position = "bottom") 

其中 res[[1]]res[[2]] 是在循环中创建的列表元素。

如有任何建议,我们将不胜感激。

Resulting plot

删除+ 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')