This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Plot graphs in R by loop and save it like jpeg

I am trying to plot graphs by loop.

Input data: Tables, which have same ending *depth.txt, there is 2 tab delimited columns in table

Baba    58.38
Tata    68.38
Mama    30.80
jaja    88.65

OUTPUT: I would like to get jpeg file with plot for each *depth.txt (name will be same name like tables) for all files (axis x will be first column from table and axis y will be second column)

I create part of script but it doesn't work..

files <- list.files(path="/home/fil/Desktop/", pattern="*depth.txt", full.names=T,recursive=FALSE)

for (i in 1:length(files)) {
plot(read.table(files[i],header=F,sep="\t")$V1,read.table(files[i],header=F,sep="\t")$V2)
dev.copy(jpeg,filename=files[i])
dev.off() }

But it doesn't work(I received only one .pdf file, where are two graphs, first one is good and second graph has weird index values on both axis and this file have name Rplots.pdf). Could you help me please. I am beginner with R.

Thanks a lot

plot r

help(jpeg)

Also, you're unlikely to want to write over your text files like that. Further, "it doesn't work" isn't enough information for anyone to help you with anything.

I tried to define it..

Hello fedulka!

We believe that this post does not fit the main topic of this site.

This is not a bioinformatics question, but a basic R question.

For this reason we have closed your question. This allows us to keep the site focused on the topics that the community can help with.

If you disagree please tell us why in a reply below, we'll be happy to talk about it.

Cheers!

Hi Michael,

That is true and I agree with you. Truly, I do not know how to solve this problem, so I try it here. But if it is not bioinformatics topic, I agree with your opinion.

Have a nice day
fedulka

try to search on stackoverflow.com you need to understand how the plotting devices in R work first, and therefore this is a generic question. read ?Devices . If you call dev.off() the device is closed and there is no plotting device for your next call to dev.copy, so you need to make a new device in each round.

One way is to store your object names in a vector and then loop over this. You use the paste() function with jpeg() for the purposes of modifying the filename on each iteration of the loop. For plotting, you call the objects via the get() function:

obj1 <- c(1:100)
obj2 <- c(1:100)
obj3 <- c(1:1000)

objects <- c("obj1","obj2","obj3")

for (i in 1:length(objects)) {
  jpeg(paste(objects[i], ".jpg", sep=""))
    plot(get(objects[i]))
  dev.off()
}

....and here the buggers are on my disk:

Screen_Shot_2018_08_16_at_17_33_37

0 answers

No answers yet.

Log in to answer this question.