[R] grid layout scaling viewport width based solely on height

Peter Cowan cowan.pd at gmail.com
Wed Aug 6 10:13:30 CEST 2008


Hello all,

I'm trying to write a function that produces a main plotting region
with several square plots along the right side.  Ideally the size of
right side plots will scale only with the height of the entire plot,
yet never overlap with another secondary plot.  The following two
snippets get close, however, as I resize the plot horizontally the
right side plots (green squares) get smaller whereas I'd like them to
remain the same size.  It seems the answer lies in setting the layout
width for the side plot column in a clever way that I haven't been
able to discover.  Any suggestions on how to achieve that effect or
other improvements to the below code are most welcome.

Hopefully, my explanation makes my goal clear.  Also note, the real
application is a function that will have arbitrary numbers of side
plots.

Thanks

Peter

require(grid)
grid.newpage()
lout1 <- grid.layout(nrow = 1, ncol = 2,
    widths = unit(c(1, 1/10), c('null', 'snpc'))
)
pushViewport(viewport(layout = lout1, w = 0.8, h = 0.8))
    pushViewport(viewport(layout.pos.col = 1))
        grid.rect()
    popViewport()

    pushViewport(viewport(layout.pos.col = 2))
        lout2 <- grid.layout(nrow = 10, ncol = 1,
            heights = unit(1, 'snpc'))
        pushViewport(viewport(layout = lout2))
            for(i in seq(0, 1, length.out = 10)) {
                pushViewport(viewport(y = i, width = unit(1, 'snpc'),
                                      height = unit(1, 'snpc')))
                    grid.rect(gp = gpar(col = 3))
                popViewport()
            }
        popViewport()
    popViewport()
popViewport()


grid.newpage()
lout3 <- grid.layout(nrow = 10, ncol = 11,
    respect = matrix(c(rep(0, 10 * 10), rep(0, 10 - 1),  1), nrow = 10)
)
pushViewport(viewport(layout = lout3, w = 0.8, h = 0.8))
    pushViewport(viewport(layout.pos.col = 1:10))
        grid.rect()
    popViewport()

    pushViewport(viewport(layout.pos.col = 11))
        for(i in seq(0, 1, length.out = 10)) {
            pushViewport(viewport(y = i, width = unit(1, 'snpc'),
                                  height = unit(1, 'snpc')))
                grid.rect(gp = gpar(col = 3))
            popViewport()
        }
    popViewport()
popViewport()



More information about the R-help mailing list