library(grid)
library(devtools)
load_all("~/Dropbox/dev/R/gtable", TRUE)
## Loading gtable
opts_chunk$set(tidy = FALSE)

just a frame

gtf <- gtable_matrix("gtf", matrix(rep(list(rectGrob(gp = gpar(fill = "grey95"))), 4), nrow = 2), 
                     widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))

default

grid.newpage()
grid.draw(gtf)

gs <- lapply(1:4, function(x) rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
                                       gp = gpar(fill = rainbow(4)[x])))
gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))
grid.draw(gt)

plot of chunk unnamed-chunk-3


justification by grob setting

grid.newpage()
grid.draw(gtf)

gs <- lapply(1:4, function(x) rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
                                       y = 0, just = "bottom",
                                       gp = gpar(fill = rainbow(4)[x])))
gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))
grid.draw(gt)

plot of chunk unnamed-chunk-4


justification by viewport

grid.newpage()
grid.draw(gtf)

gs <- lapply(1:4, function(x) 
  rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
           gp = gpar(fill = rainbow(4)[x]),
           vp = viewport(y = 0, just = "bottom", height = unit(x/2, "cm"))))
gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))
gtgt <- gtable_gTree(gt)
grid.draw(gt)

plot of chunk unnamed-chunk-5


justification by wrapping by gTree

we need to set the size of viewport

grid.newpage()
grid.draw(gtf)

gs <- lapply(1:4, function(x) 
  gTree(children = gList(rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
                                  gp = gpar(fill = rainbow(4)[x]),
                                  vp = viewport(height = unit(x/2, "cm"), y = 0, just = "bottom")))))

gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))
grid.draw(gt)

plot of chunk unnamed-chunk-6


gtable of gtable – default

grid.newpage()
grid.draw(gtf)

gsc <- lapply(1:4, function(x) rectGrob(width = unit(1, "cm"), height = unit(1, "cm"),
                                        gp = gpar(fill = rainbow(4)[x])))
gtc <- gtable_matrix("gtc", matrix(gsc, nrow = 2),
                     widths = unit(rep(1, 2), "cm"), heights = unit(rep(1, 2), "cm"))
gtcgt <- gtable_gTree(gtc)

gs <- lapply(1:3, function(x) rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
                                       gp = gpar(fill = rainbow(4)[x])))
gs[[4]] <- gtcgt
gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))

grid.draw(gt)

plot of chunk unnamed-chunk-7


gtable of gtable – justification

grid.newpage()
grid.draw(gtf)

gsc <- lapply(1:4, function(x) rectGrob(width = unit(1, "cm"), height = unit(1, "cm"),
                                        gp = gpar(fill = rainbow(4)[x])))
gtc <- gtable_matrix("gtc", matrix(gsc, nrow = 2),
                     widths = unit(rep(1, 2), "cm"), heights = unit(rep(1, 2), "cm"))
gtcgt <- gtable_gTree(gtc, vp = viewport(y = 0, just = "bottom", height = sum(gtc$heights)))

gs <- lapply(1:3, function(x) rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
                                       gp = gpar(fill = rainbow(4)[x])))

gs[[4]] <- gtcgt
gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))
grid.draw(gt)

plot of chunk unnamed-chunk-8


gtable of gtable – justification of child and parent

grid.newpage()
grid.draw(gtf)

gsc <- lapply(1:4, function(x) rectGrob(width = unit(1, "cm"), height = unit(1, "cm"),
                                        gp = gpar(fill = rainbow(4)[x])))
gtc <- gtable_matrix("gtc", matrix(gsc, nrow = 2),
                     widths = unit(rep(1, 2), "cm"), heights = unit(rep(1, 2), "cm"))
gtcgt <- gtable_gTree(gtc, vp = viewport(y = 0, just = "bottom", height = sum(gtc$heights)))

gs <- lapply(1:3, function(x) rectGrob(width = unit(x/2, "cm"), height = unit(x/2, "cm"),
                                       gp = gpar(fill = rainbow(4)[x])))

gs[[4]] <- gtcgt
gt <- gtable_matrix("gt", matrix(gs, nrow = 2), 
                    widths = unit(rep(4, 2), "cm"), heights = unit(rep(4, 2), "cm"))
grid.draw(gtable_gTree(gt, vp = viewport(just = "left", x = 0, width = sum(gt$widths))))

plot of chunk unnamed-chunk-9