library(highcharter)
## Highcharts (www.highcharts.com) is a Highsoft software product which is
## not free for commercial and Governmental use
library(gapminder)
## Warning: package 'gapminder' was built under R version 3.3.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
data("gapminder")
gapminder
## # A tibble: 1,704 x 6
##        country continent  year lifeExp      pop gdpPercap
##         <fctr>    <fctr> <int>   <dbl>    <int>     <dbl>
##  1 Afghanistan      Asia  1952  28.801  8425333  779.4453
##  2 Afghanistan      Asia  1957  30.332  9240934  820.8530
##  3 Afghanistan      Asia  1962  31.997 10267083  853.1007
##  4 Afghanistan      Asia  1967  34.020 11537966  836.1971
##  5 Afghanistan      Asia  1972  36.088 13079460  739.9811
##  6 Afghanistan      Asia  1977  38.438 14880372  786.1134
##  7 Afghanistan      Asia  1982  39.854 12881816  978.0114
##  8 Afghanistan      Asia  1987  40.822 13867957  852.3959
##  9 Afghanistan      Asia  1992  41.674 16317921  649.3414
## 10 Afghanistan      Asia  1997  41.763 22227415  635.3414
## # ... with 1,694 more rows
dim(gapminder)
## [1] 1704    6
dim(gapminder_unfiltered)
## [1] 3313    6
data_strt <- distinct(gapminder_unfiltered, country, continent, .keep_all = TRUE) %>% 
  mutate(x = lifeExp, y = gdpPercap, z = pop) %>% 
  left_join(
    data_frame(
      continent = names(continent_colors),
      color = continent_colors
      )
  ) %>% 
  mutate(color = colorize(continent))
## Joining, by = "continent"
## Warning: Column `continent` joining factor and character vector, coercing
## into character vector
## Warning: package 'bindrcpp' was built under R version 3.3.3
data_seqc <- gapminder_unfiltered %>% 
  arrange(country, year) %>% 
  group_by(country) %>% 
  do(sequence = list_parse(select(., x = lifeExp, y = gdpPercap, z = pop)))
  

data <- left_join(data_strt, data_seqc)  
## Joining, by = "country"
data
## # A tibble: 187 x 11
##        country continent  year lifeExp      pop  gdpPercap      x
##         <fctr>     <chr> <int>   <dbl>    <int>      <dbl>  <dbl>
##  1 Afghanistan      Asia  1952  28.801  8425333   779.4453 28.801
##  2     Albania    Europe  1952  55.230  1282697  1601.0561 55.230
##  3     Algeria    Africa  1952  43.077  9279525  2449.0082 43.077
##  4      Angola    Africa  1952  30.015  4232095  3520.6103 30.015
##  5   Argentina  Americas  1952  62.485 17876956  5911.3151 62.485
##  6     Armenia       FSU  1992  68.663  3378331  1442.9378 68.663
##  7       Aruba  Americas  1972  70.941    59461  4939.7580 70.941
##  8   Australia   Oceania  1950  69.020  8267337 10031.1214 69.020
##  9     Austria    Europe  1950  64.880  6935100  5733.0981 64.880
## 10  Azerbaijan      Asia  1992  65.623  7413618  3455.5430 65.623
## # ... with 177 more rows, and 4 more variables: y <dbl>, z <int>,
## #   color <chr>, sequence <list>
data$sequence[[1]]
## [[1]]
## [[1]]$x
## [1] 28.801
## 
## [[1]]$y
## [1] 779.4453
## 
## [[1]]$z
## [1] 8425333
## 
## 
## [[2]]
## [[2]]$x
## [1] 30.332
## 
## [[2]]$y
## [1] 820.853
## 
## [[2]]$z
## [1] 9240934
## 
## 
## [[3]]
## [[3]]$x
## [1] 31.997
## 
## [[3]]$y
## [1] 853.1007
## 
## [[3]]$z
## [1] 10267083
## 
## 
## [[4]]
## [[4]]$x
## [1] 34.02
## 
## [[4]]$y
## [1] 836.1971
## 
## [[4]]$z
## [1] 11537966
## 
## 
## [[5]]
## [[5]]$x
## [1] 36.088
## 
## [[5]]$y
## [1] 739.9811
## 
## [[5]]$z
## [1] 13079460
## 
## 
## [[6]]
## [[6]]$x
## [1] 38.438
## 
## [[6]]$y
## [1] 786.1134
## 
## [[6]]$z
## [1] 14880372
## 
## 
## [[7]]
## [[7]]$x
## [1] 39.854
## 
## [[7]]$y
## [1] 978.0114
## 
## [[7]]$z
## [1] 12881816
## 
## 
## [[8]]
## [[8]]$x
## [1] 40.822
## 
## [[8]]$y
## [1] 852.3959
## 
## [[8]]$z
## [1] 13867957
## 
## 
## [[9]]
## [[9]]$x
## [1] 41.674
## 
## [[9]]$y
## [1] 649.3414
## 
## [[9]]$z
## [1] 16317921
## 
## 
## [[10]]
## [[10]]$x
## [1] 41.763
## 
## [[10]]$y
## [1] 635.3414
## 
## [[10]]$z
## [1] 22227415
## 
## 
## [[11]]
## [[11]]$x
## [1] 42.129
## 
## [[11]]$y
## [1] 726.7341
## 
## [[11]]$z
## [1] 25268405
## 
## 
## [[12]]
## [[12]]$x
## [1] 43.828
## 
## [[12]]$y
## [1] 974.5803
## 
## [[12]]$z
## [1] 31889923
summarise_if(gapminder, is.numeric, funs(min, max)) %>% 
  tidyr::gather(key, value) %>% 
  arrange(key)
## # A tibble: 8 x 2
##             key        value
##           <chr>        <dbl>
## 1 gdpPercap_max 1.135231e+05
## 2 gdpPercap_min 2.411659e+02
## 3   lifeExp_max 8.260300e+01
## 4   lifeExp_min 2.359900e+01
## 5       pop_max 1.318683e+09
## 6       pop_min 6.001100e+04
## 7      year_max 2.007000e+03
## 8      year_min 1.952000e+03
highchart() %>% 
  hc_add_series(data = data, type = "bubble",
                minSize = 0, maxSize = 30) %>% 
  hc_motion(enabled = TRUE, series = 0, labels = unique(gapminder$year),
            loop = TRUE, autoPlay = TRUE, 
            updateInterval = 1000, magnet = list(step =  1)) %>% 
  hc_plotOptions(series = list(showInLegend = FALSE)) %>% 
  hc_xAxis(min = 20, max = 90) %>% 
  hc_yAxis(type = "logarithmic", min = 100, max = 100000) %>% 
  hc_add_theme(hc_theme_smpl())