How does vp argument in gridExtra::tableGrob works?

Viewed 87

How does vp argument in gridExtra::tableGrob works? Documentation only states vp: optional viewport and i couldn't find a vignette that does.

1 Answers

A viewport is a fundamental part of the grid graphics system, which allows the user to specify a rectangular portion of the plotting window to draw on as if it were a normal plotting window with normal co-ordinates. This allows us to draw shapes, plots and tables, then scale and rotate them by specifying viewport dimensions. You can read more about viewports by typing ?grid::viewport into your console.

The vp argument of tableGrob allows you to pre-specify a viewport in which to draw the tableGrob. Here's a very simple (and a bit silly) example.

First, we'll load our packages and define some reproducible dummy data:

library(grid)
library(gridExtra)

set.seed(69)

df <- data.frame(Month = month.name, 
                 Value = scales::dollar(runif(12, 1e5, 2e5)))

And we can make a tableGrob like this:

tg1 <- tableGrob(df)

grid.newpage()
grid.draw(tg1)

Now let's do the same thing with a rotated viewport passed to vp:

tg2 <- tableGrob(df, vp = viewport(angle = 45))

grid.newpage()
grid.draw(tg2)

Created on 2020-07-26 by the reprex package (v0.3.0)

Related