Capture a plot as a self-contained <img> tag

plotTag(
  expr,
  alt,
  device = defaultPngDevice(),
  width = 400,
  height = 400,
  pixelratio = 2,
  mimeType = "image/png",
  deviceArgs = list(),
  attribs = list(),
  suppressSize = c("none", "x", "y", "xy")
)

Arguments

expr

A plotting expression that generates a plot (or yields an object that generates a plot when printed, like a ggplot2).

alt

A single-element character vector that contains a text description of the image. This is used by accessibility tools, such as screen readers for vision impaired users.

device

A graphics device function; by default, this will be either grDevices::png(), ragg::agg_png(), or Cairo::CairoPNG(), depending on your system and configuration. See defaultPngDevice().

width, height

The width/height that the generated tag should be displayed at, in logical (browser) pixels.

pixelratio

Indicates the ratio between physical and logical units of length. For PNGs that may be displayed on high-DPI screens, use 2; for graphics devices that express width/height in inches (like grDevices::svg(), try 1/72 or 1/96.

mimeType

The MIME type associated with the device. Examples are image/png, image/tiff, image/svg+xml.

deviceArgs

A list of additional arguments that should be included when the device function is invoked.

attribs

A list of additional attributes that should be included on the generated <img> (e.g. id, class).

suppressSize

By default, plotTag will include a style attribute with width and height properties specified in pixels. If you'd rather specify the image size using other methods (like responsive CSS rules) you can use this argument to suppress width ("x"), height ("y"), or both ("xy") properties.

Value

A browsable() HTML <img> tag object. Print it at the console to preview, or call as.character() on it to view the HTML source.

See also

capturePlot() saves plots as an image file.

Examples

if (FALSE) { # rlang::is_interactive()
img <- plotTag({
  plot(cars)
}, "A plot of the 'cars' dataset", width = 375, height = 275)

img

if (capabilities("cairo")) {
  plotTag(
    plot(pressure), "A plot of the 'pressure' dataset",
    device = grDevices::svg, width = 375, height = 275, pixelratio = 1/72,
    mimeType = "image/svg+xml"
  )
}
}