Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to page multiple plots in R in separate jpeg files?

I'd like to plot multiple plots in separate bitmap files using the file name pattern (for example, for JPEG) file.%03d.jpg in R. I tried using something like:

somevar <- 1
jpg(paste(sep='',filename,'.%03d.jpg'))
while(somevar <= n)
{
  plot(data[somevar])
  dev.new()
  somevar <- somevar + 1
}
dev.off()

but it creates one .jpg file and several Rplotnnn.pdf files. How can I change the default device to jpg, and use the custom file name pattern?

like image 682
Vilinkameni Avatar asked Jun 01 '11 12:06

Vilinkameni


1 Answers

I think this should work

somevar <- 1
while(somevar <= n) {
  jpg(sprintf("%s%03.jpg", filename, somevar))
  plot(data[somevar])
  dev.off()
  somevar <- somevar + 1
}

Plotting goes from device opening (here jpeg(...)) to dev.off(). You control the filename (where I corrected your use of paste() to sprintf()) and the loop.

like image 125
Dirk Eddelbuettel Avatar answered Sep 27 '22 16:09

Dirk Eddelbuettel