How to use gtable_add_grob() does not 'add'

1.7k views Asked by At

Why does the following plot not display the numbers (g; specified via textGrob(label=g))) in the 6 panels? If I use the text grob only, this also works, but a text grob and a rectangular grob seem to be not so easy. Unfortunately the help page of gtable_add_grob does not give a lot of help...

require(gtable)
base <- gtable(widths=unit(rep(1, 2), "null"),
               heights=unit(rep(1, 3), "null"))
g <- 1
for(i in 1:3) {
    for(j in 1:2) {
        base <- gtable_add_grob(base, list(rectGrob(gp=gpar(fill="#FF000088")), textGrob(label=g)), i, j)
        g <- g+1
    }
}
grid.draw(base)
1

There are 1 answers

1
Roland On BEST ANSWER

The grobs need different names:

base <- gtable_add_grob(base, 
          list(rectGrob(gp=gpar(fill="#FF000088")), textGrob(label=g)), i, j,
          name=1:2)