Text alignment and font size in gtable

独自空忆成欢 提交于 2019-11-29 16:04:40

It would be useful to review how the grid system works, in particular look at ?grid.text. The grid system saves its plot object in 'grobs' (graphical-objects). I discovered a bit to my surprise that the "justification" is backwards to what I expected (and it is referenced to the centerline rather than the edges of a cell). If you run this code on the "g"-object from the page you cited you might be expecting the text to move to the right, while it actually moves to the left.

g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)] <- 
      lapply(g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)], 
             function(x) modifyList( x, list(just="right") ) ) 
grid.draw(g)

In order to change the font you would need to set gp node of the "x" argument to a be a list with a different structure (different than just an empty list). See `?gpar' for the arguments it accepts:

str(g$grobs[[6]])
List of 11
 $ label        : chr "5.1"
 $ x            :Class 'unit'  atomic [1:1] 0.5
  .. ..- attr(*, "unit")= chr "npc"
  .. ..- attr(*, "valid.unit")= int 0
 $ y            :Class 'unit'  atomic [1:1] 0.5
  .. ..- attr(*, "unit")= chr "npc"
  .. ..- attr(*, "valid.unit")= int 0
 $ just         : chr "centre"
 $ hjust        : chr "left"
 $ vjust        : NULL
 $ rot          : num 0
 $ check.overlap: logi FALSE
 $ name         : chr "GRID.text.1032"
 $ gp           : list()
  ..- attr(*, "class")= chr "gpar"
 $ vp           : NULL


g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)] <- 
           lapply(g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)], 
                function(x) modifyList( x, list(gp=list(cex=0.8) ) ) )
grid.newpage()
grid.draw(g)

Or use the fontsize argument:

g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)] <- 
      lapply(g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)], 
          function(x) modifyList( x, list(gp=list(fontsize=14, cex=1) ) ) )
grid.newpage()
grid.draw(g)

To change the justification for the gtable object one can "adjust" the $x element within the grob:

g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)] <- 
   lapply(g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)], 
     function(z) modifyList( z, list(x=unit(0.1,"npc"), just="left") ) )
g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)] <- 
   lapply(g$grobs[c(6:8, 10:12, 14:16, 18:20, 22:24)], 
     function(x) modifyList( x, list(gp=list(fontsize=16, cex=1) ) ) )
grid.draw(g)

I've added a table function in gtable, with more options.

It's really slow, unfortunately (and unsurprisingly for grid graphics).

require(gtable)

d <- head(iris, 3)

core <- gtable_table(d,
                     fg.par = list(col=1:8, hjust=0, x=0.1),
                     bg.par = list(fill=9:15, alpha=0.5))

colhead <- gtable_table(t(colnames(d)), fg.par = list(fontface=4),
                        bg.par = list(col=NA))

rowhead <- gtable_table(c("", rownames(d)), fg.par = list(fontface=3),
                        bg.par = list(col=NA))

g <- rbind(colhead, core)
g <- cbind(rowhead, g)

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