Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将GGPLOT保存到for循环中的列表_R_Ggplot2 - Fatal编程技术网

将GGPLOT保存到for循环中的列表

将GGPLOT保存到for循环中的列表,r,ggplot2,R,Ggplot2,我在for循环中生成九个ggplots,然后使用网格排列这些绘图。排列: plot_list <- list() for(i in c(3:ncol(bilanz.vol))) { histogram <- ggplot(data = bilanz.vol, aes(x = bilanz.vol[,i])) + geom_histogram() + scale_x_log10() + ggtitle(paste(varnames[i])) # ggsave(fi

我在for循环中生成九个
ggplots
,然后使用
网格排列这些绘图。排列

plot_list <- list()
for(i in c(3:ncol(bilanz.vol))) {
  histogram <- ggplot(data = bilanz.vol, aes(x = bilanz.vol[,i])) +
  geom_histogram() +
  scale_x_log10() +
  ggtitle(paste(varnames[i]))

  # ggsave(filename = paste("Graphs/", vars[i], ".png", sep = ""), width = 16, height = 12, units = "cm")

  plot_list <- c(plot_list, list(histogram))
}

library(gridExtra)

png(filename = "Graphs/non-mfi.png", width = 1280, height = 960, units = "px")
do.call(grid.arrange, c(plot_list, list(ncol = 3)))
dev.off()

plot\u list原因是ggplot在使用
aes
调用之前没有对表达式求值(至少我相信是这样),它只是设置了绘图并将数据存储在其中。在您的情况下,“数据”是整个数据帧
bilanz.vol
,因为
i=ncol(bilanz.vol)
for
循环完成后,表达式
bilanz.vol[,i]
将对所有打印对象进行相同的计算

要使其正常工作,可以这样做,以确保所有绘图对象包含不同的数据集
my.data

my.data <- data.frame(x = bilanz.vol[,i])
histogram <- ggplot(data = my.data, aes(x = x)) +
    geom_histogram() +
    scale_x_log10() +
    ggtitle(paste(varnames[i]))

my.data看起来您无法
打印
。至少R-FAQ是这么说的。我想你可以尝试使用
force
。因为所有绘图都在同一个表格上,我建议你使用
facet\u wrap
,而不是将它们与
网格一起粘贴。排列
。添加
打印
不会改变任何东西。