How to use gtable_add_grob() does not 'add'

此生再无相见时 提交于 2019-12-23 18:01:05

问题


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:


The grobs need different names:

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


来源:https://stackoverflow.com/questions/18028920/how-to-use-gtable-add-grob-does-not-add

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!