Search code examples
rhighchartsr-highcharter

R Highcharter legend does not recognize custom colors for stacked bar charts


I have a stacked column chart, created with highcharts in R and guidance from @madepiet, see minimal example below. The data is provided manually in order to specify a custom color for each data point (specifically, I want to use a pattern only on one column). While this styles the columns as intended, the custom colors are not populated in the legend - highcharts default colors are shown. How can I address this? Thanks so much!!

highchart() %>%
  hc_chart(type = "column") %>%
  hc_add_dependency("modules/pattern-fill.js") %>%
  hc_xAxis(categories = c('Day 1', 'Day 2'), tickmarkPlacement = "on") %>%
  hc_yAxis(title = list(text = "Value")) %>%
  hc_plotOptions(column = list(stacking = "normal")) %>%
  hc_plotOptions(line = list (datalabels = list(enabled = TRUE), enableMouseTracking = FALSE)) %>%
  hc_add_series( 
  name = "Var1", 
  data = list(list(y = 2, color="blue"), 
              list(y = 4, color = list(pattern = list(path = 'M 0 0 L 10 10 M 9 -1 L 11 1 M -1 9 L 1 11', width = 10, height = 10, color = 'blue')))),
  showInLegend = T) %>% 
  hc_add_series(
    name = "Var2", 
    data = list(list(y = 3, color="red", showInLegend = T), 
                list(y = 5, color = list(pattern = list(path = 'M 0 0 L 10 10 M 9 -1 L 11 1 M -1 9 L 1 11', width = 10, height = 10, color = 'red')))),
    showInLegend = T)

Solution

  • The issue is that you specified the colors via the data attribute. However, according to the docs this is meant to color or highlight a single point of the series.

    Instead, you have to set the color of the series.

    Unfortunately the series color is not inherited by the patterned bars (or more precisely: I haven't found an option to achieve that). Hence, for patterned bars one has to set the colors manually as you already did in your code.

    library(highcharter)
    
    highchart() %>%
      hc_chart(type = "column") %>%
      hc_add_dependency("modules/pattern-fill.js") %>%
      hc_xAxis(categories = c("Day 1", "Day 2"), tickmarkPlacement = "on") %>%
      hc_yAxis(title = list(text = "Value")) %>%
      hc_plotOptions(column = list(stacking = "normal")) %>%
      hc_plotOptions(line = list(
        datalabels = list(enabled = TRUE),
        enableMouseTracking = FALSE
      )) %>%
      hc_add_series(
        name = "Var1",
        data = list(
          list(y = 2),
          list(
            y = 4,
            color = list(
              pattern = list(
                path = "M 0 0 L 10 10 M 9 -1 L 11 1 M -1 9 L 1 11",
                width = 10, height = 10, color = "blue"
              )
            )
          )
        ),
        color = "blue",
        showInLegend = T
      ) %>%
      hc_add_series(
        name = "Var2",
        data = list(
          list(y = 3),
          list(
            y = 5,
            color = list(
              pattern = list(
                path = "M 0 0 L 10 10 M 9 -1 L 11 1 M -1 9 L 1 11",
                width = 10, height = 10, color = "red"
              )
            )
          )
        ),
        color = "red"
      )
    

    enter image description here