# adapted from code for this chart by Silvia Canelón in repo for
# <https://silvia.rbind.io/blog/2021-curated-compilations/01-data-viz-a11y/>

library(highcharter)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(palmerpenguins)

highchart() %>%
  hc_add_dependency(name = "modules/accessibility.js") %>%
  hc_add_dependency(name = "modules/annotations.js") %>%
  hc_add_dependency(name = "modules/exporting.js") %>%
  hc_add_dependency(name = "modules/export-data.js") %>%
  hc_add_series(penguins, "scatter", hcaes(x = flipper_length_mm,
                                           y = bill_length_mm,
                                           group = species)) %>%
  # n.b. by not adding color above, you get "automatic" dual encoding
  # of points with a different shape for the markers for each species
  hc_xAxis(
    title = list(text = "Flipper length (mm)"),
    accessibility = list(
      enabled = TRUE,
      description = "flipper length in millimeters"
    )
  ) %>%
  hc_yAxis(
    title = list(text = "Bill length (mm)"),
    accessibility = list(
      enabled = TRUE,
      description = "bill length in millimeters"
    )
  ) %>%
  hc_title(
    text = "Flipper length vs. bill length in <b>{palmerpenguins}</b>",
    style = list(useHTML = TRUE)
           ) %>%
  hc_subtitle(
    text = "Grouped by species: Adelie, Chinstrap, and Gentoo"
    ) %>%
  hc_annotations(
    list(
      labels = list(
        list(
          point = list(x = 201, y = 54.2, xAxis = 0, yAxis = 0),
          text = "Chinstrap<br/>x: {x}<br/>y: {y}",
          shape = "connector" # defaults to 'callout'
        )
      ),
      # below gives you screenreader descriptions of annotations
      labelOptions = list(
        accessibility = list(
          # probably wouldn't hard code description here if there was
          # more than one point annotated
          description = "A Chinstrap penguin observation mapping to a flipper length of 201mm and bill length of 54.2mm."
      )
    )
  )
  )%>%
  hc_caption(text = "Scatterplot of the palmerpenguins dataset showing data points clustered by species (Adelie, Chinstrap, and Gentoo) using the highcharter package making it possible to focus on one cluster and identify the x and y values of a specific data point. In this case the data point is a Chinstrap penguin observation mapping to a flipper length of 201mm and bill length of 54.2mm.") %>%
  hc_exporting(
    enabled = TRUE,
    accessibility = list(
      enabled = TRUE
    )
  ) %>%
  hc_plotOptions(
    accessibility = list(
      enabled = TRUE,
      keyboardNavigation = list(enabled = TRUE)
    )
  ) %>%
  # can still use the custom colors and keep shape per group if added here
  hc_colors(c("darkorange", "purple", "#057276"))