开发者

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

开发者 https://www.devze.com 2023-03-10 03:49 出处:网络
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:

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?


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.


What happens if you remove the dev.new() from your code? The jpg function/device should generate the multiple files following your pattern as long as you keep writing to the jpg device (the device.new call starts a new device each time, hence the pdf files).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号