Skip to contents

This function takes a plot or list of plots and writes them to a (multipage) file.

Usage

save_plot(
  plot = ggplot2::last_plot(),
  filename,
  width = NA,
  height = NA,
  units = c("mm", "cm", "in"),
  multiple_files = FALSE,
  bg = "transparent",
  ...
)

Arguments

plot

Plot to save, defaults to last plot displayed.

filename

File name to create on disk.

width, height

Dimensions of the saved plot. Defaults to NA.

units

Units of length. Defaults to "mm".

multiple_files

Whether to save multiple pages as individual files.

bg

Background colour. If NULL, uses the plot.background fill value from the plot theme.

...

Other arguments passed on to the graphics device function, as specified by device.

Value

A tidyplot object

Details

Handling of file dimensions. Output file dimensions are determined according the the following precedence.

  1. The width and height arguments.

  2. Dimensions inferred from an incoming plot object with absolute dimensions.

  3. System default device dimensions.

Examples

if (FALSE) {

# Save plot to file
study %>%
  tidyplot(treatment, score) %>%
  add_data_points() %>%
  save_plot("single_plot.pdf")

# Save multipage PDF file
gene_expression %>%
  tidyplot(group, expression, color = sample_type) %>%
  add_data_points() %>%
  split_plot(by = external_gene_name, nrow = 3, ncol = 3) %>%
  save_plot("multipage_plot.pdf")

# Save multiple PDF files
gene_expression %>%
  tidyplot(group, expression, color = sample_type) %>%
  add_data_points() %>%
  split_plot(by = external_gene_name, nrow = 3, ncol = 3) %>%
  save_plot("plot.pdf", multiple_files = TRUE)

# Save intermediate stages to file
study %>%
  tidyplot(x = treatment, y = score, color = treatment) %>%
  add_mean_bar(alpha = 0.4) %>%
  add_sem_errorbar() %>%
  add_data_points_beeswarm() %>%
  save_plot("before.pdf") %>%
  adjust_colors(colors_discrete_seaside) %>%
  save_plot("after.pdf")

  }