[R] gridBase and new.page() / grid.newpage()

Paul Murrell p.murrell at auckland.ac.nz
Sun Aug 10 22:48:13 CEST 2008


Hi


Peter Cowan wrote:
> Hello all,
> 
> I'm trying to write a function using the gridBase package.  I'd like
> to push several base subplots to a larger plot constructed with grid.
> However, I'm having trouble getting consistent results when running
> the function when the plotting window (quartz) is closed, when it is
> left open and the plot function is repeated to the same window, and
> when the output is saved to a pdf, see the example below.  In the case
> of the pdf plot pages are preceded by a blank page.  However, leaving
> out either plot.new() or grid.newpage() each presents different
> problems.  I looked through the R Graphics book and package vignette
> but didn't see this particular issue addressed.


There can be complications when both the 'graphics' package and the
'grid' package try to start a new page.

Before trying to untangle this particular example, is it possible to
side step the problem by drawing those density plots using a
grid-compatible system such as either lattice or ggplot ?

Or, if the "real" case is as simple as this, perhaps even just
grid.lines() will do the job (?)

Paul


> Thanks in advance,
> 
> Peter
> 
> myplot <- function() {
>     grid.newpage()
>     plot.new()
>     n <- 10
>     datalayout <- grid.layout(ncol = 2, width = unit(c(1, 1/n),
> c('null', 'null')))
> 
>     pushViewport(viewport(width = .8, height = .8, layout = datalayout))
>     grid.rect(gp=gpar(col="grey"))
> 
>     for(i in 1:n) {
>         pushViewport(viewport(
>             y = i/n - 0.5 / n, x = 1,
>             height = unit(1/n, 'npc'),
>             width = unit(1/n, 'npc')))
>             par(plt = gridPLT(), new = TRUE)
>             grid.rect(gp=gpar(fill="light grey"))
>             dens <- density(rnorm(10))
>             plot.density(dens, axes = FALSE,
>                 mar = c(0,0,0,0), main = "", xlab = "", ylab = "")
>         upViewport()
>     }
>     pushViewport(viewport(xscale = c(0, 1), layout.pos.col = 1))
>     grid.rect(gp=gpar(fill="grey"))
>     grid.points()
>     upViewport()
>     upViewport()
> }
> 
> myplot()
> myplot()
> 
> pdf()
> myplot()
> myplot()
> dev.off()
> 
> ______________________________________________
> 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.

-- 
Dr Paul Murrell
Department of Statistics
The University of Auckland
Private Bag 92019
Auckland
New Zealand
64 9 3737599 x85392
paul at stat.auckland.ac.nz
http://www.stat.auckland.ac.nz/~paul/



More information about the R-help mailing list