Save a 'ggplot2' Plot using '+' Operator

Provides ggsnap(), which saves a 'ggplot2' plot to disk in a '+' chain, acting as a thin, chainable wrapper around ggplot2::ggsave(). Can be called multiple times in a chain to save different snapshots.


ggsnap

CRANstatus CRANdownloads

Save a ggplot2 plot using + instead of breaking out of the pipe for a separate ggsave() call.

library(ggplot2)
library(ggsnap)

mtcars |>
  ggplot(aes(mpg, wt)) +
  geom_point() +
  labs(title = "Cars") +
  ggsnap("cars.png", width = 6, height = 4)

No more splitting a plot-building chain into an assignment plus a separate ggsave() call:

# Without ggsnap
p <- mtcars |>
  ggplot(aes(mpg, wt)) +
  geom_point() +
  labs(title = "Cars")
ggsave("cars.png", p, width = 6, height = 4)

# With ggsnap
mtcars |>
  ggplot(aes(mpg, wt)) +
  geom_point() +
  labs(title = "Cars") +
  ggsnap("cars.png", width = 6, height = 4)

ggsnap() accepts the same arguments as ggplot2::ggsave(), and ggsave_snap() is provided as an alias for discoverability.

Installation

# Install from CRAN
install.packages("ggsnap")

# Install latest development version
remotes::install_github("orgadish/ggsnap")

Multiple snapshots

Since the plot is returned invisibly, ggsnap() can also be called more than once in a chain, to save intermediate states:

mtcars |>
  ggplot(aes(mpg, wt)) +
  geom_point() +
  ggsnap("base.png") +
  theme_minimal() +
  ggsnap("minimal.png")

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("ggsnap")

0.1.2 by Or Gadish, 3 months ago


Browse source code at https://github.com/cran/ggsnap


Authors: Or Gadish [aut, cre, cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports ggplot2

Suggests testthat, png, rlang, glue, spelling


See at CRAN