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

问题描述 投票:0回答:1
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)):数据长度超过矩阵的大小

r
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.