render_svg: Render ggplot2 objects as in-line SVG

View source: R/images.R

render_svgR Documentation

Render ggplot2 objects as in-line SVG

Description

render_svg() converts ggplot2 objects to SVG code to render them as in-line SVG code, and provides options for providing ARIA labels for assistive technologies.

Usage

render_svg(
  plot,
  width,
  height,
  units = "px",
  alt_title = NULL,
  alt_desc = NULL,
  caption = NULL,
  dpi = 96
)

Arguments

plot

A ggplot2::ggplot() object

width

The desired width of the object

height

The desired height of the object

units

The units of width and height, default is px (pixels)

alt_title

Short alt text (will show as tool-tip)

alt_desc

Longer alt text (embedded within SVG)

caption

A caption to display to all users, can also be set to "alt_title" or "alt_desc" to match their value

dpi

Dots per inch, default of 96 for screen resolution, switch to 300 if using physical units (mm, cm or inches)

Details

When rendering to HTML, Rmarkdown will render plots as <img> tags, including when the output device is set to svg or svglite. This is problematic for accessibility reasons, as while scalable the plot continues to be rendered by the browser as an image meaning that any text within the plot is not selectable by the user and cannot be read by a screen reader.

render_svg() uses ggplot2::ggsave() to convert the plot to an chunk of SVG code, rendering text as words (via the svglite::svglite() device) and returning that code rather than producing a plot. If the alt_title, alt_desc are used then these will embedded within the SVG code, these are encoded as ARIA labels for the SVG chunk and alt_title will also present as a tooltip. Optionally, a caption can be provided which will be inserted below the plot, if the caption argument is set as alt_title or alt_desc then it will take the value of that argument.


co-analysis/govuk-hugo-r documentation built on June 23, 2022, 5:44 a.m.