【问题标题】:ggsave does not save all the elements to pdfggsave 不会将所有元素保存为 pdf
【发布时间】:2019-05-24 07:27:22
【问题描述】:

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

【问题讨论】:

  • 我认为问题在于您将plots 传递给ggsave(),而不是do.call() 的输出。
  • 问题解决了!谢谢

标签: r ggplot2


【解决方案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)

【讨论】:

    猜你喜欢
    • 2015-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    • 2019-11-26
    • 1970-01-01
    • 2011-06-07
    相关资源
    最近更新 更多