Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.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
用于从多个文件打印多个绘图并保存在r中的多个PDF中的循环_R_Plot_Reshape - Fatal编程技术网

用于从多个文件打印多个绘图并保存在r中的多个PDF中的循环

用于从多个文件打印多个绘图并保存在r中的多个PDF中的循环,r,plot,reshape,R,Plot,Reshape,我感兴趣的是从r中的多个文本文件(每个文件有3列)绘制多个绘图。例如,我有6个文本文件,其中V1(第一列值)=1 类似地,V1=2,V1=3的6个文件,…直到V1=10,所以总共有60个文件。V2(第2列)和V3(第3列)的范围也不同 我可以从1个数据集进行绘图,但如何生成一个循环脚本来生成所有10个数据集(每个数据集有6个文件)并保存在各自的PDF中 我的1个数据集的数据和脚本如下所示。请导游。 谢谢 #我的数据(1_d_a1_s.txt) V1 V2 V3 1 122 1 1 123

我感兴趣的是从r中的多个文本文件(每个文件有3列)绘制多个绘图。例如,我有6个文本文件,其中V1(第一列值)=1

类似地,V1=2,V1=3的6个文件,…直到V1=10,所以总共有60个文件。V2(第2列)和V3(第3列)的范围也不同

我可以从1个数据集进行绘图,但如何生成一个循环脚本来生成所有10个数据集(每个数据集有6个文件)并保存在各自的PDF中

我的1个数据集的数据和脚本如下所示。请导游。 谢谢

#我的数据(1_d_a1_s.txt)
V1 V2 V3
1   122 1
1   123 1
1   124 1
1   132 2
1   133 2
1   134 3
1   140 3
1   141 2
1   142 2
1   143 4
1   144 2
1   145 10
#我的数据(2_d_a1_s.txt)
V1 V2 V3
2   127 1
2   128 1
2   132 2
2   133 3
2   134 3
2   140 3
2   145 2
2   142 2
2   143 4
2   144 2
2   157 8
#用于从1个数据集打印数据的Rscript
图书馆(E2)

1_a1你的例子有点难以重现,这里有一个通用的模板可能会有所帮助

实际上,您将每组文件放在一个目录中。迭代每个目录和其中的每个文件

#getwd()
for(i in 1:10){
  dir.create(path = paste0('dir',i),)
  setwd(paste0('dir',i))
  for(j in 1:6){
    df <- data.frame(V1 = 1, V2 = 2, V3 = 3)
    write.table(df, file = paste0('file',j,'.txt')) 
  }
  setwd('..')
}


d <- list.dirs(path = getwd(), full.names = T)
d <- d[grepl(x = d,'dir')]

for(i in d){
  setwd(i)
  pdf('helloworld.pdf')
  plot(0, type = 'n', xlim = c(-10,10), ylim = c(-10,10))
  f <- list.files(i, pattern  = 'txt')
  for(j in f){
    df <- read.table(j)
    points(df[,1],df[,2])
  }
  dev.off()
  setwd('..')
}
#getwd()
因为(我在1:10){
创建目录(路径=粘贴0('dir',i),)
setwd(粘贴0('dir',i))
对于(1:6中的j){
df
# my data (1_d_a1_s.txt)
V1  V2  V3
1   122 1
1   123 1
1   124 1
1   132 2
1   133 2
1   134 3
1   140 3
1   141 2
1   142 2
1   143 4
1   144 2
1   145 10

# my data (2_d_a1_s.txt)
V1  V2  V3
2   127 1
2   128 1
2   132 2
2   133 3
2   134 3
2   140 3
2   145 2
2   142 2
2   143 4
2   144 2
2   157 8

# Rscript for plotting data from 1 dataset
library(reshape2)
1_a1 <- read.table("1_d_a1_s.txt", header=FALSE, sep ="\t")
1_a2 <- read.table("1_d_a2_s.txt", header=FALSE, sep ="\t")
1_a3 <- read.table("1_d_a3_s.txt", header=FALSE, sep ="\t")
1_b1 <- read.table("1_d_b1_s.txt", header=FALSE, sep ="\t")
1_b2 <- read.table("1_d_b2_s.txt", header=FALSE, sep ="\t")
1_b3 <- read.table("1_d_b3_s.txt", header=FALSE, sep ="\t")

1_a1_r <- rename(1_a1,c(V1="A", V2="B", V3="C"))
1_a2_r <- rename(1_a2,c(V1="A", V2="B", V3="C"))
1_a3_r <- rename(1_a3,c(V1="A", V2="B", V3="C"))
1_b1_r <- rename(1_b1,c(V1="A", V2="B", V3="C"))
1_b2_r <- rename(1_b2,c(V1="A", V2="B", V3="C"))
1_b3_r <- rename(1_b3,c(V1="A", V2="B", V3="C"))

pdf("1_d_s.pdf")        
plot(NULL, lwd=1, xlim=range(1_a1_r$B, 1_a2_r$B, 1_a3_r$B,1_b1_r$B, 1_b2_r$B, 1_b3_r$B), ylim=range(1_a1_r$C, 1_a2_r$C, 1_a3_r$C,1_b1_r$C, 1_b2_r$C, 1_b3_r$C), xlab="B", ylab = "C", main="1_d_s Plot", xaxt="n")
axis(1, at = seq(0, 2000, by = 100), las = 2)                           
lines(1_a1_r$B,1_a1_r$C, pch=1, col= 1, lwd=1)
lines(1_a2_r$B,1_a2_r$C, pch=2, col= 2, lwd=1)
lines(1_a3_r$B,1_a3_r$C, pch=3, col= 3, lwd=1)
lines(1_b1_r$B,1_b1_r$C, pch=4, col= 4, lwd=1)
lines(1_b2_r$B,1_b2_r$C, pch=5, col= 5, lwd=1)
lines(1_b3_r$B,1_b3_r$C, pch=6, col= 6, lwd=1)
legend("topright",c("a1","a2", "a3", "b1", "b2", "b3"),col=c(1, 2, 3, 4, 5, 6), lwd=1, cex=1.0, text.font=8)
dev.off()
#getwd()
for(i in 1:10){
  dir.create(path = paste0('dir',i),)
  setwd(paste0('dir',i))
  for(j in 1:6){
    df <- data.frame(V1 = 1, V2 = 2, V3 = 3)
    write.table(df, file = paste0('file',j,'.txt')) 
  }
  setwd('..')
}


d <- list.dirs(path = getwd(), full.names = T)
d <- d[grepl(x = d,'dir')]

for(i in d){
  setwd(i)
  pdf('helloworld.pdf')
  plot(0, type = 'n', xlim = c(-10,10), ylim = c(-10,10))
  f <- list.files(i, pattern  = 'txt')
  for(j in f){
    df <- read.table(j)
    points(df[,1],df[,2])
  }
  dev.off()
  setwd('..')
}