Chapter 6 Timeline 的应用

# fake data
df <- data.frame(
  year = c(
    rep(2016, 25),
    rep(2017, 25),
    rep(2018, 25),
    rep(2019, 25)
  ),
  x = rnorm(100),
  y = rnorm(100),
  grp = c(
    rep("A", 50),
    rep("B", 50)
  )
) 
df
##     year            x            y grp
## 1   2016 -0.296782623  0.718985265   A
## 2   2016  0.312793305  0.677681461   A
## 3   2016  1.817075245 -1.104529717   A
## 4   2016  0.238115698 -0.006028082   A
## 5   2016  1.350354507 -0.808396383   A
## 6   2016  0.118038429 -0.243130048   A
## 7   2016  0.050326459  1.197025864   A
## 8   2016 -1.037839953 -1.176511611   A
## 9   2016  1.020651149  0.005653544   A
## 10  2016 -0.524535306  1.274133550   A
## 11  2016 -0.565236275  1.437384284   A
## 12  2016 -1.291292641  2.597664494   A
## 13  2016 -0.270646262  2.281484805   A
## 14  2016  1.219938867 -0.097324732   A
## 15  2016 -0.901850050 -0.494629622   A
## 16  2016 -1.062584092 -0.465067677   A
## 17  2016  0.000570845  0.312191443   A
## 18  2016  0.419383946 -0.702976334   A
## 19  2016  0.289888222  0.246592144   A
## 20  2016  0.234681967 -1.486379952   A
## 21  2016 -0.999642762 -0.598242739   A
## 22  2016 -0.106152245  1.470907051   A
## 23  2016 -0.005350965  0.423576567   A
## 24  2016  0.935846847  0.646514821   A
## 25  2016 -0.029422237 -0.856781516   A
## 26  2017  1.632636209 -1.344829678   A
## 27  2017 -0.197441407 -0.794504261   A
## 28  2017  1.092323453  2.021271984   A
## 29  2017  1.583604321  0.591413528   A
## 30  2017  0.644523190 -0.382952293   A
## 31  2017  2.208418916 -1.759460582   A
## 32  2017 -0.663672097  1.163947064   A
## 33  2017  1.004204049  0.511043740   A
## 34  2017 -0.075935451 -0.156142616   A
## 35  2017 -0.207016525 -2.257377565   A
## 36  2017  0.701250690 -0.410791783   A
## 37  2017 -0.068560873 -0.116472806   A
## 38  2017  0.740448741 -0.591395856   A
## 39  2017 -0.541359687 -0.524522219   A
## 40  2017 -1.399168389 -0.037624141   A
## 41  2017 -0.669938865  1.467127602   A
## 42  2017  0.455138452 -2.242708376   A
## 43  2017 -0.264072274  0.053960919   A
## 44  2017  1.384445567  1.404407274   A
## 45  2017 -0.555434957 -0.894484722   A
## 46  2017 -1.710709394  1.205762662   A
## 47  2017 -0.627077016  0.113297004   A
## 48  2017 -1.481589795  0.560303971   A
## 49  2017 -1.170346444  0.816909398   A
## 50  2017 -1.143187678 -0.341919468   A
## 51  2018  0.251725974  0.288196244   B
## 52  2018  1.618166167  0.565898911   B
## 53  2018  0.330343357  0.587254934   B
## 54  2018 -0.387089294 -0.249830381   B
## 55  2018 -0.203429469  1.518763408   B
## 56  2018 -0.353292946 -0.670490604   B
## 57  2018 -0.933930611 -1.238365741   B
## 58  2018 -1.526409672  0.488875380   B
## 59  2018 -0.503692449 -1.465528820   B
## 60  2018  1.865257330  3.029477021   B
## 61  2018  0.667498117 -1.926520364   B
## 62  2018  0.212699614 -0.771956884   B
## 63  2018 -0.853166896  1.180006960   B
## 64  2018 -1.010269135  0.288282745   B
## 65  2018  0.747310451 -0.361486477   B
## 66  2018  1.481153996 -1.415105562   B
## 67  2018 -0.140419074  0.643557225   B
## 68  2018 -0.605700438 -0.021985744   B
## 69  2018  1.295516709  0.246057156   B
## 70  2018 -1.384787454 -1.018143554   B
## 71  2018 -0.915316498 -1.685467400   B
## 72  2018 -0.423887379 -1.334321526   B
## 73  2018  1.793131286  1.382982274   B
## 74  2018 -0.227629808  0.119007662   B
## 75  2018 -0.810225424 -0.613609946   B
## 76  2019  0.178838356  0.748382197   B
## 77  2019  0.107159238 -0.304321839   B
## 78  2019  2.212073491 -0.758382377   B
## 79  2019 -0.796318561  0.251677897   B
## 80  2019  1.815386659  0.724524796   B
## 81  2019  0.346248382 -0.639431921   B
## 82  2019  0.535964787  0.318571510   B
## 83  2019 -0.645245715 -0.022188096   B
## 84  2019 -0.351599736 -0.740556649   B
## 85  2019  0.115510581  1.531348381   B
## 86  2019 -0.545631155  1.926177007   B
## 87  2019 -0.432295049 -0.442586302   B
## 88  2019  1.220507120  1.461153205   B
## 89  2019  0.852704640  1.559490496   B
## 90  2019  0.228705251  1.619696795   B
## 91  2019  0.409849456  1.880508765   B
## 92  2019  0.209431976 -0.235924967   B
## 93  2019  0.179461875  0.393598332   B
## 94  2019 -1.034125923 -0.108949748   B
## 95  2019  0.183910812 -0.892360308   B
## 96  2019 -0.789447234  0.351797758   B
## 97  2019 -1.723376053  1.135834258   B
## 98  2019 -2.035568854 -0.916957319   B
## 99  2019  0.077780134  0.771851422   B
## 100 2019 -0.640441867  1.893584653   B
df %>%
  group_by(year) %>% 
  e_charts(x, timeline = T) %>% 
  e_scatter(y, symbol_size = 5)
