How to adjust the Font Size of tableGrob
Asked Answered
K

2

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.

Kwan answered 2/8, 2015 at 21:1 Comment(1)
related to this questionDeclinature
P
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.

Prolactin answered 2/8, 2015 at 21:28 Comment(5)
Thank you Bryan, it worked like a charm. Appreciate your help and link to the examples.Kwan
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.Prolactin
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.Kwan
the wiki 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.Declinature
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?Kwan
S
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) ))
Sybil answered 9/1, 2019 at 15:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.