echarts4r
echarts4r copied to clipboard
Aesthetic mapping in echarts4r legend
Hi,
I have a need to plot a single time series using echarts4r.
I have been able to plot the desired output using the below
library(dplyr)
library(echarts4r)
ptd_data <- data.frame(
x = seq.Date(from = as.Date("2021-01-01"), by = "month", length.out = 12),
y = rnorm(12, 100, 10),
upl = rnorm(12, 110, 10),
lpl = rnorm(12, 90, 10),
point_type = sample(c("point_type_1", "point_type_2", "point_type_3", "point_type_4"), 12, replace = TRUE)
)
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 # Enable scaling
)
) %>%
e_add_nested("itemStyle", color = point_colour) |>
e_tooltip(trigger = "axis")
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
library(ggplot2)
# Sample data
ptd_data <- data.frame(
x = seq.Date(from = as.Date("2021-01-01"), by = "month", length.out = 12),
y = rnorm(12, 100, 10),
upl = rnorm(12, 110, 10),
lpl = rnorm(12, 90, 10),
point_type = sample(c("point_type_1", "point_type_2", "point_type_3", "point_type_4"), 12, replace = TRUE)
)
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]]))
# Plot using 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()
Is there a solution using e charts4r