ggiraph icon indicating copy to clipboard operation
ggiraph copied to clipboard

Add `alt_text` argument to girafe() for accessibility

Open geotheory opened this issue 3 years ago • 1 comments

To make the output usable for people with accessibility needs we can currently hack the output to add alt param to <svg> tag as follows:

g = girafe(ggobj = gg_point)

g$x$html = stringr::str_replace(g$x$html, fixed('<svg '), '<svg alt="A scatterplot of mtcars, plotting wt against qsec. The graph suggests a low correlation between two normally-distributed variables" ')
g

It would be preferable if this could be a simple option in the girafe() function.

geotheory avatar Feb 11 '22 16:02 geotheory

In fact would be good to implement alt_title, alt_desc & caption as per https://co-analysis.github.io/govuk-hugo-demo/section/render_svg/

geotheory avatar Feb 11 '22 16:02 geotheory

Fixed this in 70e4d62f4e7b2c431ee9c0e9f8293f9bdca1faea

Added title and desc arguments to dsvg function. You can set them in girafe function and they'll be forwarded. The result is that:

  • title and desc sub-elements are added to the svg element
  • aria-labelledby and aria-describedby attributes are added to the svg element
  • role attribute (role='img') is added to the svg element

Here is a minimal example:

library(ggplot2)
library(ggiraph)
dat <- mtcars
dat$carname <- row.names(mtcars)
gg <- ggplot(data = dat) +
  geom_point_interactive(aes(
    x = wt, y = qsec, color = disp,
    tooltip = paste(carname, disp, sep = "\n"), 
    data_id = carname
  )) +
  theme_minimal()
x <- girafe(ggobj = gg_point, title = "ggiraph is cool!", desc = "very very cool!")
print(x)

Be aware that when using the title argument, the browser will use it as a tooltip for the whole svg an it may class with the interactive elements' tooltip, as you can see in the example above.

sigmapi avatar Sep 09 '22 11:09 sigmapi

I preferred to stick with the w3c naming and use title and desc instead of alt-title and alt-desc.

As for the caption, we'd like to leave it out for now. If you really want a caption you can accomplish it by other way, for example enclose the widget in a figure element with a figcaption. Additionally, with the quarto develpment going on, this could by accomplished with quarto code.

sigmapi avatar Sep 09 '22 11:09 sigmapi