Skip to content

Instantly share code, notes, and snippets.

@baptiste
Created February 26, 2012 08:42
Show Gist options
  • Save baptiste/1915307 to your computer and use it in GitHub Desktop.
Save baptiste/1915307 to your computer and use it in GitHub Desktop.
alignment of gtables
library(gtable)
gcore <- gtable_matrix(name="core",
grobs=matrix(list(rectGrob(gp=gpar(fill="blue",alpha=0.5))),1,1),
widths=unit(5,"cm"), heights=unit(3,"cm"))
gtop <- gtable_matrix(name="top",
grobs=matrix(list(rectGrob(gp=gpar(fill="red",alpha=0.5))),1,1),
widths=unit(5,"cm"), heights=unit(1,"cm"))
gleft <- gtable_matrix(name="left",
grobs=matrix(list(rectGrob(gp=gpar(fill="green",alpha=0.5))),1,1),
widths=unit(1,"cm"), heights=unit(3,"cm"))
align_table <- function(core, top, left, just=c("center", "center")){
gl <- grid.layout(nrow=2,ncol=2,
widths=unit.c(sum(left$widths), sum(core$widths)),
heights=unit.c(sum(top$heights), sum(core$heights)),
just=just)
g1 <- gtable_gTree(core, vp=viewport(layout.pos.row=2, layout.pos.col=2))
g2 <- gtable_gTree(top, vp=viewport(layout.pos.row=1, layout.pos.col=2))
g3 <- gtable_gTree(left, vp=viewport(layout.pos.row=2, layout.pos.col=1))
gTree(children=gList(g1,g2,g3), vp=viewport(layout=gl))
}
grid.newpage()
g <- align_table(gcore,gtop,gleft)
grid.draw(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment