Skip to content

Instantly share code, notes, and snippets.

@PaulC91
Last active February 27, 2019 19:42
Show Gist options
  • Save PaulC91/e1799669ccf77b262ae625555b099391 to your computer and use it in GitHub Desktop.
Save PaulC91/e1799669ccf77b262ae625555b099391 to your computer and use it in GitHub Desktop.
maintain desired order of categorical xAxis regardless of data order
library(highcharter)
library(tibble)
df <- tibble(
group = c(rep("G1", 3), rep("G2", 4)),
cat = c("B", "C", "D", "A", "B", "C", "D"),
index = c(2, 3, 4, 1, 2, 3, 4),
n = runif(7)
)
# this plots with "B" as the first xAxis label with "A" at the end because of the order of data
hchart(df, "line", hcaes(cat, n, group = group))
# set the xAxis as a numeric index of the desired category then the category as
# the 'name' argument in hcaes and set xAxis to type category to get desired order
hchart(df, "line", hcaes(index, n, group = group, name = cat)) %>%
hc_xAxis(type = "category")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment