JohnCoene / echarts4r

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

How to create bubble plot with timeline by echarts4r? #525

Closed Robinmoon26 closed 1 year ago

Robinmoon26 commented 1 year ago

I tried to create a bubble plot with timeline as below:

library(echarts4r)
library(tidyverse)

df<-data.frame(siteid=c(rep(1,3),rep(2,3),rep(3,3)),
           year=rep(c(2001,2005,2010),3),
           population=c(c(10,30,40),c(15,35,50),c(12,40,60)),
           dis=c(c(2,5,10),c(2,0,0),c(0,5,10)))

df |> 
  group_by(year)|> 
  e_charts(population,timeline = TRUE) |>
  e_scatter(dis,population)

However, there was nothing showed up.

In general, I'd like to know how to plot it in the right way. I'd like the bubble size determined by the different values of population and color determined by siteid while making the x and y axis to be stabilized, so that I could see the bubbles growing and moving through the timeline.

rdatasculptor commented 1 year ago

I guess te main problem in this script is that you use the population variable for both y axis and bubble size.

library(echarts4r)
library(tidyverse)

df<-data.frame(siteid=c(rep(1,3),rep(2,3),rep(3,3)),
               year=rep(c(2001,2005,2010),3),
               population=c(c(10,30,40),c(15,35,50),c(12,40,60)),
               dis=c(c(2,5,10),c(2,0,0),c(0,5,10))) |>
  mutate(population2 = population)

df |> 
  group_by(year)|> 
  e_charts(population,timeline = TRUE) |>
  e_scatter(dis,population2) |>
  e_x_axis(min = min(df$population2), max = max(df$population2)) |>
  e_y_axis(min = min(df$dis), max = max(df$dis))

Note that I also froze the extremes of the axis. Another problem is that echarts 'thinks' that the entities with the same size are one and the same individual bubble through the years. You can change that for example with:

library(echarts4r)
library(tidyverse)

df<-data.frame(siteid=c(rep(1,3),rep(2,3),rep(3,3)),
               year=rep(c(2001,2005,2010),3),
               population=c(c(10,30,40),c(15,35,50),c(12,40,60)),
               dis=c(c(2,5,10),c(2,0,0),c(0,5,10)))

df |> 
  group_by(year)|> 
  e_charts(siteid,timeline = TRUE) |>
  e_scatter(dis,population) |>
  e_x_axis(min = min(df$siteid), max = max(df$siteid)) |>
  e_y_axis(min = min(df$dis), max = max(df$dis))

At least it seems to show kind of the effect you wanted. There are ways to make the bubble more prominent by using e.g. the symbolSize argument and e_add_unnested().

Like this:

library(echarts4r)
library(tidyverse)

df<-data.frame(siteid=c(rep(1,3),rep(2,3),rep(3,3)),
               year=rep(c(2001,2005,2010),3),
               population=c(c(10,30,40),c(15,35,50),c(12,40,60)),
               dis=c(c(2,5,10),c(2,0,0),c(0,5,10)))

df |> 
  group_by(year)|> 
  e_charts(siteid,timeline = TRUE) |>
  e_scatter(dis) |>
  e_x_axis(min = min(df$siteid), max = max(df$siteid)) |>
  e_y_axis(min = min(df$dis), max = max(df$dis)) %>%
  e_add_unnested("symbolSize", population)

I am not sure if this solved your problems entirely, but if it does, please close the issue again. Thanks!

JohnCoene commented 1 year ago

Thanks @rdatasculptor !

Robinmoon26 commented 1 year ago

Thanks @rdatasculptor, it really helps a lot!

I edited your code a little bit, and this is what I want so far:


library(echarts4r)
library(tidyverse)

df<-data.frame(siteid=c(rep(1,3),rep(2,3),rep(3,3)),
               year=rep(c(2001,2005,2010),3),
               population=c(c(10,30,40),c(15,35,50),c(12,40,60)),
               dis=c(c(2,5,10),c(2,0,0),c(0,5,10))) |>
    mutate(population2 = population)

df |> 
  group_by(year)|> 
  e_charts(population,timeline = TRUE) |>
  e_scatter(dis,population2) |>
  e_x_axis(min = min(df$population2), max = max(df$population2)) |>
  e_y_axis(min = min(df$dis), max = max(df$dis)) |>
  e_add_unnested("symbolSize", population)

There is only one question left. How to assign different color to the dots based on siteid? It would be great if I could put the name of siteid in the middle of the dots.

rdatasculptor commented 1 year ago

For the coloring of the dots you can make an extra column in your data frame containing the color codes. Just give each siteid value its own color. After that use e_add_nested() and its color parameter to assign the colors. I think there is a good example on @JohnCoene's documentation websitte regarding e_add_nested.

For labeling you can e.g. experiment with the label parameter.