ggiraph
ggiraph copied to clipboard
Add `alt_text` argument to girafe() for accessibility
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.
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/
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:
titleanddescsub-elements are added to thesvgelementaria-labelledbyandaria-describedbyattributes are added to thesvgelementroleattribute (role='img') is added to thesvgelement
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.
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.