Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

plotTag

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


Description

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

Usage

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

img <- plotTag({
  plot(cars)
}, "A plot of the 'cars' dataset", width = 375, height = 275)

if (interactive()) img


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

if (interactive()) svg

htmltools

Tools for HTML

v0.5.1.1
GPL (>= 2)
Authors
Joe Cheng [aut], Carson Sievert [aut, cre] (<https://orcid.org/0000-0002-4958-2844>), Winston Chang [aut], Yihui Xie [aut], Jeff Allen [aut], RStudio [cph]
Initial release

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.