Closed lgnbhl closed 1 year ago
According to @helgasoft "ECharts seems to ignore non-numerical categories in piecewise choropleths" (https://github.com/helgasoft/echarty/issues/14).
Is there a way to use characters/factors instead of transforming them into numerical values? The workaround proposed by @helgasoft is also problematic when using the tooltip calling e_tooltip()
.
For the record here the solution provided by @helgasoft with numerical values:
cns <- countrycode::codelist$country.name.en
cns <- data.frame(
country = cns,
# category = sample(LETTERS[1:4], length(cns), replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05))
numeric = sample(1:4, length(cns), replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05))
)
# set colors for categories
colors <- c('#8b0069','#75c165', '#ce5c5c', '#fbc357')
pieces <- lapply(unique(cns$numeric), function(x) {
list(value= x, label= LETTERS[x], color= colors[x])
})
cns |>
e_charts(country) |>
e_map(numeric) |>
e_visual_map(numeric, type = "piecewise", pieces = pieces)
@helgasoft kindly provided another better solution using the parameter categories
with his own R package echarty (https://github.com/helgasoft/echarty/issues/14).
However, I am not able to reproduce this solution using the categories
parameter with "echarts4r".
The official Echarts documentation related to categories
: https://echarts.apache.org/en/option.html#visualMap-piecewise.categories
library(dplyr)
cns <- countrycode::codelist$country.name.en
cns <- data.frame(
country = cns,
category = sample(LETTERS[1:4], length(cns), replace=TRUE, prob=c(0.1, 0.2, 0.65, 0.05))
) |>
mutate(numbas= match(category,LETTERS)) |> # keep them in sync
relocate(numbas, .after= country) # default Y-axis is the second column
palet <- c('#8b0069','#75c165', '#ce5c5c', '#fbc357')
fmtr <- htmlwidgets::JS("function (x) { return x.data[0]+'<br>cat: <b>'+x.data[2]; }")
# https://echarts.apache.org/en/option.html#visualMap-piecewise.categories
cns |>
e_charts(country) |>
e_map(category) |>
e_visual_map(category,
type = "piecewise",
inRange= list(color= palet),
outOfRange = list(color= 'grey'),
categories = LETTERS[1:4], # NOT WORKING!!
dimension = 2) |>
e_tooltip(formatter = fmtr) # NOT WORKING!!
Here the solution to add the tooltip, given by the developer of the package: https://github.com/JohnCoene/echarts4r/issues/146#issuecomment-871160283
Hi John, Is it possible to create a choropleth world map with a categorical variable using echarts4r?
Here an reproducible example (not working):
I was not even able to find an choropleth map in the official Echarts docs.
If not possible, could Leaflet be used as a workaround?
Thanks again for this great package! Felix