Align data panels across graphics systems

align_forma.RdMatch the physical edges of a single data panel in a Base R drawing,
Lattice plot, or ggplot. Combine the returned plots using patchwork's
+, |, or / operators.
Usage
align_forma(
...,
plots = NULL,
slot,
axis = c("both", "x", "y"),
device = NULL,
insets = NULL,
tolerance = 0.1
)Arguments
- ...
Original graphics or a single unnamed list of graphics. Use a Base formula, callback or same-session recording, a trellis, a ggplot, or a Grid grob. Pass original graphics before wrapping with
as_forma().- plots
Optional list of original graphics.
- slot
Two positive numbers giving each child's width and height in millimetres.
- axis
Edges to match. Use
"x"for left and right edges in a column,"y"for bottom and top edges in a row, or"both"to match all edges relative to the origin of each slot.- device
Optional graphics opener taking named
file,width, andheightarguments, with dimensions in inches. Use the same device and settings for the final output. The opener must leave exactly one newly opened device active. The default measures on a PDF device.- insets
Optional list with one entry per graphic. Use
NULLto measure a panel, or four non-negative numbers for a Grid grob or gtable with known insets in left, right, bottom, top order (mm).- tolerance
Maximum spread between corresponding measured edges, in millimetres.
Value
A list containing plots (wrapped patchwork elements), matrices
before and after (edges in mm relative to each slot's lower-left
corner), and the requested slot, axis and tolerance. Input names
are carried through to the list of plots and to the matrices.
Details
Allocate equal slots along the matched axis and keep their origins aligned. The returned figures are sized in physical units. The allocated cells must fit the requested slots. Export with enough space for patchwork margins, tags and annotations. Re-run alignment if changing the output device, font settings or slot dimensions.
The engine measures and lays out each child at its new viewport size.
Base formulas and callbacks start with compact margins and axis spacing
on the capture device. Settings supplied inside the drawing, including
par(mar = ...) and par(mai = ...), take precedence. Existing
recorded plots keep their original settings. Base callbacks execute
once and the resulting drawing is replayed at each size. Unsupported
Base drawing operations depend on gridGraphics.
An aligned panel narrower or shorter than 15 mm triggers a warning. Increase the slot and output size or reduce the margins on the graphics identified in the warning. Lattice panels may be redrawn while sizing. Automatic identification supports one panel. Use ordinary composition for fixed-aspect ggplots, non-filling Lattice panels and faceted or multi-panel figures. Padding the panels does not change coordinate limits to match across plots.
Examples
p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()
by_weight <- p + ggplot2::scale_y_continuous(labels = function(x) sprintf("%06.2f", x))
aligned <- align_forma(p, by_weight, slot = c(100, 75), axis = "x")
aligned$after
#> left right bottom top
#> [1,] 17.98838 98.06697 11.25738 73.06697
#> [2,] 17.98838 98.06697 11.25738 73.06697
figure <- aligned$plots[[1]] / aligned$plots[[2]]
ggplot2::ggsave(tempfile(fileext = ".pdf"), figure, device = grDevices::pdf,
width = 112, height = 162, units = "mm")