25

Any idea how to adjust the font size of a tableGrob? I am using gridExtra 2.0.0, which seem to be very different previous version 0.9.3.

g <- tableGrob(mtcars)
grid.arrange(g)

enter image description here

I am looking to adjust the font size of the text in the table, header, and rowname as well.

user2103970
  • 703
  • 3
  • 9
  • 15
  • related [to this question](http://stackoverflow.com/questions/22138111/how-do-i-fit-a-very-wide-grid-table-or-tablegrob-to-fit-on-a-pdf-page/31620903#31620903) – baptiste Aug 02 '15 at 22:24

2 Answers2

36

You can do this via themes:

mytheme <- gridExtra::ttheme_default(
    core = list(fg_params=list(cex = 2.0)),
    colhead = list(fg_params=list(cex = 1.0)),
    rowhead = list(fg_params=list(cex = 1.0)))

myt <- gridExtra::tableGrob(mtcars[1:5, 1:5], theme = mytheme)

grid.draw(myt)

There are a number of other examples in browseVignettes("gridExtra") -- look at the tableGrob examples. A great deal of control is possible.

Bryan Hanson
  • 6,055
  • 4
  • 41
  • 78
  • Thank you Bryan, it worked like a charm. Appreciate your help and link to the examples. – user2103970 Aug 02 '15 at 22:00
  • My pleasure. If you really want to do a lot of detailed formatting, check out the `tableGrob` vignette on the Github version. The vignette there has a number of additional examples, especially w/regard to adding lines and boxes to the table. Of course you'd need to install that version to use it. – Bryan Hanson Aug 02 '15 at 22:04
  • That sounds like what I am looking for. I am working on trying to automate a report using a combination of knitr to pdf / rmarkdown / ggplot / gridextra (for tables), so it has been a little challenging with formatting/sizing/positioning/. I'll check out the github version. – user2103970 Aug 02 '15 at 22:13
  • 1
    [the wiki](https://github.com/baptiste/gridextra/wiki/tableGrob) has more examples than the CRAN vignette, although some of them require the dev version to work. I've made it a bit simpler to change the font size globally, with a top-level theme argument. – baptiste Aug 02 '15 at 22:21
  • Bryan and Baptiste, do you know how to adjust the size of the output from grid.arrange. Ever since I started using gridExtra/grid I have noticed the size of the output seem a bit small. I usually have a ggplot chart and a tablegrob in the grid.arrange like this (grid.arrange(ggplotchart, tblTableGrob, ncol=2, nrow=1)) in a pdf using knitr and rmarkdown. The image is small and there still a lots of empty spaces on the edges of the pdf document. I am wondering if you knew how to adjust the image size? Does it have something to do with viewport and I need to expand the size of viewport? – user2103970 Aug 13 '15 at 16:34
10

You can also change the general font size easily with base_size

grid.draw(tableGrob(head(mtcars, n=10),theme=ttheme_minimal(base_size = 5) ))
K. Peltzer
  • 326
  • 3
  • 7