Package 'ggsnap'

Title: Save a 'ggplot2' Plot using '+' Operator
Description: 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.
Authors: Or Gadish [aut, cre, cph]
Maintainer: Or Gadish <[email protected]>
License: MIT + file LICENSE
Version: 0.1.2
Built: 2026-07-17 07:52:50 UTC
Source: https://github.com/orgadish/ggsnap

Help Index


Save a ggplot2 plot using the + operator

Description

ggsnap() saves a ggplot2 plot to disk inside a + chain, rather than requiring a separate ggplot2::ggsave() call. It accepts the same arguments as ggplot2::ggsave().

The plot is returned invisibly so the chain can continue — meaning ggsnap() can be used multiple times in a single chain to capture intermediate states.

ggsave_snap() is an alias provided for discoverability.

Usage

ggsnap(
  filename,
  width = NA,
  height = NA,
  units = c("in", "cm", "mm", "px"),
  dpi = 300,
  ...
)

ggsave_snap(
  filename,
  width = NA,
  height = NA,
  units = c("in", "cm", "mm", "px"),
  dpi = 300,
  ...
)

Arguments

filename

File name to create on disk.

width, height

Plot size in units expressed by the units argument. If not supplied, uses the size of the current graphics device.

units

One of the following units in which the width and height arguments are expressed: "in", "cm", "mm" or "px".

dpi

Plot resolution. Also accepts a string input: "retina" (320), "print" (300), or "screen" (72). Only applies when converting pixel units, as is typical for raster output types.

...

Arguments passed on to ggplot2::ggsave

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). If NULL (default), the device is guessed based on the filename extension.

path

Path of the directory to save plot to: path and filename are combined to create the fully qualified file name. Defaults to the working directory.

scale

Multiplicative scaling factor.

limitsize

When TRUE (the default), ggsave() will not save images larger than 50x50 inches, to prevent the common error of specifying dimensions in pixels.

bg

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

create.dir

Whether to create new directories if a non-existing directory is specified in the filename or path (TRUE) or return an error (FALSE, default). If FALSE and run in an interactive session, a prompt will appear asking to create a new directory when necessary.

Value

The ggplot object, invisibly.

Examples

library(ggplot2)

save_dir <- tempdir()

# Single use
ggplot(mtcars) +
  aes(mpg, wt) +
  geom_point() +
  labs(title = "Cars") +
  ggsnap(file.path(save_dir, "cars.png"), width = 6, height = 4)

# Two snapshots capturing before and after changing the theme
ggplot(mtcars) +
  aes(mpg, wt) +
  geom_point() +
  labs(title = "Cars") +
  ggsnap(file.path(save_dir, "base.png")) +
  theme_minimal() +
  ggsnap(file.path(save_dir, "minimal.png"))