[R] How to capture multiple graph pages to .png ?

jim holtman jholtman at gmail.com
Sat Jan 3 17:36:51 CET 2009


W.R.T. multiple pages, the HELP page says to use a filename like this:

png(filename = "Rplot%03d.png",

where %03d is an incremented value for each page.

On Fri, Jan 2, 2009 at 2:23 PM, Mike Williamson <this.is.mvw at gmail.com> wrote:
> Hello all and thanks in advance for any help,
>
>    There are really two issues that I am having, both related to saving
> graphs:
> 1) It seems that the "dev...()" functions are unstable when using higher
> level graphs (specifically I was using levelplot)
>
> 2) I have a large grid of levelplot graphs that I can print out into a
> subset quite easily when I print to screen, but if I print to a file, I
> cannot print the subset.  (I mention .png in the subject because that's what
> I used, but really any format is acceptable to me).
>
> =========================================================
>
> 1) Unstable dev...() functions:
>
>
> I have the following line of code embedded in a function using simple "plot"
> commands and it works fine:
>
>  if(!is.na(plotFileName)) dev.off()
>
>    I set "plotFIleName" as "NA" by default, in which case the plot prints
> to screen.  If a name is given, the print goes to the png() function.
> Therefore, I want to turn the device off to ensure that the next plot isn't
> sent to the same device & overwrites it.
>    This works *perfectly fine* when using the plot function.  This exact
> same code, however, does not work for levelplot or (I am assuming) any other
> higher level trellis (sp!) graphs.  Below are snippets of the entire code,
> edited a bit for
>
>  if(!is.na(plotFileName)) {
>    print(paste("Saving Zonal plot to file ",plotFileName,".png",sep=""))
>    print("(To display instead, do not pass any 'plotFileName' argument to
> cro.zone.plot function.)")
>    maxLayout <- c(0,length(unique(miniDataSet$splitCol)))
>    png(filename =
> paste(as.character(plotFileName),".png",sep=""),width=(1.2*plotSize*(maxLayout[2]^(1/3))),
>        height=(plotSize*(maxLayout[2]^(1/3))))
>  } else {
>    devAskNewPage(ask = TRUE)
>    maxLayout <- c(0,min(16,length(unique(miniDataSet$splitCol))))
>  }
>  levelplot(dataCol ~ siteX * siteY | splitCol,data = miniDataSet,
>
> col.regions=rainbow(n=length(unique(miniDataSet$dataCol))+5,start=0.5,end=0.95),
>            layout = maxLayout,
>            shrink = c(0.6,1.1),at =
> keyCols,xlab=list(label=xlab,cex=labelScale),
>            ylab=list(label=ylab,cex=labelScale),scales =
> list(cex=axisScale),
>            main=list(label=paste(dataCol,plotTitle,splitCol),
>              cex=titleScale),colorkey=list(labels=list(cex=labelScale)))
>
>  if(!is.na(plotFileName)) dev.off()
>
> If I keep the code exactly as is, it will not work.  If I remove *just the
> last line*, the function will work, and then I simply have to manually
> remove the device "by hand" (also by using dev.off() ) outside of the
> function code.  Now the *really weird* thing:  if I comment out that last
> "if(!is.na... dev.off()" line, the function *still does not work*.  I
> literally have to remove the line, I cannot simply comment it out!  This is
> why I think it is "unstable".
>    I have found a way around this (I simply put a loop in the front of any
> of my plotting functions to ensure they have turned off all devices before
> doing anything... this seems to work), so this is not my biggest problem,
> personally.  But it does seem to be a bigger problem for R itself, and for
> this community.
>    On a related note:  I have noticed that sometimes "devAskNewPage(ask =
> TRUE)" is also buggy.  Normally I can just alter the code slightly to get it
> to work, but the solutions are odd.  (For instance, I have found that if
> "devAskNewPage(ask = TRUE)" is in an "if" clause, it sometimes won't work
> unless I use the { } brackets around it, even with a one line if clause.
>
> ========================================
>
> 2) Printing multiple pages to png:
>
> If you look through the code above, you'll see that I have altered the size
> of the grid that is printed, depending upon whether it is printed to screen
> or to png.  Specifically, look at:
>
>  if(!is.na(plotFileName)) {
> ...
>    maxLayout <- c(0,length(unique(miniDataSet$splitCol)))
> ...
>  } else {
> ...
>    maxLayout <- c(0,min(16,length(unique(miniDataSet$splitCol))))
>  }
>  levelplot(...layout = maxLayout...)
>
> The reason I do this is because when I print to screen, the "askNewPage"
> will allow me to look at the screen before the next one is printed.  I could
> even screen capture that, if I wanted to choose a slow, ugly, and
> non-scriptable solution.  But if I try to send to png, each of the new pages
> of the grid overwrite each other.
>    Specifically, this levelplot is making a grid of ~ 60 graphs.  Since the
> layout maxes to 16 graphs per page, it nicely forces it to a 4 x 4 matrix of
> graphs, 4 pages long.  For the visual display, this is fine, as I mention.
> For the save to png, only one of the pages is captured.  And, oddly, it is
> rarely the last page.  I don't quite understand this, but it is usually
> either the 2nd or the 3rd page that is saved to the png.
>    You can see my solution to it in the code above:  when saving to png,
> all of the graphs are printed out in a gigantic grid, but I also increase
> the height & width of the graph set so that each individual plot isn't too
> tiny.  It isn't a totally useless solution, but still not ideal.
>
>    I think I just don't quite understand how to use these "devices", as
> they are called, and that is the root of the problem.  In fact, I don't even
> know how to bring one device (say, for instance, the device holding the png
> graph) to another device type.  E.g., a solution I was trying to figure out
> was to simply print everything to screen and then switch the screen device
> to png.  This still isn't ideal because it requires constant user
> interaction, but it nonetheless illuminated the fact that I don't understand
> how to manage the devices.
>
>                                     Thanks in advance for any help!
>                                                           Mike
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list