我有一堆由Python代码生成的csv文件。这些文件名为
new_pareto_1.csv, new_pareto_2.csv, new_pareto_3.csv, etc.
和
new_paretoF_1.csv, new_paretoF_2.csv, new_paretoF_3.csv, etc.
我需要调整我的代码,以便使用以下代码顺序绘制所有文件对:
library(ggplot2)
dat <- read.csv("/new_pareto_1.csv", check.names = FALSE)
dat2 <- read.csv("/new_paretoF_1.csv", check.names = FALSE)
dat$modu = dat$modu
dat$mnc = dat$mnc
dat$eff = dat$eff
dat2$modu = dat2$modu
dat2$mnc = dat2$mnc
dat2$eff = dat2$eff
res2 <- do.call(rbind, combn(1:2, 2, function(ii)
cbind(setNames(dat2[,c(ii, setdiff(1:2, ii))], c("x", "y")),
var=paste(names(dat2)[ii], collapse="/")), simplify=F))
res <- do.call(rbind, combn(1:2, 2, function(ii)
cbind(row=seq(nrow(dat)),setNames(dat[,c(ii, setdiff(1:2, ii))], c("x", "y")),
var=paste(names(dat)[ii], collapse="/")), simplify=F))
ggplot(res, aes(x=x, y=y, color=row))+ geom_point(shape=4, alpha = .9) +
geom_line(data = res2, aes(x=x, y=y), colour = "black") +
scale_color_gradientn(colours=rev(brewer.pal(9,"Spectral")))+
facet_wrap(~ var, scales="free") +
scale_y_continuous("y", limits = c(0.38, .5)) +
scale_x_continuous("x", limits = c(.25, .75))
并保存到一堆名为:
的jpg文件中solutions_1.jpg, solutions_2.jpg, solutions_3.jpg, etc.
如何在R?
中实现这一目标非常感谢!
答案 0 :(得分:1)
您可以创建一个循环来读取一对文件,处理它们并编写jpeg,然后对每个连续的文件对执行相同的操作。我还没有测试下面的代码,因为我没有样本数据可供使用:
# Vectors with the names of the two file groups we want to work with
fg1 = list.files(pattern="new_pareto_[0-9]*\\.csv")
fg2 = list.files(pattern="new_paretoF_[0-9]*\\.csv")
# Loop over each pair of files
for (i in 1:length(fg1)) {
dat = read.csv(fg1[i])
dat2 = read.csv(fg2[i])
... processing steps ...
p = ggplot(res, aes(x=x, y=y, color=row))+ geom_point(shape=4, alpha = .9) +
geom_line(data = res2, aes(x=x, y=y), colour = "black") +
scale_color_gradientn(colours=rev(brewer.pal(9,"Spectral")))+
facet_wrap(~ var, scales="free") +
scale_y_continuous("y", limits = c(0.38, .5)) +
scale_x_continuous("x", limits = c(.25, .75))
# Get file number
num = gsub(".*_([0-9]*).*", "\\1", fg1[i])
# Write to jpeg file. Set width and height to desired values.
jpeg(paste0("solutions_", num, ".jpg"), 800, 600)
p
dev.off()
}