Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pie charts with coord_system = "geo" #511

Closed
FlavioLeccese92 opened this issue Mar 29, 2023 · 5 comments
Closed

Pie charts with coord_system = "geo" #511

FlavioLeccese92 opened this issue Mar 29, 2023 · 5 comments

Comments

@FlavioLeccese92
Copy link

FlavioLeccese92 commented Mar 29, 2023

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
Copy link
Owner

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
Copy link
Owner

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

@JohnCoene
Copy link
Owner

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

@FlavioLeccese92
Copy link
Author

FlavioLeccese92 commented Mar 30, 2023

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants