【问题标题】:R: storing plots generated in loop in list and passing to multiplotR:将循环生成的图存储在列表中并传递给多图
【发布时间】:2018-05-30 20:40:46
【问题描述】:
library("ggplot2")
library("Rmisc")

myplots <- list()
x = seq(1,100,1)
y = seq(1,100,1)

for(i in 1:10) {
    myplots[[i]] <- plot(x,y)
}

multiplot(plotlist = myplots, cols=2)

我收到以下错误:

单位错误(rep_len(1, nrow), "null") : 'x' 和 'units' 必须有 长度> 0 另外:警告消息:在矩阵中(序列(1,列* 天花板(numPlots/cols)),ncol = cols,nrow = 天花板(numPlots/cols)): 数据长度超过矩阵大小

【问题讨论】:

  • 在真实的代码中,for循环生成了独特而有意义的图,不过我这里已经简化了。

标签: r


【解决方案1】:

您没有使用 ggplot object ,您使用的是 R 基础图,因此 multiplot 不起作用:

你应该这样做:

library("ggplot2")
library("Rmisc")

myplots <- list()
x = seq(1,100,1)
y = seq(1,100,1)
df <- data.frame(x, y)

for(i in 1:10) {
  myplots[[i]] <- ggplot(data=df, aes(x,y)) + geom_point()
}

multiplot(plotlist = myplots, cols=2)

?多图:

Usage

  multiplot(..., plotlist = NULL, cols = 1, layout = NULL)
Arguments

...      ggplot objects
plotlist a list of ggplot objects
cols     Number of columns in layout
layout   A matrix specifying the layout. If present, 'cols' is ignored
         Note

【讨论】:

  • Forgot multiplot 仅适用于 ggplots。希望有一个等效的遗传多重情节适用于任何情节类型......
猜你喜欢
  • 2021-01-19
  • 2017-06-24
  • 2014-03-11
  • 2017-06-12
  • 2017-10-25
  • 2021-07-01
  • 2022-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多