ggsave_and_print: Save and/or Print Plot

Description Usage Arguments Examples

View source: R/ggplot2_utils.R

Description

A thin wrapper around ggplot2::ggsave() that takes a plot as the first argument and filename as the second (the reverse of ggplot2::ggsave()), but that doesn't save the plot when called interactively. Works best inside R Markdown documents, allowing you to view the figure when running the code chunk interactively but ensure that a specific copy of the figure is saved to a specific directory during the markdown rendering.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
ggsave_and_print(
  plot,
  filename,
  width = 20,
  height = 15,
  device = fs::path_ext(filename),
  include = knitr::opts_current$get("include") %||% TRUE,
  ...,
  force_save = FALSE,
  base_dir = getwd()
)

Arguments

plot

Plot to save, defaults to last plot displayed.

filename

File name to create on disk.

width

Plot size in units ("in", "cm", or "mm"). If not supplied, uses the size of current graphics device.

height

Plot size in units ("in", "cm", or "mm"). If not supplied, uses the size of current graphics device.

device

Device to use. Can either be a device function (e.g. png()), or one of "eps", "ps", "tex" (pictex), "pdf", "jpeg", "tiff", "png", "bmp", "svg" or "wmf" (windows only).

include

Mimics the knitr include chunk option. If FALSE then the plot is saved but not printed. If TRUE, then the plot is saved and printed.

...

Additional arguments passed on to ggplot2::ggsave().

force_save

If TRUE the plot is saved, even if inside an interactive session. The default is FALSE, meaning that plots are not saved when in interactive mode.

base_dir

Save figures into a specified directory

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
## Not run: 
iris %>%
{
  ggplot(.) +
    aes(Sepal.Length, Sepal.Width, color = Species) +
    geom_point()
} %>%
ggsave_and_print("iris_plot.png", width = 10, height = 6)

## End(Not run)

GerkeLab/grkmisc documentation built on Feb. 23, 2020, 6:50 a.m.