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

Sarah Goslee sarah.goslee at gmail.com
Sat Apr 28 14:25:35 CEST 2012


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.



More information about the R-help mailing list