[R] Exporting plots generated by a 'for' loop

Greg Snow 538280 at gmail.com
Mon Apr 30 21:01:42 CEST 2012


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



More information about the R-help mailing list