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

Peter Cowan cowan.pd at gmail.com
Fri Aug 8 06:52:21 CEST 2008


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.

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()



More information about the R-help mailing list