Package ‘patchwork’

Operators defined in package ‘patchwork’ can be used to combine multiple plots created with package ‘ggplot2’ or extensions to it, such as my own ‘ggpmisc’ and ‘ggspectra’. (The site ggplot2 extensions showcases at the moment more than 40 extensions to ‘ggplot2’.)

Package ‘patchwork’ has been developed by Thomas Lin Pedersen, and being relatively new, it is not yet in CRAN. It can be installed from the public Git repository at Github.

# install.packages("devtools")
devtools::install_github("thomasp85/patchwork")

The ‘ggplot2’ package provides a strong API for sequentially building up a plot, but does not concern itself with composition of multiple plots. ‘patchwork’ is a package that expands the API to allow for arbitrarily complex composition of plots by providing mathmatical operators for combining multiple plots. Other packages that address this need (but with a different approach) are ‘gridExtra’ and ‘cowplot’.

From the package’s DESCRIPTION.

Package ‘ggspectra’ provides function multiplot() for this purpose, but this function is minimalistic, with its most important handicap in its inability to independently align the plotting areas and axis labels of the composed plots.

An example using the operators / and & from ‘patchwork’ follows.

library(ggspectra)
library(patchwork)
p <- autoplot(sun.spct) / 
  autoplot(polyester.spct, range = c(280, 800)) / 
  autoplot(sun.spct * polyester.spct)
png("three-plots.png", height = 800, width = 600)
print(p) & theme_bw()
dev.off()

from Data to Viz (external link)

from data to Viz is a new web site related to data analysis and R. Its aim is to make it easier to choose among different types of data visualisations. It looks beautiful, is easy to navigate, includes “trees” displaying a classification of visualizations and multiple individual examples with the corresponding R code.  Highly recommended!

To access the website and/or to buy the printed poster visit from Data to Viz.

 

Update

ggpmisc 0.2.7.9001 (Preview)

Improve the automatic positioning of labels generated by stat_poly_eq(), most notably when grouping is in effect and with facets with free scales.  The values passed as argument to the new parameters label.x.npc and label.y.npc are in normalized parent coordinates with fractional values relative to the total dimension of the x and y scales. Character strings “right”, “center” and “left” for x and “top”, “center”, and “bottom” for y are also recognized. The behaviour of parameters label.x and label.y remains unchanged and they override any arguments passed through label.x.npc and label.y.npc. However, passing absolutes coordinates through the old parameters will be rarely needed.

Add stat_fit_deviations() for easily highlighting residuals in plots of fitted models, and stat_fit_residuals() for easily plotting residuals in plots matching plots of “lm” fits plotted with stat_smooth() even with grouping or facets. At the moment these two stats support lm() fits only.

Add stat_fit_glance() which uses package broom::glance() to produce a one-row data frame summarizing the model fit object. This gives maximum flexibility in model function choice at the expense of very frequently having to set aesthetics explicitly.

Add preliminary version of stat_fit_augment() which uses broom::augment() to enhance the data frame received as input with variables related to a model fit. At the moment functionality is very limited.

NOTE: This version of the package is now available from the R and R-test repositories at this site.

Back to Top