Why are the lines (via linesGrob
) not drawn in the following plot?
require(gtable)
base <- gtable(widths=unit(rep(1, 2), "null"),
heights=unit(rep(1, 3), "null"))
grid.newpage()
g <- 1
for(i in 1:3) {
for(j in 1:2) {
base <- gtable_add_grob(base,
grobs=list(linesGrob(x=1:4, y=4:1),
rectGrob(gp=gpar(fill="#FF0000")),
textGrob(label=g)), i, j, name=1:3)
g <- g+1
}
}
grid.draw(base)
Two reasons:
the coordinates fall outside the viewport
the rectGrob is drawn on top and masks it
Note
gtable_add_grobs
is vectorised, which means you shouldn't have to use for loops, ideally. It's easier if you group all the grobs together first, for a given cell, in a gTree. Here's a simplified version,