save_png: Save a plot as a .png

Description Usage Arguments Value Examples

View source: R/save_png.R

Description

A wrapper for calling grDevices::png() then grDevices::dev.off(). Allows users to save a plot to a specified path. The preferred method for ggplot2::ggplot objects remains ggplot::ggsave(). However if you wish to use this function to save ggplot2::ggplot objects, print the plot as the last step of plot_code.

Usage

1
save_png(plot_code, path, width = 8, height = 6, units = "in", res = 600, ...)

Arguments

plot_code

code required to generate the plot, sandwiched in curly braces.

path

character(1) specifying path for where to save your plot, including file name.

width

the width of the device.

height

the height of the device.

units

The units in which height and width are given. Can be px (pixels, the default), in (inches), cm or mm.

res

The nominal resolution in ppi which will be recorded in the bitmap file, if a positive integer. Also used for units other than the default, and to convert points to pixels.

...

additional arguments are passed to grDevices::png().

Value

Invisibly, the path to saved plot.

Examples

1
2
3
4
5
6
save_png(
  {
    plot(x = 1:5)
  },
  path = file.path(tempdir(), "test_plot.png"),
)

dzhang32/rutils documentation built on Feb. 10, 2022, 9:12 p.m.