JPlotToPNG: Plot to a PNG file

View source: R/plot_to_file.R

JPlotToPNGR Documentation

Plot to a PNG file

Description

Writes the output of a plot to a PNG file. If you are using ggplot, try either using JPlotToPNG(filename, print(<plotting code>)) or else ggsave().

Usage

JPlotToPNG(
  filename,
  plotExpr,
  width = 600,
  height = NA,
  aspectRatio = 3/2,
  units = c("px", "mm", "cm", "in"),
  tryToUseRagg = TRUE,
  type = ifelse(capabilities()["cairo"], "cairo", NULL),
  res = 72,
  onlyIfDoesntExist = FALSE,
  createDirectory = TRUE,
  ...
)

Arguments

filename

The name of the PNG to create or overwrite. If NULL, plot output goes to the current device.

plotExpr

A function or expression which will produce the plot to be written to the file.

width

The width of the output PNG file in units.

height

The height of the output PNG file in units. Defaults to width / aspectRatio. If height is specified, aspectRatio will be ignored.

aspectRatio

Aspect ratio (width / height) of the output PNG file.

units

Units of width and height. Note that defaults units are pixels (px).

tryToUseRagg

If TRUE (the default) and the ragg package is installed, the PNG file will be created using agg_png, otherwise png will be used. See https://ragg.r-lib.org for more information.

type

Plotting device; passed directly to png. Defaults to "cairo" if that is an available device since it produces nicer looking graphics. Ignored if tryToUseRagg == TRUE and ragg is installed. Note that R can be compiled with Cairo support, but it still won't work on MacOS if Quartz is not installed.

res

The nominal resolution in ppi. The value is simply passed in to agg_png or png. Increasing the resolution will increase the size (in pixels) of the text and graph elements.

onlyIfDoesntExist

If TRUE and the output file already exists, JPlotToPNG will do nothing.

createDirectory

If TRUE and filename is located in a directory which doesn't exist, the directory will be created.

...

Any additional arguments are passed to either agg_png or png.

Value

The result of evaluating plotExpr is returned invisibly (which means it is not automatically printed).

See Also

agg_png, png

Examples

## Not run: 
JPlotToPNG("test.png", plot(rnorm(50), type = 'l', main = 'Wiggly line'))

# Plot to a PNG file with width 180 mm, height 120 mm
# (i.e. height / aspectRatio which defaults to (3 / 2)), resolution 300 ppi.
# This results in a PNG file with size 2125x1417 pixels
JPlotToPNG("test.png", plot(1:10 + rnorm(10), type = "o"), width = 180, units = "mm", res = 300)

## End(Not run)


JimMcL/JUtils documentation built on Nov. 7, 2024, 11:25 a.m.