Compute ggsave() dimensions for a fixed panel content area
Source: R/ggsave-dims.R
hv_ggsave_dims.RdGiven a fitted ggplot, returns width and height values such that, when
passed to ggplot2::ggsave(), the resulting file has a panel content area
matching width / height. The "panel content area" is the rectangular
bounding box of the gtable cells tagged panel — i.e., the smallest
rectangle that encloses every plotting panel. Whatever grobs fall inside
that rectangle (e.g., inter-panel gutters and strip rows that sit between
facet rows) are counted as part of the target; everything outside it
(axes, axis titles, plot title, caption, legend, plot margins, and any
strips that sit above/below or beside the panel block such as
facet_grid side strips) is counted as chrome.
Usage
hv_ggsave_dims(plot, width, height, units = c("in", "cm", "mm"))Value
A named list with elements width, height, units — shaped to
splat directly into ggplot2::ggsave() via do.call() (see examples).
Details
Useful for multi-panel figure sets where a constant data region is
required across PDFs regardless of label length or legend placement.
Always measured with a PDF sizing device, which is why units is limited
to length (inches, cm, mm) — DPI is irrelevant for vector output.
Examples
if (FALSE) { # \dontrun{
p <- ggplot2::ggplot(mtcars, ggplot2::aes(hp, mpg)) +
ggplot2::geom_point() +
ggplot2::labs(title = "Long title that eats vertical space",
x = "Horsepower", y = "Miles per gallon")
dims <- hv_ggsave_dims(p, width = 4, height = 3)
do.call(
ggplot2::ggsave,
c(list(filename = "fig.pdf", plot = p), dims)
)
} # }