Search code examples
rhighcharts

Legend color of highcharter boxplot in R


I tried to build boxplot as described here https://rpubs.com/techanswers88/highcharter-boxplot-in-r

install.packages("highcharter")
install.packages("dplyr")
install.packages("gapminder")

library(highcharter)
library(dplyr)
library(gapminder)

ds <- gapminder %>%
   dplyr::filter(year == 2007) %>%
   dplyr::arrange(-pop)

myboxplotData <- data_to_boxplot(
    ds,
    lifeExp,
    continent,
    group_var     = continent,
    add_outliers  = FALSE,
    fillColor     = c("red", "green","yellow", "pink","blue"),
    color        = "black",                        
   )

highchart()%>%
hc_xAxis(type ="category") %>%
hc_add_series_list(myboxplotData) %>%
hc_xAxis(title = list(text = "contient")) %>%
hc_yAxis(title = list(text = "Life expectancy")) %>%
hc_title(text= "Boxplot  using highcharter") %>% 
hc_subtitle(text= "Life expectancy of each continent") %>% 
hc_caption(text= "Based on year 2007 population") %>% 
hc_legend(enabled= FALSE)

and its working fine

Now I wanted to add a legend at the bottom so I changed

hc_legend(enabled= FALSE)

to

hc_legend(enabled= TRUE)

The name of the legends appears but all colors are black instead of those defined in fillColor in the data_to_boxplot function.

So I tried :

color = c("red", "green","yellow", "pink","blue")

instead of

color        = "black"

and it works for legend color BUT the boxplot shape and the median disappears (not black anymore)

Is there a way to keep the shape of boxplot and median black and the legens with the same color as the boxplot fillColor ? Thanx


Solution

  • In Highcharter, the color argument actually changes the color of the boxplot outline and median. You're correct in using the fillColor argument to change the color of the boxplot itself, but unfortunately, the Highcharter library in R does not automatically use fillColor for the legend symbols.

    One solution to this issue is to change the color of the legend symbols using JavaScript directly within the hc_chart function using the Highcharts.setOptions method. Here is how you can do it:

    install.packages("highcharter")
    install.packages("dplyr")
    install.packages("gapminder")
    
    library(highcharter)
    library(dplyr)
    library(gapminder)
    
    ds <- gapminder %>%
      dplyr::filter(year == 2007) %>%
      dplyr::arrange(-pop)
    
    myboxplotData <- data_to_boxplot(
      ds,
      lifeExp,
      continent,
      group_var     = continent,
      add_outliers  = FALSE,
      fillColor     = c("red", "green","yellow", "pink","blue"),
      color        = "black",                        
    )
    
    highchart()%>%
      hc_chart(
        events = list(
          load = JS("function () {
            Highcharts.each(this.series, function (series) {
              series.legendSymbol.attr({ fill: series.options.fillColor });
            });
          }")
        )
      ) %>%
      hc_xAxis(type ="category") %>%
      hc_add_series_list(myboxplotData) %>%
      hc_xAxis(title = list(text = "contient")) %>%
      hc_yAxis(title = list(text = "Life expectancy")) %>%
      hc_title(text= "Boxplot  using highcharter") %>% 
      hc_subtitle(text= "Life expectancy of each continent") %>% 
      hc_caption(text= "Based on year 2007 population") %>% 
      hc_legend(enabled= TRUE)