I need to make graphs from multiple tab separated files in a folder. One graph for each file. The first column of these files is same. I also have to shade some regions in all the plots, the regions to shaded is same for all files. The coordinates for shading are in a different file. I can successfully make a graph from a single file with following code.
shading_cor<- read.table(file="shading", header=T, sep="\t") # importing shading cordinates
shading <- geom_rect(aes(NULL, NULL, xmin = start, xmax = end), ymin= -Inf, ymax=Inf, data=shading_cor, fill="grey80", alpha=0.2)
to_be_plotted <- read.table(file="plot_file", header=F, sep="\t") # one of the data files
ggplot() +
geom_line(data=melt(to_be_plotted, id = "V1", measure = c("V2", "V3")), aes(V1, value, colour = variable)) +
shading+theme_bw()
Now to make graphs in a loop, I am roughly using this syntax, which makes plots for each file. I know it is not the most efficient way, but serves my purpose as beginner.
my_files <- list.files(pattern=".txt")
for (i in files){
my_data <- read.table(i, header=F )
pdf(paste0(i,".pdf"))
print (ggplot() + geom_line(data=melt(my_data, id = "V1", measure = c("V2", "V3")), aes(V1, value, colour = variable)))
dev.off
}
However to shade all the plots that that are made by above loop, I modify the code that follows
shading_cor <- read.table(file="shading", header=T, sep="\t") # importing shading cordinates
shading <- geom_rect(aes(NULL, NULL, xmin = start, xmax = end), ymin= -Inf, ymax=Inf, data=shading_cor, fill="grey80", alpha=0.2)
my_files <- list.files(pattern=".txt")
for (i in files){
my_data <- read.table(i, header=F )
pdf(paste0(i,".pdf"))
p1 <- ggplot() + geom_line(data=my_data(graph, id = "V1", measure = c("V2", "V3")), aes(V1, value, colour = variable)
print (p1 + shading)
dev.off
}`
loop following code results in to error:
"arguments imply differing number of rows: 0, 32 Calls: print ... as.data.frame -> as.data.frame.list -> eval -> eval -> data.frame"
I have no idea what is wrong here. I am sure I must be making some very silly mistake. I will be grateful for a solution. EDIT - some test data
> dput(test1.txt)
structure(list(X20 = c(21L, 22L, 23L, 24L, 24L, 26L, 27L, 28L,
29L, 30L), X0 = c(5L, 0L, 7L, 3L, 3L, 4L, 5L, 5L, 4L, 3L), X.5 = c(-2,
0, -12, -5, -6, -5, -4, -4, -3, -2)), .Names = c("X20", "X0",
"X.5"), class = "data.frame", row.names = c(NA, -10L))
> dput(test2.txt)
structure(list(X20 = c(21L, 22L, 23L, 24L, 24L, 26L, 27L, 28L,
29L, 30L), X0 = c(5L, 0L, 0L, 3L, 2L, 4L, 4L, 3L, 2L, 1L), X.2 = c(-3L,
0L, 0L, -4L, -3L, -3L, -2L, 0L, -3L, -2L)), .Names = c("X20",
"X0", "X.2"), class = "data.frame", row.names = c(NA, -10L))
> dput(test3.txt)
structure(list(X20 = c(21L, 22L, 23L, 24L, 24L, 26L, 27L, 28L,
29L, 30L), X0 = c(0L, 2L, 2L, 3L, 3L, 4L, 4L, 3L, 2L, 1L), X.2 = c(-3L,
-3L, -3L, -4L, -3L, -2L, -2L, -1L, 0L, 0L)), .Names = c("X20",
"X0", "X.2"), class = "data.frame", row.names = c(NA, -10L))
and the shading coordinated could be like
start end
20 22
25 27
29 30