Hello! I have a 'for' loop that generates a plot with each iteration. I would either like the plots to be stacked one below the other in a single .jpg file or be stored in three different files with each file being named dynamically. The following code is an illustration of my query (but does not accomplish my aim):- for (i in 1:4) { jpeg("samplo.jpg") par(mfrow=c(3,1)) plot(iris[,i+1]~iris[,i]) dev.off() } Is it possible to fulfill any of my two desires? If not, what is the best alternative I have? I hope I have put forth my question clearly. Many thanks for all your help. Regards Tejas Kale IUCAA, Pune, India
I don't think jpeg supports pages, but you have other options, including one you asked about: Use a device like pdf() that supports pages and don't call dev.off() until the loop has finished. Use paste() to give each graph a separate name, rather than overwriting them at each iteration, such as jpeg(paste("samplo", i, ".jpg", sep=".")). Sarah On Apr 28, 2012, at 6:40 lAM, Tejas Kale <kaletejas2006 at gmail.com> wrote:> Hello! > > I have a 'for' loop that generates a plot with each iteration. I would > either like the plots to be stacked one below the other in a single > .jpg file or be stored in three different files with each file being > named dynamically. The following code is an illustration of my query > (but does not accomplish my aim):- > > for (i in 1:4) > { > jpeg("samplo.jpg") > par(mfrow=c(3,1)) > plot(iris[,i+1]~iris[,i]) > dev.off() > } > > Is it possible to fulfill any of my two desires? If not, what is the > best alternative I have? I hope I have put forth my question clearly. > > Many thanks for all your help. > > Regards > Tejas Kale > IUCAA, Pune, India > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.
On 04/28/2012 08:40 PM, Tejas Kale wrote:> Hello! > > I have a 'for' loop that generates a plot with each iteration. I would > either like the plots to be stacked one below the other in a single > .jpg file or be stored in three different files with each file being > named dynamically. The following code is an illustration of my query > (but does not accomplish my aim):- > > for (i in 1:4) > { > jpeg("samplo.jpg") > par(mfrow=c(3,1)) > plot(iris[,i+1]~iris[,i]) > dev.off() > } > > Is it possible to fulfill any of my two desires? If not, what is the > best alternative I have? I hope I have put forth my question clearly. >Hi Tejas, Just rearrange: # set up your graphics device jpeg("samplo.jpg") # set the margins par(mfrow=c(3,1)) # now do the three plots for(i in 1:3) plot(iris[i+1]~iris[i]) # and close the device dev.off() I think you only want three plots, no? Jim
A couple of things that I did not see mentioned by the others: Generally statistics plots work better in .png files than in .jpg files due to the type of compression each uses (detailed image plots and some surface plots may be the exception), though that may be out of your control (some journals insist on jpeg even when something else is better). You can give additional arguments to the jpeg function, one option would be to have it create the graphics region 3 times as high as it is wide, then use par(mfrow=c(3,1)) to put 3 plots in 1 figure. Another option is to specify a name like "samplo%03d.jpg", then the first plot will be saved to "samplo001.jpg", the second will be saved as "samplo002.jpg" and so forth. In either case the jpeg call (and par call) should be before the for loop and the dev.off should be after the loop. On Sat, Apr 28, 2012 at 4:40 AM, Tejas Kale <kaletejas2006 at gmail.com> wrote:> Hello! > > I have a 'for' loop that generates a plot with each iteration. I would > either like the plots to be stacked one below the other in a single > .jpg file or be stored in three different files with each file being > named dynamically. The following code is an illustration of my query > (but does not accomplish my aim):- > > for (i in 1:4) > { > ? jpeg("samplo.jpg") > ? par(mfrow=c(3,1)) > ? plot(iris[,i+1]~iris[,i]) > ? dev.off() > } > > Is it possible to fulfill any of my two desires? If not, what is the > best alternative I have? I hope I have put forth my question clearly. > > Many thanks for all your help. > > Regards > Tejas Kale > IUCAA, Pune, India > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com