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

support Animated Grouped Bubble Chart? #435

Closed
XiangyunHuang opened this issue Jun 8, 2022 · 7 comments
Closed

support Animated Grouped Bubble Chart? #435

XiangyunHuang opened this issue Jun 8, 2022 · 7 comments

Comments

@XiangyunHuang
Copy link

XiangyunHuang commented Jun 8, 2022

I want to create an Animated Grouped Bubble Chart, but I don't know how to do that. This is my attempt

1. simulate some fake data

# some fake data
library(echarts4r)
set.seed(2022)
dat <- data.frame(
  x1 = rep(2020:2023, each = 4),
  x2 = rep(c("A", "A", "B", "B"), 4),
  x3 = runif(16),
  x4 = runif(16),
  x5 = abs(runif(16))
)
dat
     x1 x2       x3     x4      x5
1  2020  A 0.815978 0.7732 0.25652
2  2020  A 0.647259 0.5145 0.40536
3  2020  B 0.120329 0.5954 0.17088
4  2020  B 0.543800 0.8509 0.06798
5  2021  A 0.184730 0.8428 0.84603
6  2021  A 0.635791 0.5703 0.54988
7  2021  B 0.074299 0.4266 0.80484
8  2021  B 0.041976 0.1372 0.76506
9  2022  A 0.370317 0.1631 0.64224
10 2022  A 0.757253 0.9351 0.44170
11 2022  B 0.001862 0.5370 0.64938
12 2022  B 0.159799 0.9977 0.52454
13 2023  A 0.144742 0.4790 0.86723
14 2023  A 0.519397 0.7736 0.10065
15 2023  B 0.609476 0.4680 0.88715
16 2023  B 0.122511 0.6376 0.62795

2. try to create Animated Grouped Bubble Chart

I'm able to create Grouped Bubble Chart and Animated Bubble Chart.

# **Grouped Bubble Chart**
dat |>
  group_by(x1) |>
  e_charts(x = x3) |>
  e_scatter(serie = x5, size = x4, symbol_size = 5) |>
  e_tooltip()

截屏2022-06-08 23 18 14

# **Animated Bubble Chart**
dat |>
  group_by(x1) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(serie = x5, size = x4, symbol_size = 5) |>
  e_tooltip()

截屏2022-06-08 23 17 47

But, I failed to create Animated Grouped Bubble Chart. when I add x2 into group_by(), Is there any other way to do that?

dat |>
  group_by(x1, x2) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(serie = x5, size = x4, symbol_size = 5) |>
  e_tooltip()

截屏2022-06-08 23 17 30

Session info

xfun::session_info(packages = "echarts4r", dependencies = F)
#> R version 4.2.0 (2022-04-22)
#> Platform: x86_64-apple-darwin17.0 (64-bit)
#> Running under: macOS Big Sur/Monterey 10.16
#> 
#> Locale: en_US.UTF-8 / en_US.UTF-8 / en_US.UTF-8 / C / en_US.UTF-8 / en_US.UTF-8
#> 
#> Package version:
#>   echarts4r_0.4.4

Created on 2022-06-08 by the reprex package (v2.0.1)

@XiangyunHuang XiangyunHuang changed the title How to create Animated Grouped Bubble Chart support Animated Grouped Bubble Chart? Jun 16, 2022
@rdatasculptor
Copy link
Contributor

rdatasculptor commented Jul 1, 2022

@XiangyunHuang (@JohnCoene) I think you can do this in echarts4r as well. No new features are needed I think.

Given your examnple I was not entirely sure which output you intended, to be honest. So I made two possible animated, grouped bubbel charts. Pick the one you want :)

library(echarts4r)
library(tidyverse)
set.seed(2022)
dat <- data.frame(
  x1 = rep(2020:2023, each = 4),
  x2 = rep(c("A", "A", "B", "B"), 4),
  x3 = runif(16),
  x4 = runif(16),
  x5 = abs(runif(16))
)

## Bubble chart 1
datlist <- list()
years <- 2020:2023
for (i in 1:length(years)){
  df <- dat |> filter(x1 == years[i]) |> select(-x1)
  names(df)[length(df)] <- years[i]
  datlist[[i]] <- df 
}
dat2 <- bind_rows(datlist)
dat2 |>
  group_by(x2) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(`2020`, size = x4, symbol_size = 5) |>
  e_scatter(`2021`, size = x4, symbol_size = 5) |>
  e_scatter(`2022`, size = x4, symbol_size = 5) |>
  e_scatter(`2023`, size = x4, symbol_size = 5) |>
  e_tooltip()

