score:1

Accepted answer

Bit of a pain to get this to work but I believe this is a solution to your question. Unfortenly there doesn't appear to be a way to do this with highcharts directly so you have to manipulate the data beforehand.

library(highcharter)
library(tidyverse)
cols <- c("#2c3e50", "#f39c12")
dta %>% 
  ungroup() %>% 
  mutate(antibiotic = factor(antibiotic) %>% fct_reorder(n, .desc = TRUE)) %>% 
  mutate(cat_index = as.numeric(antibiotic)) %>% 
  hchart(
    type = "bar",
    hcaes(x = cat_index,
          y = n,
          group = category,
          name = antibiotic),
          color = cols
  ) %>%
  hc_xAxis(type = "category", labels = list(step = 1)) %>% 
  hc_yAxis(title = "", stackLabels = list(enabled = TRUE)) %>%
  hc_tooltip(headerFormat = "",
             pointFormat = "{point.n} patients have taken {point.antibiotic}") %>%
  hc_plotOptions(series = list(stacking = "normal"))

example


Related Query

More Query from same tag