df %>%
  group_by(year) %>% 
  e_charts(x, timeline = T) %>% 
  e_scatter(y, symbol_size = 5) %>% 
  e_lm(y ~ x)
df %>%
  group_by(year) %>% 
  e_charts(x, timeline = T) %>% 
  e_scatter(y, symbol_size = 5) %>% 
  e_datazoom() %>%
  e_loess(y ~ x)

6.1 map timeline

library(echarts4r.maps)

df <- USArrests

# scale 0 to 1
.scl <- function(x){
    (x - min(x)) / (max(x) - min(x))
}

df %>% 
  mutate(
      State = row.names(.),
      Rape = .scl(Rape),
      Murder = .scl(Murder),
      Assault = .scl(Assault)
  ) %>% 
  select(State, Rape, Murder, Assault) %>% 
  # group_by(State) %>% 
  # tidyr::gather("Key",  "Value", Murder, Rape, Assault) %>% 
  pivot_longer(
    -c(State),
    names_to = "Key",
    values_to = "Value"
  ) %>% 
  group_by(Key) %>% 
  e_charts(State, timeline = TRUE) %>% 
  em_map("USA") %>% 
  e_map(Value, map = "USA") %>% 
  e_visual_map(min = 0, max = 1) %>% 
  e_timeline_opts(autoPlay = TRUE) %>% 
  e_timeline_serie(
    title = list(
      list(text = "Assault", subtext = "Percentage based on arrests"),
      list(text = "Murder", subtext = "Percentage based on arrests"),
      list(text = "Rape", subtext = "Percentage based on arrests")
    )
  )

6.2 利用timeline实现动态条形图,效果相比gganimate要好些

示例链接