Re: [R] Saving multiple 3x3 TIFF graphics inside a loop

From: <nidal.alhuniti_at_gmail.com>
Date: Tue, 10 May 2011 09:59:08 -0400

Try this

dat=data.frame (ID=rep(1:27,each=10),IDV=rep(seq(1:10),times=27)) dat$DV <- with(dat, 50+15*IDV)
dat=dat[order(dat$ID,dat$IDV),]
for(i in 1:27){

   dt1 = dat[dat$ID==i,]
   pagei=as.integer(ceiling(i/9))

   #open device for subjects 1-9
   if(i==1) {tiff(file=paste("Outfile",pagei,".tiff",sep=""),width=17.15

             ,height=17.15,units="cm", res=1200, pointsize=10, compression = "lzw")}

   if(i==1) {par(mar=c(5,5,4,1),mfrow=c(3,3))}

   ## open new file for subjects 10-18
   if(i==10) {tiff(file=paste("Outfile",pagei,".tiff",sep=""),width=17.15    ,height=17.15,units="cm", res=1200, pointsize=10, compression = "lzw")}    if(i==10) {par(mar=c(5,5,4,1),mfrow=c(3,3))}

   ## open new file for subjects 19-27
   if(i==19) {tiff(file=paste("Outfile",pagei,".tiff",sep=""),width=17.15    ,height=17.15,units="cm", res=1200, pointsize=10, compression = "lzw")}    if(i==19) {par(mar=c(5,5,4,1),mfrow=c(3,3))}

   plot(dt1[, "IDV"],dt1[, "DV"], type="n",ylim=range(dt1[, "DV"]), xlim=range(dt1[, "IDV"]))

   lines(dt1[, "IDV"], dt1[, "DV"], type="b", col=1,lwd=3,pch=1)    title(paste(i),col=8,cex=.8)

   if(i==9) dev.off ()  ## close file no 1
   if(i==18) dev.off () ## close file no 2
   if(i==27) dev.off () ## close file no 3

}

On Tue, May 10, 2011 at 9:25 AM, John S <john.smithr75_at_gmail.com> wrote:

> Thanks Uwe . The parentheses were a typo. Please note that if I have only 9
> subjects in my data then the following works (creates 493 KB data file)
>
>
>
> #windows()
>
> {tiff(file=paste("Outfile",1,".tiff",sep=""),width=17.15
>
> ,height=17.15,units="cm", res=1200, pointsize=10, compression =
> "lzw")}
>
> par(mar=c(5,5,4,1),mfrow=c(3,3))
>
> for(i in 1:9){
>
> dt1 = dat[dat$ID==i,]
>
> #pagei=as.integer(ceiling(i/9))
>
> #This does not work
>
> #if(i==1) {tiff(file=paste("Outfile",pagei,".tiff",sep=""),width=17.15
>
> ##,height=17.15,units="cm", res=1200, pointsize=10, compression = "lzw")}
>
>
> #par(mar=c(5,5,4,1),mfrow=c(3,3))
>
> plot(dt1[, "IDV"],dt1[, "DV"], type="n",ylim=range(dt1[, "DV"]),
> xlim=range(dt1[, "IDV"]))
>
> lines(dt1[, "IDV"], dt1[, "DV"], type="b", col=1,lwd=3,pch=1)
>
> title(paste(i),col=8,cex=.8)
>
> #if(i==5) dev.off ## close file
>
> ## open new file
>
> #if(i==5) {tiff(file=paste("Outfile",pagei,".tiff",sep=""),width=17.15
>
> ##,height=17.15,units="cm", res=1200, pointsize=10, compression = "lzw")}
>
> #if(i==9) dev.off () ## close file
>
> #if(i==9) {tiff(file=paste("Outfile",pagei,".tiff",sep=""),width=17.15
>
> ##,height=17.15,units="cm", res=1200, pointsize=10, compression = "lzw")}
>
> }
>
> dev.off()
>
>
>
>
>
> My workaround around was to break the dataset into small data each with 9
> subjects but if I change the mfrow then I have to change no. of subjects.
> Any smart suggestion to get around this? I tried to open and close devices
> based on iterations (i) but it does not work for me
>
>
>
>
> 2011/5/10 Uwe Ligges <ligges_at_statistik.tu-dortmund.de>
>
> >
> >
> > On 10.05.2011 03:36, John S wrote:
> >
> >> Dear Friends,
> >>
> >> I have been trying to save multiple 3x3 (mfrow=c(3,3) graphics inside a
> >> loop
> >> using tiff figure format (not using PDF or savePlot functions) with no
> >> success. Could you please help?
> >>
> >> Here is a simplified example code:
> >>
> >>
> >>
> >> dat=data.frame (ID=rep(1:10,each=10),IDV=rep(seq(1:10),times=10))
> >>
> >> dat$DV<- with(dat, 50+15*IDV)
> >>
> >> dat=dat[order(dat$ID,dat$IDV),]
> >>
> >> for(i in 1:10){
> >>
> >> dt1 = dat[dat$ID==i,]
> >>
> >> pagei=as.integer(ceiling(i/9))
> >>
> >> #This does not work
> >>
> >> if(i==1) {tiff(file=paste("Outfile",pagei,".tiff",sep="")
> >>
> >> ,width=17.15,height=17.15,units="cm", res=1200, pointsize=10,
> >> compression = "lzw")}
> >>
> >
> > You may not be able to allocate space for this huge tiffs (at least I am
> > not able to do so under Windows).
> >
> >
> >
> >
> > if(i==10) {tiff(file=paste("Outfile",pagei,".tiff",sep="")
> >>
> >> ,width=17.15,height=17.15,units="cm", res=1200, pointsize=10,
> >> compression = "lzw")}
> >>
> >> par(mar=c(5,5,4,1),mfrow=c(3,3))
> >>
> >> plot(dt1[, "IDV"],dt1[, "DV"], type="n")
> >>
> >> lines(dt1[, "IDV"], dt1[, "DV"], type="b", col=1,lwd=3,pch=1)
> >>
> >> title(paste(i),col=8,cex=.8)
> >>
> >> if(i==9) dev.off ## close file
> >>
> >
> > Your forgot parentheses after dev.off.
> >
> > Uwe Ligges
> >
> >
> > }
> >>
> >> dev.off()
> >>
> >> The idea is to save 2 files with 3x3 layouts but I only get one subject
> >> in
> >> Outfile1.
> >>
> >>
> >>
> >> OS: Windows 7 64 bits
> >>
> >> R version 2.13.0
> >>
> >> Many Thanks,
> >>
> >> John
> >>
> >> [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> 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<http://www.r-project.org/posting-guide.html>
> <http://www.r-project.org/posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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<http://www.r-project.org/posting-guide.html>
> and provide commented, minimal, self-contained, reproducible code.
>

        [[alternative HTML version deleted]]



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. Received on Tue 10 May 2011 - 14:09:52 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 10 May 2011 - 14:10:06 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive