Search code examples
recharts4r

How can I hide a single series in an echarts legend with R?


Given the following example I want to hide the "series2" in the legend (it should still show up in the chart).

I already received an answer to this with a javascript solution here but I am struggling to make it work with R. I don't know which datastructure has to be used for the legend.data property.

library(echarts4r)
library(dplyr)

dataset <- data.frame(id = 1:5, value1 = seq(10, 50, length.out = 5), value2 = seq(200, 500, length.out = 5), value3 = seq(100, 250, length.out = 5))

dataset %>%
  e_charts(x = id) %>%
  e_line(serie = value1, name = "series1") %>%
  e_line(serie = value2, name = "series2") %>%
  e_line(serie = value3, name = "series3") %>%
  e_legend(
    selected = list("series3" = FALSE)
  )

The javascript solution would look like this:

legend: {
    data: ['series1', 'series3'],   // 'series2' excluded
    selected: {series3: false}
  },

Solution

  • You could use legend = FALSE in one of the e_line like this:

    library(echarts4r)
    library(dplyr)
    
    dataset <- data.frame(id = 1:5, value1 = seq(10, 50, length.out = 5), value2 = seq(200, 500, length.out = 5), value3 = seq(100, 250, length.out = 5))
    
    dataset %>%
      e_charts(x = id) %>%
      e_line(serie = value1, name = "series1") %>%
      e_line(serie = value2, name = "series2", legend = FALSE) %>%
      e_line(serie = value3, name = "series3")
    

    Output:

    enter image description here