Skip to contents

Takes a ggplot and modifies its facet drawing behaviour such that the widths and heights of panels are set by the user.

Usage

force_panelsizes(
  rows = NULL,
  cols = NULL,
  respect = NULL,
  total_width = NULL,
  total_height = NULL
)

Arguments

rows, cols

a numeric or unit vector for setting panel heights (rows) or panel widths (cols).

respect

a logical value. If TRUE, widths and heights specified in "null" units are proportional. If FALSE, "null" units in x- and y-direction vary independently.

total_width, total_height

an absolute unit of length 1 setting the total width or height of all panels and the decoration between panels. If not NULL, rows and cols should be numeric and not units.

Value

A forcedsize S3 object that can be added to a plot.

Details

Forcing the panel sizes should in theory work regardless of what facetting choice was made, as long as this function is called after the facet specification. Even when no facets are specified, ggplot2 defaults to the ggplot2::facet_null() specification; a single panel. force_panelsizes works by wrapping the original panel drawing function inside a function that modifies the widths and heights of panel grobs in the original function's output gtable.

When rows or cols are numeric vectors, panel sizes are defined as ratios i.e. relative "null" units. rows and cols vectors are repeated or shortened to fit the number of panels in their direction. When rows or cols are NULL, no changes are made in that direction.

When respect = NULL, default behaviour specified elsewhere is inherited.

No attempt is made to guarantee that the plot fits the output device. The space argument in ggplot2::facet_grid() will be overruled. When individual panels span multiple rows or columns, this function may not work as intended.

Examples

ggplot(mtcars, aes(disp, mpg)) +
  geom_point() +
  facet_grid(vs ~ am) +
  force_panelsizes(rows = c(2, 1),
                   cols = c(2, 1))