
Improve keyboard accessibility for (stacked) bar graphs in plotly/ggplotly
Source:R/ro_ply_keyboard_accessibility.R
ro_ply_keyboard_nav_bargraph.RdArguments
- fig
A
plotlyobject (fromplotly::plot_ly()orplotly::ggplotly()).
Details
Enhances the accessibility of interactive horizontal bar charts created with plotly or ggplotly.
This function injects custom JavaScript to make bar elements focusable and navigable via keyboard,
displays accessible tooltips, and ensures screen readers can interpret the chart. The function is
intended for use on plotly bar graphs (including those created with ggplotly).
Keyboard navigation: users can use Tab to focus the first bar, and arrow keys (Up/Down/Left/Right) to move between bars. Pressing Esc removes focus from the chart and returns focus to the next logical element.
This function works for:
Standard (vertical) bar graphs
Stacked bar graphs
Grouped/dodged bar graphs
Proportional stacked bar graphs (bars with proportions)
Not suitable for:
Horizontal barcharts
See also
Other ggplotly:
ro_ply_cursor(),
ro_ply_disable_toolbar(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline()
Other plotly:
ro_ply_add_theme(),
ro_ply_cursor(),
ro_ply_dim(),
ro_ply_disable_toolbar(),
ro_ply_focus_linechart(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_linechart(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
Examples
if (FALSE) { # \dontrun{
library(ggplot2)
library(plotly)
data <- tidyr::tibble(
Sex = factor(c("Vrouw", "Man"), levels = c("Vrouw", "Man")),
n = sample(1e6:5e6, 2)
)
p <- ggplot(data, aes(x = Sex, y = n, fill = Sex,
text = paste0(
"Geslacht: <b>", Sex, "</b>",
"</b><br>Aantal cases: <b>",
format(round(n, 0),
big.mark = ".", decimal.mark = ",",
scientific = FALSE), "</b>"
))) +
geom_col(width = 0.6) +
labs(x = "Geslacht", y = "Aantal cases") +
theme_minimal()
fig <- ggplotly(p, tooltip = "text")
fig <- ro_ply_keyboard_nav_bargraph(fig)
fig
} # }