The goal of this document is to provide a fairly comprehensive
translation of highcharts.js plots within R. It attempts to combine
references found on the official highcharter website, in
addition to filling in gaps with highcharts.js examples,
and other plots.
It also provides a small extension to the highcharter
library which allows anyone to create runtime like toggles within an
existing highcharter plot definition within a statically
rendered document (like an rmarkdown html output).
gapminder::gapminder |>
mutate(iso_2 = countrycode::countrycode(country, "country.name", "iso2c")) |>
filter(country != "Kuwait") |>
gather(key, value, lifeExp:gdpPercap) |>
hcmap(
map = "custom/world",
download_map_data = TRUE,
joinBy = c("iso-a2", "iso_2"),
name = "",
nullColor = "transparent",
borderColor = "black",
value = "value"
) |>
hc_colorAxis(minColor = "#e6ebf5",maxColor = getOption("highcharter.color_palette") %>% tail(1)) %>%
hc_title(text = "Gapminder Map Toggles") %>%
# hc_add_theme()
add_multi_drop(
c("year", "key"),
c("2002", "lifeExp")
)
This page is heavily influenced by Joshua Kunst’s blog, documentation, and issue responses.
Theme
Theme
thm <- hc_theme(
colors = rev(c(
"#003f5c", "#2f4b7c", "#665191", "#a05195", "#d45087", "#f95d6a",
"#ff7c43", "#ffa600"
)),style = list(fontSize = "1.2rem"), chart = list(
backgroundColor = "var(--page-background)"
), xAxis = list(
labels = list(style = list(color = "var(--text-color)")),
gridLineDashStyle = "Dash",
gridLineWidth = 1, gridLineColor = "var(--code-block-background)", lineColor = "var(--text-color)",
minorGridLineColor = "var(--text-color)", tickColor = "var(--text-color)", tickWidth = 1,
title = list(style = list(color = "var(--text-color)"))
), yAxis = list(
labels = list(style = list(color = "var(--text-color)")),
gridLineDashStyle = "Dash",
gridLineWidth = 1, gridLineColor = "var(--code-block-background)", lineColor = "var(--text-color)",
minorGridLineColor = "var(--text-color)", tickColor = "var(--text-color)", tickWidth = 1,
title = list(style = list(color = "var(--text-color)"))
), legendBackgroundColor = "var(--page-background)",
background2 = "var(--page-background)", dataLabelsColor = "var(--text-color)", textColor = "var(--text-color)",
contrastTextColor = "var(--text-color)", maskColor = "rgba(255,255,255,0.3)",
title = list(style = list(color = "var(--text-color)")), subtitle = list(
style = list(color = "var(--text-color)")
), legend = list(
itemStyle = list(
color = "var(--text-color)"
), itemHoverStyle = list(color = "var(--text-color)"),
itemHiddenStyle = list(color = "var(--text-color)")
)
)
thm <- hc_theme_merge(
hc_theme_darkunica(),
thm
)
options(
highcharter.theme = thm,
highcharter.color_palette = rev(c(
"#003f5c", "#2f4b7c", "#665191", "#a05195", "#d45087", "#f95d6a",
"#ff7c43", "#ffa600"
))
)
highcharter::highcharts_demo()
Extending highcharter
Creating Multiple Toggles
One of the most difficult tasks I’ve ran into during my career was the need to create runtime interactivity… without runtime. In other words, finding a way to create the ability to toggle / filter / and select different series, within a statically rendered document.
Although I’ve approached this problem in multiple ways (many of them containing a lot of javascript code / very manual events and definitions), the below is the most seamless solution that I’ve come up with to date. It seems to work with multiple plot types, respects grouped data, and it allows you to go from a standard highcharts plot, to a ‘dashboard’ like plot with one line of code.
This makes these types of plots available to anyone that can define a highcharts plot :) - & there are a good amount of examples of those below.
tidy_employment_data <- fpp3::us_employment |>
janitor::clean_names() |>
as_tibble() |>
filter(!str_detect(title, ":")) |>
rename(value = employed) |>
mutate(month = lubridate::as_date(month)) |>
group_by(title) |>
mutate(
yoy = value / lag(value, 12) - 1,
`Three year comp` = value / lag(value, 36) - 1
) |>
gather(key, value, value:`Three year comp`)
tidy_employment_data |>
mutate(month = datetime_to_timestamp(month)) |> # for highcharts date format
highcharter::hchart("line", highcharter::hcaes(month, value, name = title)) |>
hc_xAxis(type = "datetime") |>
add_multi_drop(
selectors = c("title", "key"),
selected = c("Total Private", "Three year comp")
)