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