Search code examples
recharts4r

echarts4r : how to map a third continuous variable to a custom color scale (without changing the points size)


In the following reproducible example I would like to map the year value to a continuous color scale (with all the points with a manually selected and fixed size). The points are not of the expected color...

library(echarts4r)

d <- data.frame(
    x = rnorm(30), 
    y = rnorm(30), 
    year = rep(2000:2014, 2)
)

d |> 
    e_chart(x) |> 
    e_scatter(y, symbol_size = 10, bind = year, legend = FALSE) |> 
    e_visual_map(year, color = c("#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072")) |> 
    e_tooltip()

Solution

  • Looks like a bug: the year column is not in the series object of the echart object. Here is a way to include it and the code to produce the plot:

    library(echarts4r)
    
    d <- data.frame(
      x = rnorm(30), 
      y = rnorm(30), 
      year = rep(2000:2014, 2)
    )
    
    dd <- lapply(purrr::transpose(d), function(row) list(value = unname(unlist(row))))
    
    ECHART <- d |> 
      e_charts(x) |> 
      e_scatter(y, symbol_size = 10, legend = FALSE) |> 
      e_visual_map(year, dimension = 2, inRange = list(color = c("#8DD3C7", "#FFFFB3", "#BEBADA", "#FB8072"))) |> 
      e_tooltip()
    
    ECHART$x$opts$series[[1]]$data <- dd
    ECHART