library(highcharter)
library(dplyr)
library(purrr)
years <- 10
nx <- 5
ny <- 6
df <- data_frame(year = rep(c(2016 + 1:years - 1), each = nx * ny),
xVar = rep(1:nx, times = years * ny),
yVar = rep(1:ny, times = years * nx))
df <- df %>%
group_by(xVar, yVar) %>%
mutate(heatVar = cumsum(rnorm(length(year))))
df_start <- df %>%
arrange(year) %>%
distinct(xVar, yVar, .keep_all = TRUE)
df_start
## Source: local data frame [30 x 4]
## Groups: xVar, yVar [30]
##
## year xVar yVar heatVar
## <dbl> <int> <int> <dbl>
## 1 2016 1 1 -0.60610722
## 2 2016 2 2 0.39672493
## 3 2016 3 3 0.23764936
## 4 2016 4 4 -0.28602598
## 5 2016 5 5 -0.54167398
## 6 2016 1 6 -1.35607586
## 7 2016 2 1 0.01522277
## 8 2016 3 2 -0.01679639
## 9 2016 4 3 1.38049057
## 10 2016 5 4 -1.62184441
## # ... with 20 more rows
df_seqc <- df %>%
group_by(xVar, yVar) %>%
do(sequence = list_parse(select(., value = heatVar)))
df_seqc
## Source: local data frame [30 x 3]
## Groups: <by row>
##
## # A tibble: 30 × 3
## xVar yVar sequence
## * <int> <int> <list>
## 1 1 1 <list [10]>
## 2 1 2 <list [10]>
## 3 1 3 <list [10]>
## 4 1 4 <list [10]>
## 5 1 5 <list [10]>
## 6 1 6 <list [10]>
## 7 2 1 <list [10]>
## 8 2 2 <list [10]>
## 9 2 3 <list [10]>
## 10 2 4 <list [10]>
## # ... with 20 more rows
data <- left_join(df_start, df_seqc)
data
## Source: local data frame [30 x 5]
## Groups: xVar, yVar [?]
##
## year xVar yVar heatVar sequence
## <dbl> <int> <int> <dbl> <list>
## 1 2016 1 1 -0.60610722 <list [10]>
## 2 2016 2 2 0.39672493 <list [10]>
## 3 2016 3 3 0.23764936 <list [10]>
## 4 2016 4 4 -0.28602598 <list [10]>
## 5 2016 5 5 -0.54167398 <list [10]>
## 6 2016 1 6 -1.35607586 <list [10]>
## 7 2016 2 1 0.01522277 <list [10]>
## 8 2016 3 2 -0.01679639 <list [10]>
## 9 2016 4 3 1.38049057 <list [10]>
## 10 2016 5 4 -1.62184441 <list [10]>
## # ... with 20 more rows
limits <- (unlist(data$sequence)) %>%
{ c(min(.), max(.))}
limits
## [1] -7.481183 5.286344
hc1 <- hchart(data, type = "heatmap", hcaes(x = xVar, y = yVar, value = heatVar))
hc2 <- hchart(data, type = "heatmap", hcaes(x = xVar, y = yVar, value = heatVar)) %>%
hc_motion(enabled = TRUE, series = 0, startIndex = 0,
labels = unique(df$year)) %>%
hc_legend(layout = "vertical", verticalAlign = "top", align = "right") %>%
hc_colorAxis(min = limits[1], max = limits[2])
hc2