
Improve keyboard accessibility for line charts in plotly/ggplotly
Source:R/ro_ply_keyboard_accessibility.R
ro_ply_keyboard_nav_linechart.RdArguments
- fig
A
plotlyobject (fromplotly::plot_ly()orplotly::ggplotly()).
Details
Enhances the accessibility of interactive line 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 line charts (including those created with ggplotly).
Keyboard navigation: users can use Tab to focus the first bar, and arrow keys (Left/Right/Up/Down) to move between points on the line Pressing Esc removes focus from the chart and returns focus to the next logical element.
See also
Other ggplotly:
ro_ply_cursor(),
ro_ply_disable_toolbar(),
ro_ply_keyboard_nav_barchart(),
ro_ply_keyboard_nav_bargraph(),
ro_ply_keyboard_nav_legend(),
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_bargraph(),
ro_ply_keyboard_nav_legend(),
ro_ply_keyboard_nav_trendline(),
ro_ply_legend_theme(),
ro_ply_theme(),
ro_ply_title()
Examples
if (FALSE) { # \dontrun{
data <- tidyr::tibble(expand.grid(Jaar = 2001:2020, Geslacht = c("Man", "Vrouw")),
value = sample(1:50, 40, TRUE)) |>
dplyr::bind_rows(., dplyr::summarize(., value = sum(value), .by = "Jaar") |>
dplyr::mutate(Geslacht = "Totaal")) |>
dplyr::mutate(Geslacht = factor(Geslacht, levels = c("Man", "Vrouw", "Totaal")))
library(ggplot2)
library(plotly)
p <- ggplot(data, aes(x = Jaar, y = value, color = Geslacht, group = Geslacht,
text = paste0(
"Geslacht: <b>", Geslacht,
"</b><br>Jaar: <b>", Jaar,
"</b><br>Aantal cases: <b>", value
))) +
geom_line()
fig <- ggplotly(p, tooltip = "text")
fig <- ro_ply_keyboard_nav_linechart(fig)
fig
} # }