Created
December 9, 2014 19:00
-
-
Save mplourde/a2c57c2b0db16f6d9ac9 to your computer and use it in GitHub Desktop.
Working with layouts in grid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# example1 (using grobs instead of grid.*) | |
vp <- vpTree( | |
parent=viewport(layout=grid.layout(nrow=2, ncol=2), name='parent'), | |
children=vpList( | |
viewport(layout.pos.row=1, layout.pos.col=1, name='vp1'), | |
viewport(layout.pos.row=1, layout.pos.col=2, name='vp2') | |
) | |
) | |
pushViewport(vp) | |
seekViewport('parent') | |
gs <- gList( | |
rectGrob(height=.5, width=.5, vp='vp1'), | |
textGrob('sq1', vp='vp1'), | |
rectGrob(height=.5, width=.5, vp='vp2'), | |
textGrob('sq2', vp='vp2') | |
) | |
grid.draw(gs) | |
# example 2 (custom grob) | |
graphics.off() | |
foursquareGrob <- function(labels, name=NULL, gp=NULL, vp=NULL) { | |
grob(labels=labels, name=name, gp=gp, vp=vp, cl='foursquare') | |
} | |
preDrawDetails.foursquare <- function(x) { | |
vp <- vpTree( | |
parent=viewport(layout=grid.layout(nrow=2, ncol=2), name='parent'), | |
children=vpList( | |
viewport(layout.pos.row=1, layout.pos.col=1, name='vp1'), | |
viewport(layout.pos.row=1, layout.pos.col=2, name='vp2') | |
) | |
) | |
pushViewport(vp) | |
seekViewport('parent') | |
} | |
drawDetails.foursquare <- function(x, ...) { | |
gs <- gList( | |
rectGrob(height=.5, width=.5, vp='vp1'), | |
textGrob(x$labels[1], vp='vp1'), | |
rectGrob(height=.5, width=.5, vp='vp2'), | |
textGrob(x$labels[2], vp='vp2') | |
) | |
grid.draw(gs) | |
} | |
postDrawDetails.foursquare <- function(x) { | |
popViewport() | |
} | |
grid.draw(foursquareGrob(labels=c('sq1', 'sq2'))) | |
# example 3 (example2, but with parent viewport different from root) | |
graphics.off() | |
little.root <- viewport(height=.5, width=.5, name='root') | |
pushViewport(little.root) | |
grid.draw(foursquareGrob(labels=c('sq1', 'sq2'), vp=little.root)) | |
# example 4 (without explicit viewports) | |
graphics.off() | |
boxGrob <- function(label, name, height=unit(.5, 'npc'), width=unit(.5, 'npc'), gp=NULL, vp=NULL) { | |
grob(label=label, height=height, width=width, gp=gp, vp=vp, cl='box') | |
} | |
drawDetails.box <- function(x, ...) { | |
grid.rect(height=x$height, width=x$width, gp=x$gp) | |
grid.text(label=x$label) | |
} | |
x <- frameGrob(layout=grid.layout(nrow=2, ncol=2)) | |
x <- placeGrob(x, boxGrob(label='sq1', height=.5, width=.5), row=1, col=1) | |
x <- placeGrob(x, boxGrob(label='sq2', height=.5, width=.5), row=1, col=2) | |
grid.draw(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment