JoeK
JoeK

Reputation: 41

Is there an echarts4r version of color mapping in aes from ggplot2

I have a need to plot a single time series using echarts4r.

I have been able to plot the desired output using the below, which shows the legend mapped by colour.

Source data below

library(dplyr)
library(echarts4r)

ptd_data <- data.frame(
  x = seq.Date(from = as.Date("2021-01-01"), by = "month", length.out = 12),
  y = c(105.5, 97.2, 102.3, 99.8, 98.6, 101.0, 96.2, 103.1, 99.5, 98.3, 97.1, 100.4),
  upl = c(115.2, 110.1, 111.7, 108.6, 107.9, 113.0, 106.8, 112.5, 109.3, 108.1, 107.4, 110.8),
  lpl = c(89.8, 87.5, 92.0, 88.3, 87.0, 91.2, 85.9, 93.0, 88.5, 87.2, 86.1, 89.7),
  point_type = c("point_type_1", "point_type_2", "point_type_3", "point_type_4",
                 "point_type_1", "point_type_2", "point_type_3", "point_type_4",
                 "point_type_1", "point_type_2", "point_type_3", "point_type_4")
)
colours <- list("point_type_1" = "grey90","point_type_2" = "#361475","point_type_3" = "#fab428","point_type_4" = "#289de0")

ptd_data <- ptd_data %>%mutate(point_colour = sapply(point_type, function(pt) colours[[pt]]))
ptd_data %>%
  e_charts(x) %>%
  e_line(
    serie = y,
    symbol = "emptycircle",
    symbolSize = 4,emphasis = list(scale = 2)
  ) %>%
  e_add_nested("itemStyle", color = point_colour) %>%
  e_tooltip(trigger = "axis")

enter image description here

however as you can see the legend doesn't follow suit in terms of the e_add_nested color mapping

Desired output can be demonstrated using ggplot2 using the aes color mapping which splits the legend accordingly as per below using the same data.


library(ggplot2)

ggplot() +
  geom_line(data = ptd_data, aes(x = x, y = y), color = "black") +
  geom_point(data = ptd_data, aes(x = x, y = y, color = point_type), shape = "circle", size = 4) +
  scale_color_manual(values = colours) +
  theme_minimal()

enter image description here

Is there a solution using e charts4r? I have posted within the echarts4r issues area on github as well as per link: https://github.com/JohnCoene/echarts4r/issues/631

Upvotes: 2

Views: 85

Answers (1)

To use echarts4 you can do this

library(dplyr)
library(echarts4r)

ptd_data <- data.frame(
  x = seq.Date(from = as.Date("2021-01-01"), by = "month", length.out = 12),
  y = c(105.5, 97.2, 102.3, 99.8, 98.6, 101.0, 96.2, 103.1, 99.5, 98.3, 97.1, 100.4),
  upl = c(115.2, 110.1, 111.7, 108.6, 107.9, 113.0, 106.8, 112.5, 109.3, 108.1, 107.4, 110.8),
  lpl = c(89.8, 87.5, 92.0, 88.3, 87.0, 91.2, 85.9, 93.0, 88.5, 87.2, 86.1, 89.7),
  point_type = c("point_type_1", "point_type_2", "point_type_3", "point_type_4",
                 "point_type_1", "point_type_2", "point_type_3", "point_type_4",
                 "point_type_1", "point_type_2", "point_type_3", "point_type_4")
)

colours <- list("point_type_1" = "grey90","point_type_2" = "#361475","point_type_3" = "#fab428","point_type_4" = "#289de0")

plot <- ptd_data %>%
  e_charts(x) %>%
  e_line(serie = y, symbol = "none", lineStyle = list(color = "black")) %>%
  e_tooltip(trigger = "axis")

for(pt in unique(ptd_data$point_type)) {
  plot <- plot %>%
    e_scatter(serie = ptd_data %>% filter(point_type == pt) %>% pull(y), 
              name = pt,
              symbolSize = 8,
              itemStyle = list(color = colours[[pt]]))
}

plot <- plot %>%
  e_legend(show = TRUE)

plot

which gives

enter image description here

Upvotes: 0

Related Questions