Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
738 views
in Technique[技术] by (71.8m points)

r - Text alignment and font size in gtable

My question relates to the answer by Baptiste that you can find here: https://stackoverflow.com/a/18667413/2072440.

That code does pretty what I want it to do. I would like to make two modifications. First, I would like to align the text in the first column to the left. Second, I would like to change the font of the text.

I have tried to edit the line that describes the fill for the cells' background but that does not seem to impact the font and it only impacts the position of the boxes itself rather than the text in them.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...