首页 > 解决方案 > 当我尝试运行 for 循环时,为什么 R 会删除我文件中的所有信息?

问题描述

我准备了一个建模程序,它生成 26 个单独的文件,我需要在 R 中以相同的方式绘制等高线图 - 我计划重复运行该程序,因此一直在尝试创建一个循环来创建和保存所有 26 个图每次运行的文件。

在我运行创建绘图的函数时,它会从输入电子表格中删除所有数据(即文件大小从 1.2Mb 变为 0Mb) - 以前没有这样做,谁能解释出了什么问题?

我的数据样本:

Z    X     T
0    0     0
0    0.005 0
0    0.01  0
0    0.015 0

(等等 84k 行)

我的代码:

# List all files in folder to be plotted
filenames <- list.files(path=".",
                    pattern="csv", 
                    full.names=TRUE)

#Creates a contour plot in ggplot of the variable in xz space
makeplot <- function(filename) {
data <- read.csv(file = filename)

ggplot(data=data, mapping = aes(x = data[,2], 
                              y = data[,1], 
                              z = data[,3])) +

geom_raster(data=data, aes(fill=data[,3]), show.legend=TRUE, interpolate = 
FALSE) +
scale_fill_gradient(limits=range(data[,3]), high = 'red', low = 'white') +
geom_contour(bins = 50, colour = "black") +
xlab(label = "Distance from ridge axis") +
ylab(label = "Depth") +
theme_classic()+
coord_cartesian(
  ylim = c(0,1), xlim = c(0,2))+
scale_x_continuous(expand = c(0, 0)) + 
scale_y_continuous(expand = c(0, 0)) +
guides(fill=guide_legend(title="Yb concentration")) +
theme(legend.position="bottom")

}

# Repeat plotting
for(f in filenames) {
    makeplot(f)
}

任何帮助,将不胜感激!

标签: rfor-loopggplot2

解决方案


推荐阅读