Skip to contents

Tests whether the plot region of the active graphics device meets a minimal size requirement. This is useful before drawing into small devices or heavily subdivided layouts (mfrow, layout(), Shiny render panes), where an undersized region would otherwise fail with the rather cryptic error "figure margins too large".

Usage

isValidPlotRegion(minPin = .getOption("plot.minPin", c(0.5, 0.5)))

Arguments

minPin

numeric vector of length 2, giving the minimal required width and height of the plot region in inches. Defaults to the option DescToolsX.plot.minPin and falls back to c(0.5, 0.5) if the option is not set.

Value

a single logical: TRUE if the plot region is at least minPin inches in both dimensions (or if no device is open), FALSE otherwise.

Details

The plot region is the area inside the figure margins, as reported by par("pin"). Its size results from the device dimensions minus the current margins (mar, oma), so both a small device and oversized margins can render it degenerate.

If no graphics device is open (dev.cur() == 1), the function returns TRUE, since the next high-level plot call will open a fresh device with default dimensions. Note that querying par() in this state would itself open a device as a side effect, which this function deliberately avoids.

The function has no side effects and never throws; it is meant as a guard for conditional plotting, e.g. if (isValidPlotRegion()) plot(x) else message("device too small").

See also

par() (entries pin, fin, mar), dev.cur()

Other graphics.layout: abcCoords(), axTicks, axisBreak(), lineToUser(), mar(), plotFacet(), spreadOut()

Examples

# guard a plot against a degenerate region
if (isValidPlotRegion()) {
  plot(rnorm(20))
} else {
  message("plot region too small")
}


# require a larger region, e.g. for a wide correlation plot
isValidPlotRegion(minPin = c(4, 3))
#> [1] TRUE