Skip to contents

Convert Base R and Lattice drawings and Grid objects into native patchwork elements. Existing ggplot and patchwork objects pass through directly. Combine the result with patchwork's +, |, and / operators or ggassemble().

Usage

as_forma(x, width = 5, height = 4, clip = TRUE)

Arguments

x

A ggplot, patchwork, Grid grob or gtable, Lattice trellis, same-session Base recordedplot, one-sided Base drawing formula, or function called with zero arguments that draws on the current device.

width, height

Positive capture dimensions in inches for Base and Lattice. Choose values close to the child's intended output size when labels or margins depend on the drawing dimensions.

clip

Should patchwork clip the wrapped graphic to its allocated area? Has no effect on native ggplot or patchwork inputs.

Value

The original ggplot or patchwork, or a patchwork wrapped element.

Details

Formula and function callbacks run once, at conversion time. They should draw one page using Base graphics on the supplied device. Keep device management outside the callback. A Base multi-panel page may be captured as one element. For a recording, use grDevices::recordPlot() from a device with display-list recording enabled in the same R session. Base capture requires the optional 'gridGraphics' package.

Capture runs on a temporary in-memory PDF device. Wrapped graphics keep their own axes, styling and guides. Patchwork can style the wrapper and arrange its outer area. Theme propagation, guide collection and axis collection can use the structure of native ggplots. Manual styling of the original Base or Lattice plot belongs before capture.

Examples

p <- ggplot2::ggplot(mtcars, ggplot2::aes(wt, mpg)) + ggplot2::geom_point()
g <- as_forma(grid::textGrob("Fuel economy and vehicle weight"))
g / p


if (requireNamespace("gridGraphics", quietly = TRUE)) {
  as_forma(~ graphics::hist(mtcars$mpg)) | p
}