ggsave不会将所有元素保存为pdf

时间:2019-05-24 07:27:23

标签: r ggplot2

以下代码使用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)

1 个答案:

答案 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)
相关问题