【问题标题】:grid.arrange plotting same graphic for all plots in listgrid.arrange 为列表中的所有绘图绘制相同的图形
【发布时间】:2015-03-03 13:50:44
【问题描述】:

我正在运行 R 版本 3.1.1。在 RStudio 中,并且在使用 grid.arrange 时遇到了困难。

我正在阅读 100 多家商店的销售数据并绘制一段时间内的销售情况。我还尝试将图分组以一次显示一组数字。

然而,经过多次不同的尝试,命令

do.call("grid.arrange", c(plotlist, ncol=nCol))

导致绘图矩阵中的所有绘图都相同。此外,从绘图列表中绘制单个项目表明所有绘图也作为相同的元素存储在绘图列表中。

这里是重复问题的代码:

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))

【问题讨论】:

    标签: plot duplication


    【解决方案1】:

    我遇到了同样的问题,用 lapply 代替了“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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      相关资源
      最近更新 更多