R Grid

Posted onby admin

Welcome the R graph gallery, a collection of charts made with the R programming language. Hundreds of charts are displayed in several sections, always with their reproducible code available. The gallery makes a focus on the tidyverse and ggplot2. Feel free to suggest a chart or report a bug; any feedback is highly welcome. For example, in a two-column grid, relwidths = c(2, 1) would make the first column twice as wide as the second column. Relheights (optional) Numerical vector of relative rows heights.


Easy multi-panel plots in R using facetwrap and facetgrid from ggplot2 Posted on April 2, 2019 by sandy haaf 4 Comments One of the most powerful aspects of the R plotting package ggplot2 is the ease with which you can create multi-panel plots. Welcome to National Grid, providing New York, Rhode Island and Massachusetts with natural gas and electricity for homes and businesses.

First published: 2020/04/13

One of the main downsides to the ‘grid’ graphics package is thatit is slow. And that makes some important packages that depend on ‘grid’,like ‘ggplot2’,slow. For example, the scatterplots shown below are roughlyequivalent, but one is drawnusing ‘graphics’ and the other using ‘ggplot2’.

The ‘ggplot2’ version takes more than 4 times as long to draw.

(The benchmarks in this post were produced using the ‘bench’ packagewithin a Docker containerbased on rocker/r-devel,but with R-devel (r77995) built with--enable-memory-profiling and a bunch of R packages installed;the Docker images, pmur002/grid-new-units-r-3.6.3 andpmur002/grid-new-units-r-devel, are available from DockerHub.)

Thomas Lin Pedersen identified that, intypical ‘ggplot2’ usage, a significant amount of timewas being spent creating and manipulating ‘grid’ “unit” objectsand this has lead to a change inthe internal implementation of “units” in the ‘grid’ graphics packagefor R version 4.0.0.

On one hand, this is not news, because the public behaviour of ‘grid’ unitshas not changed at all. However, there are two importantconsequences of this change: one for users and one for developers.

For users, the reason for making the change was speed; with the new ‘grid’units, certain operations go a lot faster (an order of magnitude or morein many cases). For example, the following code that justcreates unit objects is up to tentimes faster with the new unit implementation.

Although manipulating units is only a fraction of what packages like ‘ggplot2’do, the impact of the unit speed upis sufficient to be noticeable in the production of ‘ggplot2’ plots.The plots below include examples of both simple and complex ‘ggplot2’ plots.

The following timings show that the new unit implementation in ‘grid’can translate to a 10%-20% speed-up in ‘ggplot2’ plots.

For developers, the impact of the changes to ‘grid’ units should beneutral, but theycan be disastrous if apackage has been peeking and poking at the internal implementationof ‘grid’ units.

R Grid

We believe that we have identified most of these cases and thatmost of those have now been fixed. In case some problems have notyet come to light, the following known problems and solutions maybe helpful:

  • it is possible for a package to contain a saved R object that containsold-style ‘grid’ units. There are protections in the new ‘grid’implementation to upgrade such objects to new-style units or, at worst,generate an error. Recreating the saved R object should hopefullyresolve any issues.

  • several packages were extracting attributes from “unit” objects, e.g.,the 'mm' from unit(1, 'mm'); there is a new grid::unitType()function that may help packages to avoid accessing ‘grid’ unit internals in thefuture.


There was also a small lie earlier: the public behaviour of ‘grid’ unitshas actuallychanged a little because the printing of some units is now different.For example, the following code and output shows that arithmeticon units produces a different printed result.

The original design and implementation of new units was contributedby Thomas Lin Pedersen. Paul Murrell contributed minor fixesand features and lead the testing, diagnosis, and remedyingof problems in packages. Paul Murrell’s contribution was partiallysupported by a donation from R Studio to The University of AucklandFoundation.Both authors would like to acknowledge the patience and supportof the CRAN team and the cooperation of the authors of thenumerous package that were affected by these changes.


Arrange multiple plots into a grid.



List of plots to be arranged into the grid. The plots can be any objects thatthe function as_gtable() can handle (see also examples).

(optional) List of plots to display. Alternatively, the plots can be providedindividually as the first n arguments of the function plot_grid (see examples).

(optional) Specifies whether graphs in the grid should be horizontally ('h') orvertically ('v') aligned. Options are 'none' (default), 'hv' (align in both directions), 'h', and 'v'.

(optional) Specifies whether graphs should be aligned by the left ('l'), right ('r'), top ('t'), or bottom ('b')margins. Options are 'none' (default), or a string of any combination of l, r, t, and b in any order (e.g. 'tblr' or 'rlbt' for aligning all margins).Must be specified if any of the graphs are complex (e.g. faceted) and alignment is specified and desired. See align_plots() for details.

(optional) Number of rows in the plot grid.

(optional) Number of columns in the plot grid.

(optional) Numerical vector of relative columns widths. For example, in a two-columngrid, rel_widths = c(2, 1) would make the first column twice as wide as thesecond column.

(optional) Numerical vector of relative rows heights. Works just asrel_widths does, but for rows rather than columns.

(optional) List of labels to be added to the plots. You can also set labels='AUTO' toauto-generate upper-case labels or labels='auto' to auto-generate lower-case labels.

(optional) Numerical value indicating the label size. Default is 14.

(optional) Font family of the plot labels. If not provided, is taken from the current theme.

(optional) Font face of the plot labels. Default is 'bold'.

(optional) Color of the plot labels. If not provided, is taken from the current theme.

R Gridsvg

(optional) Single value or vector of x positions for plot labels, relative to each subplot.Defaults to 0 for all labels. (Each label is placed all the way to the left of each plot.)

(optional) Single value or vector of y positions for plot labels, relative to each subplot.Defaults to 1 for all labels. (Each label is placed all the way to the top of each plot.)

Adjusts the horizontal position of each label. More negative values move the label furtherto the right on the plot canvas. Can be a single value (applied to all labels) or a vector of values(one for each label). Default is -0.5.

Adjusts the vertical position of each label. More positive values move the label furtherdown on the plot canvas. Can be a single value (applied to all labels) or a vector of values(one for each label). Default is 1.5.

Individual number or vector of numbers greater than 0. Enables you to scale the size of all orselect plots. Usually it's preferable to set margins instead of using scale, but scale cansometimes be more powerful.

(optional) How should margins be adjusted during alignment. See align_plots() for details.

Logical value indicating if the plots should be arrange by row (default) or by column.

R Grid Primary Arms

Deprecated. Use ncol.

Deprecated. Use nrow.

R Grid Pdf