Save a forest plot to a file

Description

Writes the plot produced by forrest() (or any base-R plotting code) to a file. The graphics device is inferred from the file extension.

Usage

save_forrest(file, plot, width = 7, height = 5, dpi = 300, bg = "white")

Arguments

file Output file path. Supported extensions: .pdf, .png, .svg, .tiff.
plot A zero-argument function whose body calls forrest(). Evaluated inside the open graphics device.
width Plot width in inches. Default 7.
height Plot height in inches. Default 5.
dpi Resolution in dots per inch for raster formats (.png, .tiff). Ignored for vector formats (.pdf, .svg). Default 300.
bg Background colour. Default “white”.

Value

Invisibly returns file.

Examples

library("forrest")

dat <- data.frame(
  label    = c("Age (per 10 y)", "Female sex", "Current smoker"),
  estimate = c(0.42, -0.38, -0.31),
  lower    = c(0.22, -0.56, -0.51),
  upper    = c(0.62, -0.20, -0.11)
)
tmp <- tempfile(fileext = ".pdf")
save_forrest(tmp, function() {
  forrest(
    dat,
    estimate = "estimate",
    lower    = "lower",
    upper    = "upper",
    label    = "label",
    xlab     = "Regression coefficient (95% CI)"
  )
})