Home > Software engineering >  Problem with echarts4r and polar coord system, why this plot returns me an error?
Problem with echarts4r and polar coord system, why this plot returns me an error?

Time:11-09

Hi and thanks for reading me. Im trying to generate a plot with a custome palette color, but im getting an error when I use the polar coord system and I don't know why that's happened. Anyone knows how to fix this problem? Thanks a lot for the help.

The code is the following:

library(echarts4r)
library(dplyr)
library(ggplot2)

df <- data.frame(
  x = seq(500),
  y = rnorm(50, 10, 3),
  z = rnorm(50, 11, 2),
  w = rnorm(50, 9, 2)
)

df |> 
  head(90) |> 
  mutate(
    color = after_scale(colorspace::lighten(x,0.4, space = "HLS") )
  ) |> 
  e_charts(x) |> 
  e_polar(show = F) |> 
  e_angle_axis(show = F) |> 
  e_radius_axis(x) |> 
  e_bar(y, coord_system = "polar",  barWidth = 1) |> 
  e_scatter(y, coord_system = "polar", symbol_size = 3) |> 
  e_theme("auritus") |> 
  e_tooltip(trigger = "item") |> 
  e_add_nested("itemStyle", color)

CodePudding user response:

This looks like a bug to me. After a look into the source code the issue is that in case of the polar chart e_add_nested returns a data list where the element containing the value is unnamed. I would suggest to raise an issue on the package website.

However, at least for your case I was able to create a small functions which fixes the issue by naming the element containing the value.

Note: I dropped the ggplot2::after_scale which IMHO is not needed and makes no sense when using echarts4r. But of course I may be wrong on that.

library(echarts4r)
library(dplyr)

set.seed(123)

df <- data.frame(
  x = seq(500),
  y = rnorm(50, 10, 3),
  z = rnorm(50, 11, 2),
  w = rnorm(50, 9, 2)
)

fix_add_nested <- function(e) {
  e$x$opts$series[[1]]$data <- lapply(
    e$x$opts$series[[1]]$data, 
    function(x) { names(x) <- c("value", "itemStyle"); x }
  )
  
  e
}

df |> 
  head(90) |> 
  mutate(
    color = colorspace::lighten(x, 0.4, space = "HLS")
  ) |> 
  e_charts(x) |> 
  e_polar(show = F) |> 
  e_angle_axis(show = F) |> 
  e_radius_axis(x) |> 
  e_bar(y, coord_system = "polar",  barWidth = 1) |> 
  e_scatter(y, coord_system = "polar", symbol_size = 3) |> 
  e_theme("auritus") |> 
  e_tooltip(trigger = "item") |> 
  e_add_nested("itemStyle", color) |> 
  fix_add_nested()

enter image description here

  • Related