ggsave_multiple() extends ggsave to save multiple ggplots to multiple files. In particular, it is suitable for saving multi-page plots created using facet_grid_paginate or facet_wrap_paginate from the ggforce package.

ggplots are saved in separate files. Therefore, the filenames and plots arguments must have the same length.

For multi-page ggplots, the filenames can be explicitly provided using a C integer format expression, such as 'figure%03d.png', to produce successive filenames 'figure001.png', 'figure002.png', etc. If that is not the case, ggsave_multiple() will automatically detect multi-page plots and amend filenames using the format mentioned above. The number of digits used for page identification will depend on the number of pages to be created and will be 2 at the minimum.

ggsave_multiple(
  filenames,
  plots,
  device = NULL,
  path = NULL,
  scale = 1,
  width = NA,
  height = NA,
  units = c("in", "cm", "mm"),
  dpi = 300,
  limitsize = TRUE,
  ...
)

Arguments

filenames

A vector of file names to create on disk.

plots

A list of plots to save.

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).

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.

width, height, units

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

dpi

Plot resolution. Also accepts a string input: "retina" (320), "print" (300), or "screen" (72). Applies only to raster output types.

limitsize

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

...

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

Examples

library(ggplot2)
library(ggforce)

g1 <- ggplot(data = diamonds) +
  aes(x = carat, y = price) +
    geom_point()

g2 <- ggplot(data = diamonds) +
  aes(x = carat, y = price) +
  geom_point() +
  facet_wrap_paginate(vars(clarity), nrow = 2, ncol = 2, page = NULL)

g3 <- ggplot(diamonds) +
  aes(carat, price) +
  geom_point(alpha = 0.2) +
  facet_grid_paginate(color ~ cut, ncol = 3, nrow = 3, page = NULL)

g4 <- ggplot(data = diamonds) +
  aes(x = carat, y = price, color = clarity) +
  geom_point(alpha = 0.2)

g5 <- ggplot(data = diamonds) +
  aes(x = carat, y = price, color = cut) +
  geom_point(alpha = 0.2)

gs <- ggpubr::ggarrange(g1, g4, g5, nrow = 2, ncol = 2)

ggsave_multiple(
  filenames = c('plot_g1.png', 'plot_g2.png', 'plot_g3.png', 'plot_gs.png'),
  plots = list(g1, g2, g3, gs),
  path = tempdir()
)
#> [1/4] Saving 6 x 3.84 in image to '/tmp/RtmpeUNqdr/plot_g1.png'
#> [2/4] Saving 6 x 3.84 in image to '/tmp/RtmpeUNqdr/plot_g2-%02d.png' (2 pages)
#> Warning: A non-standard plot layout was applied in plot #3: 3x3. Default plot dimensions may not be suitable for this case. Consider providing width and height.
#> [3/4] Saving 6 x 7.68 in image to '/tmp/RtmpeUNqdr/plot_g3-%02d.png' (6 pages)
#> [4/4] Saving 6 x 3.84 in image to '/tmp/RtmpeUNqdr/plot_gs.png'