JohnCoene / echarts4r

🐳 ECharts 5 for R
http://echarts4r.john-coene.com/
Other
595 stars 81 forks source link

Pie charts with coord_system = "geo" #511

Closed FlavioLeccese92 closed 1 year ago

FlavioLeccese92 commented 1 year ago

Hi, thank you again for the incredible work you are doing!

I'm currently struggling with pie charts on a map. As I usually do for more complex charts, I try to replicate in R the javascript structure from official examples on the echarts website and use the e_list() function your package provides. Often it works smoothly but in some situation I really cannot understand what's wrong. For instance here. I share the draft code in R:

flights <- read.csv(
  paste0(
    "https://raw.githubusercontent.com/plotly/datasets/",
    "master/2011_february_aa_flight_paths.csv"
  )
) %>% head(30)

series = list()
for(i in 1:length(unique(flights$airport1))){
  temp_inner = flights %>% filter(airport1 == unique(flights$airport1)[i])
  ### added 100 to coords to check if the series are created and they are
  center = list(temp_inner$start_lon[1] + 100, temp_inner$start_lat[1] + 100)
  name = temp_inner$airport1[1]
  temp_list = list()
  for(j in 1:nrow(temp_inner)){
    temp_list[[j]] = list(value = temp_inner$cnt[j], name = temp_inner$airport2[j])
  }
  series[[i]] = list(data = temp_list,
                     type = "pie", coordinateSystem = "geo",
                     tooltip = list(formatter = "{b}: {c} ({d}%)"),
                     center = center, name = name, radius = 30)
}

opts = list(geo = list(map = "world", roam = TRUE, center = FALSE),
            series = series,
            tooltip = list(),
            legend = list(show = TRUE))

e_charts(width = NULL, height = NULL) %>% e_list(opts) -> e

path <- system.file("htmlwidgets/lib/echarts-4.8.0", package = "echarts4r")
dep <- htmltools::htmlDependency(
  name = "echarts-world",
  version = "1.0.0",
  src = c(file = path),
  script = "world.js"
)

e$dependencies <- append(e$dependencies, list(dep))
e

Is there something wrong? Is it due to the world.js? Lines works pretty fine thought... Any suggestion?

Thank you!!

JohnCoene commented 1 year ago

It required an updated version of the dependency.

A simplified version of your example: note the correction of the pie center longitude = x and latitude = y

library(dplyr)

flights <- read.csv(
  paste0(
    "https://raw.githubusercontent.com/plotly/datasets/",
    "master/2011_february_aa_flight_paths.csv"
  )
) |> head(30)

series <- flights |>
  select(airport1, start_lat, start_lon) |>
  distinct() |>
  apply(1, as.list) |>
  purrr::map(\(row) {
    data <- lapply(LETTERS[1:4], \(l) {
      list(name = l, value = sample(1:10, 1))   
    })

    list(
      data = data,
      type = "pie", 
      coordinateSystem = "geo",
      tooltip = list(formatter = "{b}: {c} ({d}%)"),
      center = list(row$start_lon, row$start_lat), 
      radius = data |>
        purrr::map("value") |> 
        purrr::reduce(\(c, p) {
          c + p
        })
    )
  })

opts <- list(
  geo = list(
    map = "world", 
    roam = TRUE,
    itemStyle = list(
      areaColor = "#e7e8ea"
    )
  ),
  series = series,
  tooltip = list(),
  legend = list()
)

e <- e_charts(width = NULL, height = NULL) |> e_list(opts)

path <- system.file("htmlwidgets/lib/echarts-4.8.0", package = "echarts4r")
dep <- htmltools::htmlDependency(
  name = "echarts-world",
  version = "1.0.0",
  src = c(file = path),
  script = "world.js"
)

e$dependencies <- append(e$dependencies, list(dep))
e
JohnCoene commented 1 year ago

Reinstall to get the latest version of {echarts4r} and try again, feel free to reopen if this does not work.

JohnCoene commented 1 year ago

@rdatasculptor I should add that as part of the standard API, not sure if it's already possible (with latest version - just updated)

FlavioLeccese92 commented 1 year ago

Removing and reinstalling the development version worked! Thank you again!

Edit: Actually, now the chart above works BUT when running the Les Miserables e_graph(layout = "circular",... example, nodes don't show up. @JohnCoene were you suggesting to install the latest dev version or the latest CRAN one? Latest CRAN echarts4r (0.4.4) won't show the pies on the map. Any suggestion? (cannot reopen this issue). Thank you again

JohnCoene commented 1 year ago

Latest dev from GitHub, I had to update the js dependencies for this pie-geo feature to work