I am interested to plot multiple plots from multiple text files (each with 3 columns) in r. For eg I have 6 text files where V1(1st column value) = 1
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
and similarly 6 files for V1 =2, V1=3,......until V1=10 so total I have 60 files. V2 (2nd column) and V3 (3rd column) have different ranges also.
I can plot from 1 dataset but how to make a loop script to generate for all 10 datasets (each with 6 files) and save in respective pdfs?
My data and script for 1 dataset is as follows. Please guide. Thanks!
# 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()
Your example is kinda hard to reproduce, here is a general template which may help.
Essentially you put each group of files in one directory. Iterate over each directory and each file that lies within.
#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('..')
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.