Skip to contents

[toegankelijkheid]

Usage

ro_ply_keyboard_nav_bargraph(fig)

Arguments

fig

A plotly object (from plotly::plot_ly() or plotly::ggplotly()).

Value

A plotly object with improved keyboard accessibility.

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

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
} # }