Home > Software engineering >  Custom colors in R highcharter barplot
Custom colors in R highcharter barplot

Time:11-30

I am trying to produce a bar plot with a legend and custom colors with the higcharter package.

First the data:

dta <- structure(list(antibiotic = c("Ceftriaxone", "Ampicillin", "Gentamicin", 
                                     "Meropenem", "Cefotaxime", "Cloxacillin", "Metronidazole", "Vancomycin", 
                                     "Ceftazidime", "Penicillin V", "Amoxicillin", "Azithromycin", 
                                     "Cefixime", "Clindamycin", "Doxycycline", "Moxifloxacin"), 
                      category = structure(c(1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L), 
                                           .Label = c("Watch", "Access", "Reserve", "Unknown"), class = "factor"), 
                      n = c(148L, 92L, 44L, 30L, 13L, 12L, 7L, 3L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L)), 
                 class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -16L))

First attempt:

library(highcharter)
cols <- c("#2c3e50", "#f39c12")

dta %>%
  hchart(type = "bar", hcaes(x = "antibiotic", y = "n", group = "category"), color = cols) %>%
  hc_yAxis(title = "", stackLabels = list(enabled = TRUE)) %>% 
  hc_xAxis(title = "") %>%
  hc_tooltip(headerFormat = "",
             pointFormat = "{point.n} patients have taken {point.antibiotic}") %>%
  hc_plotOptions(series = list(stacking = "normal"))

The colors and legend are as expected but the resulting plot order the elements by category first while I would like to have them ordered by n Attempt 1

I have tried:

dta %>%
  hchart(type = "bar", hcaes(x = "antibiotic", y = "n", color = "category")) %>%
  # hc_colors(c("#2c3e50", "#f39c12")) |> 
  hc_yAxis(title = "", stackLabels = list(enabled = TRUE)) %>% 
  hc_xAxis(title = "") %>%
  hc_tooltip(headerFormat = "",
             pointFormat = "{point.n} patients have taken {point.antibiotic}") %>%
  hc_plotOptions(series = list(stacking = "normal"))

Now the ordering is perfect but the legend has disappeared and I can't have custom colors either. I have tried adding a column of colors to the data frame and toying with hc_colors()

Attempt 2

CodePudding user response:

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