Search code examples
rhighchartslegendlegend-propertiesr-highcharter

R - highcharter - selective legends at display


I am not sure whether this could be done in highcharter, however, I have a scenario where I have 7 series plotted of which by default I need to display only 3, however remaining 4 should be greyed out but be present, user can click and select if needed.

So, how can we do that? All suggestions are welcome. I did some research, however i was not able to get any leads.

Thanks!

hc <- highchart(type = "stock") %>% 
    hc_title(text = paste0("<span style=\"color:#000000\">", prod_line, ", ", sku , " at ", cust_group ,  "</span>")) %>%
    hc_subtitle(text = paste0("<span style=\"color:#000000\">",  prod_mktg_name, "</span>")) %>%
    hc_yAxis_multiples(yaxis) %>%
    hc_add_series(yAxis = 0, name = "Retail Price", candle_data, type = "candlestick", color = "white") %>%
    hc_add_series(yAxis = 0, name = "COGS($)" , data = my_xts_data$COGS_dollar, color = "blue") %>% 
    hc_add_series(yAxis = 0, name = "MSRP", data = my_xts_data$MSRP, color = "black") %>%
    hc_add_series(yAxis = 0, name = "Sell In Price", data = my_xts_data$SI_price, color = "orange") %>%
    hc_add_series(yAxis = 1, name = "Sell In Units", data = my_xts_data$SI_units, color = "orange", type = "column") %>%
    hc_add_series(yAxis = 1, name = "Channel Inv Units", data = my_xts_data$CI_units, color = "gray", type = "column") %>%
    hc_add_series(yAxis = 1, name = "Sell Thru Units", data = my_xts_data$ST_units, color = "black", type = "column") %>%
    hc_legend(enabled = TRUE) %>%
    hc_exporting(enabled = TRUE) %>%
    hc_tooltip(shared = TRUE) 

hc

Solution

  • You can add the argument 'visible = FALSE' to the hc_add_series functions

    https://api.highcharts.com/highcharts/plotOptions.series.visible

    Highcharter example modified from http://jkunst.com/highcharter/highstock.html below

    library("quantmod")
    
    usdjpy <- getSymbols("USD/JPY", src = "oanda", auto.assign = FALSE)
    eurkpw <- getSymbols("EUR/KPW", src = "oanda", auto.assign = FALSE)
    
    hc <- highchart(type = "stock") %>% 
      hc_title(text = "Charting some Symbols") %>% 
      hc_subtitle(text = "Data extracted using quantmod package") %>% 
      hc_add_series(usdjpy, id = "usdjpy") %>% 
      hc_add_series(eurkpw, id = "eurkpw", visible = FALSE) %>%
      hc_legend(enabled = TRUE)