I am working on an echarts4r
plot where I have customized symbols, and I want the legend to match these symbols along with specific border colors. The plot is based on the following example data:
library(echarts4r)
library(dplyr)
the_data <- data.frame(
key = c("a", "b", "c", "d", "e", "f"),
group = c("A", "B", "C", "D", "E", "F"),
x = c(0.495, -0.466, 0.85, 1.06, -0.441, 0.395),
y = c(1.06, 0.82, 3.76,3.17, 0.49, 2.67),
color = c("#FFFF00", "#FFA500", "#FFFF00", "#FFA500", "#FFFF00", "#FFA500"),
symbolSize = c(15, 15, 15, 15, 15, 15),
borderColor = c("#FF0000", "#0000FF", "#FF0000", "#0000FF", "#FF0000", "#0000FF"),
symbol = c("rect", "rect", "circle", "circle", "diamond", "path://M27,34.44 L29.87,29.47 L24.13,29.47 Z"
))
If I generate the plot without configuring the legend, there is a discrepancy between the elements in the plot and the legend.
the_data %>%
dplyr::group_by(group) %>%
echarts4r::e_chart(x) %>%
echarts4r::e_scatter(y,
itemStyle = list(borderWidth = 2)) %>%
echarts4r::e_add_unnested("symbol", symbol) %>%
echarts4r::e_add_unnested("symbolSize", symbolSize) %>%
echarts4r::e_add_nested("itemStyle", color, borderColor)
Subsequently, I attempted to configure the legend. While I managed to successfully define the shapes and colors for the legend, I encountered challenges in setting the borderColor
specifically for the legend items. The current approach involves utilizing borderColor
within the itemStyle
. An alternative method explored was using echarts4r::e_add_nested("itemStyle", color, borderColor)
, which had already been employed for implementing borderColor
in echarts4r::e_scatter()
. However, it seems to have no discernible effect in the context of the legend customization.
legend_data <- the_data %>%
dplyr::distinct(group, color, borderColor, symbol) %>%
dplyr::arrange(group)
the_data %>%
dplyr::group_by(group) %>%
echarts4r::e_chart(x) %>%
echarts4r::e_scatter(y,
itemStyle = list(borderWidth = 2)) %>%
echarts4r::e_add_unnested("symbol", symbol) %>%
echarts4r::e_add_unnested("symbolSize", symbolSize) %>%
echarts4r::e_add_nested("itemStyle", color, borderColor) %>%
echarts4r::e_color(color = legend_data$color) %>%
echarts4r::e_legend(
itemWidth = 15,
itemStyle = list(
borderColor = legend_data$borderColor,
borderWidth = 2,
itemWidth = 15
),
icons = legend_data$symbol
)
Any help is highly appreciated!