# Bubble chart 2
datlist <- list()
ab <- c("A", "B")
for (i in 1:length(ab)){
  df <- dat |> filter(x2 == ab[i]) |> select(-x2)
  names(df)[length(df)] <- ab[i]
  datlist[[i]] <- df 
}
dat2 <- bind_rows(datlist)
dat2 |>
  group_by(x1) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(A, size = x4, symbol_size = 5) |>
  e_scatter(B, size = x4, symbol_size = 5) |>
  e_tooltip()

Did this help you? Good luck!

If this was the disired outcome, then you can close the issue.

@XiangyunHuang
Copy link
Author

XiangyunHuang commented Jul 1, 2022

@rdatasculptor Below is what I want. It is created by helgasoft using echarty.

@rdatasculptor
Copy link
Contributor

Is'nt that my second bubble chart?

Also you can add a changing title, like this:

dat2 |>
  group_by(x1) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(A, size = x4, symbol_size = 5) |>
  e_scatter(B, size = x4, symbol_size = 5) |>
  e_tooltip() |>
  e_timeline_serie(
    title = list(
      list(text = "2020"),
      list(text = "2021"),
      list(text = "2022"),
      list(text = "2023")
    )
  ) 

@XiangyunHuang if it is still not what wanted, can you point me at the differences?

@XiangyunHuang
Copy link
Author

@rdatasculptor Thanks, your solution is good. Is there any other simpler method to reshape dat to dat2?

@rdatasculptor
Copy link
Contributor

rdatasculptor commented Jul 1, 2022

@XiangyunHuang Another way is using e.g. pivot_wider.
I sometimes use loops only for me to understand what's happening. But I guess that is not helpful for everyone :)

by using pivot_wider:

dat2 <- dat |> pivot_wider(names_from = "x2", values_from = "x5")
dat2 |>
  group_by(x1) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(A, size = x4, symbol_size = 5) |>
  e_scatter(B, size = x4, symbol_size = 5) |>
  e_tooltip() |>
  e_timeline_serie(
    title = list(
      list(text = "2020"),
      list(text = "2021"),
      list(text = "2022"),
      list(text = "2023")
    )
  ) 

Also you can play with symbol size, because that's where the main difference is with @helgasoft approach is I think.

@rdatasculptor
Copy link
Contributor

rdatasculptor commented Jul 1, 2022

@XiangyunHuang playing with symbol size using scaling I mean:

dat2 <- dat |> pivot_wider(names_from = "x2", values_from = "x5")

my_scale <- function(x){
  scales::rescale(x, to = c(20, 50))
}

dat2 |>
  group_by(x1) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(A, size = x4, scale = my_scale) |>
  e_scatter(B, size = x4, scale = my_scale) |>
  e_tooltip() |>
  e_timeline_serie(
    title = list(
      list(text = "2020"),
      list(text = "2021"),
      list(text = "2022"),
      list(text = "2023")
    )
  ) 

@XiangyunHuang
Copy link
Author

Thanks! Just for myself, we can use Base R's reshape() function instead of tidyr's pivot_wider() function to convert dat to dat2.

library(echarts4r)

set.seed(2022)
dat <- data.frame(
  x1 = rep(2020:2023, each = 4),
  x2 = rep(c("A", "A", "B", "B"), 4),
  x3 = runif(16),
  x4 = runif(16),
  x5 = abs(runif(16))
)

dat2 <- reshape(
  data = dat,
  varying = c("A", "B"),
  timevar = c("x2"),
  idvar = c("x1", "x3", "x4"),
  direction = "wide"
)

dat2 |>
  group_by(x1) |>
  e_charts(x = x3, timeline = TRUE) |>
  e_scatter(A, size = x4, symbol_size = 5) |>
  e_scatter(B, size = x4, symbol_size = 5) |>
  e_tooltip() |>
  e_timeline_serie(
    title = list(
      list(text = "2020"),
      list(text = "2021"),
      list(text = "2022"),
      list(text = "2023")
    )
  )

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