2017-06-26 24 views
1

rの3つのテキストファイルから複数のプロットをプロットすることに興味があります。例えば、 Iは、V1 = 10が私は60個のファイルが合計までV1(第1列の値)が1複数のファイルから複数のプロットをプロットし、複数のpdfsをrに保存するループ

1_d_a1_s.txt 
1_d_a2_s.txt 
1_d_a3_s.txt 
1_d_b1_s.txt 
1_d_b3_s.txt 
1_d_b3_s.txt 

と同様に6 V1 = 2用のファイル、V1 = 3、......を= 6つのテキストファイルを有します。 V2(2列目)とV3(3列目)の範囲も異なる。

1つのデータセットからプロットすることはできますが、それぞれの6つのデータセット(10個のデータセット)ごとにループスクリプトを作成してそれぞれのpdfに保存する方法はありますか?

1データセットのデータとスクリプトは次のとおりです。ご案内ください。 ありがとう!

# 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() 

答えて

0

あなたの例は、再現するのが難しいですが、ここでは参考になる一般的なテンプレートがあります。

基本的には、ファイルの各グループを1つのディレクトリに配置します。各ディレクトリとその中にある各ファイルを繰り返します。

#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('..') 
} 
関連する問題