目标:将多个qplots保存到图中。 重点是有一个循环列表参数(参见代码。它更清楚!)。输出与我预期的不同。他们是一样的!
# Make y be a list containing different values.
y <- list()
for (j in 1:6) {
y[[j]] <- rnorm(10)
}
# plot multi qplots into one figure
plots <- list() # new empty list
for (i in 1:6) {
p1 = qplot(1:10, y[[i]], main = i)
plots[[i]] <- p1 # add each plot into plot list
}
do.call(grid.arrange, plots)
代码从http://rstudio-pubs-static.s3.amazonaws.com/2852_379274d7c5734f979e106dcf019ec46c.html
修订部分y
:(以说明它们不同!)。这个数字只用了y中的最后一个。这很奇怪。
[[1]]
[1] 2.01846525 -2.32504052 -1.07201485 -0.21105479 0.25706024 0.50934754
[7] 0.39844954 0.18110421 1.03368602 0.01185971
[[2]]
[1] 0.01824317 -1.51801208 1.68385158 -0.30159404 -0.34894329 0.62840458
[7] -0.45447576 1.18625774 -0.36671100 -0.05502285
...
[[6]]
[1] 0.1134854 0.1806742 -0.9491033 0.7279389 -0.2193326 0.1595183 -1.1751557
[8] -0.4416456 -0.7074360 -0.3887882
答案 0 :(得分:1)
这与传递给qplot
的参数的惰性扩展有关。在您打印绘图之前,这些值实际上并未解析。此时,循环后,i
的值仅为6。
plots <- lapply(1:6, function(i) {
force(i) #required if you didn't have main=i to force the evaluation of i
qplot(1:10, y[[i]], main = i)
})
do.call(grid.arrange, plots)