【问题标题】:Multiple graphs over multiple pages using ggplot and marrangeGrob使用 ggplot 和 marrangeGrob 在多个页面上绘制多个图表
【发布时间】:2017-07-24 10:01:52
【问题描述】:

虽然这可能是重复的,但我想知道我的问题的解决方案。我试图运行来自问题Multiple graphs over multiple pages using ggplot 的代码,但我不知道数据是如何结构化的,因此无法运行它。 我想绘制多个图表并将它们打印在 pdf 的多个页面上。我用 mtcars 试过了:

library(ggplot2)
library(reshape2)
library(plyr)
library(gridExtra)

mtcars2 = melt(mtcars, id.vars='mpg')
mtcars2$group[mtcars2$variable  %in% c("cyl", "disp", "hp", "drat")] <- "A"
mtcars2$group[mtcars2$variable  %in% c("wt", "qsec", "vs", "am")] <- "B"
mtcars2$group[mtcars2$variable  %in% c("gear", "cyl")] <- "C"


p = ggplot(mtcars2) +
  geom_jitter(aes(value,mpg, colour=variable)) + 
  geom_smooth(aes(value,mpg, colour=variable), method="lm", se=FALSE) +
  scale_y_continuous(limits = c(0, 60))+
  labs(x = "Percentage cover (%)", y = "Number of individuals (N)")

plots = dlply(mtcars2, "group", `%+%`, e1 = p)
ml = do.call(marrangeGrob, c(plots, list(nrow=2, ncol=2)))
ggsave("multipage.pdf", ml)

产生错误Error in (function (grobs, ncol, nrow, ..., top = quote(paste("page", : argument "grobs" is missing, with no default。如何让这个脚本运行?

【问题讨论】:

    标签: r ggplot2 plyr gridextra


    【解决方案1】:

    使用 marrangeGrob(grobs = plots, nrow=2, ncol=2) 而不是以前版本的 gridExtra 中需要的marrangeGrob(grobs = plots, nrow=2, ncol=2)

    【讨论】:

      【解决方案2】:

      您可以使用 ggplus 包:

      https://github.com/guiastrennec/ggplus
      

      我特别发现它比使用 ArrageGrobs/gridExtra 更容易;它会自动为您将各个方面放在几个页面中。

      由于您将初始绘图保存为 "p",因此您的代码将如下所示:

      # Plot on multiple pages
      facet_multiple(plot = p, 
                 facets = 'group', #**** 
                 ncol = 2, 
                 nrow = 1)
      

      【讨论】:

      • 谢谢。有用。如果我使用pdf("your_path/testforum.pdf", width=18, height=9) ml = facet_multiple(plot = p, facets = 'variable', #**** ncol = 2, nrow = 2) ggsave("multipage.pdf", ml) dev.off(),那么它会在最后一个 pdf 页面上显示空图,其中只剩下 2 个图(而不是 4 个)。你知道为什么会这样吗?
      • 很高兴听到它有效!在任何事情之前检查您希望看到多少地块。让我知道我是否可以提供更多帮助以将此答案标记为已接受。
      • 还有参数scales = free吗?我的数据范围非常不同...
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 2016-08-30
      相关资源
      最近更新 更多