Accessible charts
When rendering to HTML, Rmarkdown will render plots as img
tags, including when the output device is set to svg or svglite. Unfortunately this is problematic for the accessibility of your plot, as while the plot will be scaleable to fit the window dimensions it will still be rendered by the browser as an image meaning that any text within the image is not selectable by the user (or readable by a screen-reader). You can set the knitr option fig.cap
to provide a caption for your plot which will be inserted as the alt text for the image and be inserted as a caption under the image (though not using the modern convention of nested figure
and figcaption
tags).
Introducing render_svg()
To solve this problem the {govukhugo}
package includes a render_svg()
function that will take a ggplot2::ggplot()
object and render it as in-line SVG.
|
|
render_svg()
requires you supply it with a plot object (from {ggplot2}
), and the width and height of the plot (in pixels by default). You can supply other measurements (mm, cm and in), if you do so you should change the dpi argument from 96 to 300.
There are three optional arguments to provide alt text and captions.
The alt_title
argument is used to produce a tool-tip (similar to how the alt
argument for images works), alt_desc
allows you to embed a more detailed description in the SVG plot that screen readers can access.
The caption
argument inserts a text caption after the plot. The caption can be different from the content of alt_title
and alt_desc
, however you can also set caption = "alt_text"
or caption = "alt_desc"
so that the caption takes the value of that argument.
Examples
A plot without any alt text or caption (not advised).
|
|
A plot with an alt_title
, hover over the plot to see the tool-tip.
|
|
A plot with an alt_title
, and a custom caption
.
|
|
A plot with an alt_title
and alt_desc
, using alt_desc
for the caption
.
|
|