R:存储在循环列表中生成的图并传递到多个图

时间:2017-12-17 06:04:12

标签: r

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另外:警告信息:矩阵(seq(1,cols *)   ceiling(numPlots / cols)),ncol = cols,nrow = ceiling(numPlots / cols)):   数据长度超过矩阵的大小

1 个答案:

答案 0 :(得分:0)

您没有使用ggplot对象,您正在使用R基本图,因此多行不能正常工作:

你应该这样做:

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)

<强>的multiplot:吗

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

enter image description here