JPlotToJPEG: Plot to a JPEG file

View source: R/plot_to_file.R

JPlotToJPEGR Documentation

Plot to a JPEG file

Description

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

Usage

JPlotToJPEG(
  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 JPEG 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 JPEG file in units.

height

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

aspectRatio

Aspect ratio (width / height) of the output JPEG 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 JPEG file will be created using agg_jpeg, otherwise jpeg will be used. See https://ragg.r-lib.org for more information.

type

Plotting device; passed directly to jpeg. Defaults to "cairo" if that is an available device since it produces nicer looking graphics. Ignored if tryToUseRagg == TRUE and ragg is installed.

res

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

onlyIfDoesntExist

If TRUE and the output file already exists, JPlotToJPEG 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 agg_jpeg or jpeg.

Value

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

See Also

agg_jpeg, jpeg

Examples

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

# Plot to a JPEG file with width 180 mm, height 120 mm
# (i.e. height / aspectRatio which defaults to (3 / 2)), resolution 300 ppi.
# This results in a JPEG file with size 2125x1417 pixels
JPlotToJPEG("test.jpg", 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.