以下代码使用ggplot和grid.arrange生成图。当我以pdf格式保存时,首页注释和底页注释都会丢失
library(tidyverse)
library(ggplot2)
library(gridExtra)
plots <- lapply(unique(mtcars$cyl), function(cyl) {
data <- mtcars %>% filter(cyl == cyl)
ggplot(data, aes(x=mpg, y=hp))+
geom_point(color = "blue")+
facet_wrap(.~carb)}) %>%
do.call(grid.arrange, .)
do.call(grid.arrange, c(plots,list(top="top", bottom="bottom")) )
ggsave(file="C:\\temp\\plot.pdf", plots, width = 21, height = 29.7, units = "cm", dpi = 300)
答案 0 :(得分:1)
这里的问题是将plots
传递给ggsave()
,而不是实际需要的图,即do.call()
的输出。应改为:
tmp1 <- do.call(grid.arrange, c(plots, list(top = "top", bottom = "bottom")))
ggsave(file = "C:\\temp\\plot.pdf", plot = tmp1)