0

我一直在尝试收集循环创建的图例。但是,这无法产生预期的结果,因为没有收集图例。我试过

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

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

任何建议将不胜感激。

结果图

4

1 回答 1

0

删除+ 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')
于 2021-04-26T06:59:16.600 回答