Plot grid.arrange为列表中的所有绘图安排相同的绘图

Plot grid.arrange为列表中的所有绘图安排相同的绘图,plot,duplication,Plot,Duplication,我正在运行R版本3.1.1。在RStudio中,我在grid.arrange中遇到困难 我正在阅读100多家商店的销售数据,并绘制销售数据。我还尝试将绘图分组,以便一次显示一个设定的数字 但是,在多次尝试之后,命令 do.call("grid.arrange", c(plotlist, ncol=nCol)) 绘图矩阵中所有绘图的结果相同。此外,打印plotlist中的各个项目表明,所有打印也作为相同的元素存储在plotlist中 以下是复制问题的代码: require(ggplot2) re

我正在运行R版本3.1.1。在RStudio中,我在grid.arrange中遇到困难

我正在阅读100多家商店的销售数据,并绘制销售数据。我还尝试将绘图分组,以便一次显示一个设定的数字

但是,在多次尝试之后,命令

do.call("grid.arrange", c(plotlist, ncol=nCol))
绘图矩阵中所有绘图的结果相同。此外,打印plotlist中的各个项目表明,所有打印也作为相同的元素存储在plotlist中

以下是复制问题的代码:

require(ggplot2)
require(gridExtra)

wss <- data.frame(ind=1:10, dep=(1:10))

plotlist <- list()

for (i in 1:4) {
  wss <- data.frame(ind=wss$ind, dep=(i*wss$dep))
  plotname <- paste0("Store", i, sep="")
  plotlist[[ plotname ]] <- ggplot(data=wss, aes(x=wss$ind, y=wss$dep)) + geom_line() + theme_bw() + ggtitle(paste0("Store #",i, sep="")) + ylab("Sales Revenue") + theme(axis.title.x=element_blank())
}

n <- length(plotlist)
nCol <- floor(sqrt(n))
do.call("grid.arrange", c(plotlist, ncol=nCol))
require(ggplot2)
需要(额外)

wss我也遇到了同样的问题,用lappy代替了“for”,似乎解决了这个问题

pList <- lapply(1:4, function(i){ 
  p <- ggplot(data=wss, aes(x=wss$ind, y=wss$dep)) + geom_line() + theme_bw() + ggtitle(paste0("Store #",i, sep="")) + ylab("Sales Revenue") + theme(axis.title.x=element_blank())
}
# print 1 by 1
for (i in 1:4){
print(pList[[i]])
}
print all
 g <- grid.arrange(grobs=pList, ncol=2, nrow=2)
pList