【问题标题】:marrangeGrob giving error for nrowmarrangeGrob 给出 nrow 错误
【发布时间】:2019-04-11 16:31:13
【问题描述】:

我正在尝试使用 for 循环创建一堆 ggplot2 图,然后将它们保存在多页 pdf 文档中,但我在使用 marrangeGrob 时遇到了问题。下面是一些示例代码:

 Plots <- list()
 Plots[[1]] <- qplot(mtcars$mpg, mtcars$wt)
 Plots[[2]] <- qplot(mtcars$cyl, mtcars$wt)
 Plots[[3]] <- qplot(mtcars$mpg, mtcars$qsec)
 Plots[[4]] <- qplot(mtcars$cyl, mtcars$drat)

 # install.packages("gridExtra", dependencies = TRUE)
 library(gridExtra)

 MyPlots <- do.call(marrangeGrob, c(Plots, nrow = 1, ncol = 2))
 ggsave("My plots on multiple pages.pdf", MyPlots)

我过去曾使用过类似版本的 do.call(marrangeGrob... 行并让它们工作,但现在,当我尝试执行该行时出现此错误:Error: nrow * ncol &gt;= n is not TRUE。与此类似的代码过去可以工作的事实使我认为其中一个软件包中的某些内容已经更新。对于如何解决这个问题,有任何的建议吗?

【问题讨论】:

  • ggsave("out.pdf", marrangeGrob(Plots, nrow = 1, ncol = 2)) 可能是由于最近的变化
  • 这对我不起作用。我收到一个错误,plot should be a ggplot2 plot.
  • 好的...几个选项; pdf("out.pdf"); marrangeGrob(Plots, nrow = 1, ncol = 2) ; dev.off() 或升级 ggplot2,它现在已经删除了对 ggplot2 对象的检查,或者使用从 SO..ggsave &lt;- ggplot2::ggsave; body(ggsave) &lt;- body(ggplot2::ggsave)[-2] 学到的 ahack,如果不升级
  • ggplot2 已经是最新的了,但是 pdf("out.pdf"; marrangeGrob... 等效果很好!谢谢!
  • 啊好的...重新 ggplot2 ,我想我可能有来自github的开发版本

标签: r ggplot2 gridextra r-grid


【解决方案1】:

使用新的grobs 参数,语法发生了一些变化。你应该使用

marrangeGrob(grobs=Plots, nrow = 1, ncol = 2)

或者,等价的,

do.call(marrangeGrob, list(grobs=Plots, nrow = 1, ncol = 2))

【讨论】:

  • 我们如何确保使用上述命令每页保存一个绘图?
猜你喜欢
  • 1970-01-01
  • 2018-05-26
  • 2015-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-20
  • 2012-09-25
  • 2013-03-21
相关资源
最近更新 更多