我正在尝试保存多个pdf绘图上的绘图,每页有一个整体图例。我想知道是否有一种巧妙的方法来使用ggsave和ggpubr::ggarrange执行此操作?更改nrow ncol范围ggarrange仅更改一页的设置,并强制所有绘图位于同一页面上(而不是超过几页)。
library(ggplot2)
library(ggpubr)
plot_f <- function(df, xx) {
df %>%
filter(carb == xx) %>%
ggplot() +
geom_line(aes(x = disp, y = hp, colour = "darkblue" )) +
geom_line(aes(x = disp, y = qsec, colour = "red")) +
scale_color_discrete(name = "Y series", labels = c("Y2", "Y1"))
}
dd <- unique(mtcars$carb)
list_plots <- map(dd, function(x) {
plot_f(mtcars, x)
})
#WORKS this saves plots all on the same page
ggsave("test.pdf", ggpubr::ggarrange(plotlist = list_plots, # list of plots
labels = "AUTO", # labels
common.legend = T, # COMMON LEGEND
legend = "bottom", # legend position
align = "hv", # Align them both, horizontal and vertical
nrow = 6),
height = 9,
width = 12)
Run Code Online (Sandbox Code Playgroud)
由于 2x2 小于绘图总数,以下代码会引发错误:
ggsave("test.pdf", ggpubr::ggarrange(plotlist = list_plots, # list of plots
labels = "AUTO", # labels
common.legend = T, # COMMON LEGEND
legend = "bottom", # legend position
align = "hv", # Align them both, horizontal and vertical
nrow = 2, ncol = 2),
height = 9,
width = 12)
Error in UseMethod("grid.draw") :
no applicable method for 'grid.draw' applied to an object of class "c('list', 'ggarrange')"
Run Code Online (Sandbox Code Playgroud)
我想要一种灵活的方法来提供各种nrow ncol值,类似于包marrangeGrob中的函数gridExtra,例如nrow = 2 ncol = 2. 我知道会做类似的事情,但我喜欢使用cowplot可以轻松添加整体图例。 ggpubrcommon.legend = T
谢谢