gtable is a layout engine build on top of the grid package. It is used to abstract away the creation of (potentially nested) grids of viewports into which graphic objects can be placed. The use of gtable makes it easy to ensure alignment of graphic elements as well as building up complex graphic compositions in a piecemeal fashion. gtable is the layout engine powering ggplot2 and is thus used extensively by many plotting functions in R without being called directly.

Installation

You can install the released version of gtable from CRAN with:

or use the remotes package to install the development version from GitHub

Example

As described above, ggplot2 uses gtable for laying out the plot, and it is possible to access the gtable representation of a plot for inspection and modification:

As can be seen, a gtable object is a collection of graphic elements along with their placement in the grid and the dimensions of the grid itself. graphic elements can span multiple rows and columns in the grid and be gtables themselves allowing for very complex automatically arranging layouts.

A gtable object is itself a grob, and can thus be drawn using standard functions from the grid package:

While most people will interact with gtable through ggplot2, it is certainly possible to use it to build a plot up from the ground.