Highcharts motion plugin - Requires 3 adjustments to a highchart.
- The inclusion of the js asset
- An option object for
motion
- Data to be within
sequence
arrays.
It seems like there are two main R
wrappers for Highcharts
. Ramnath's rCharts
and the recently released on CRAN highcharter
.
So my question: is it possible to animate a bubble highchart over time with the currently available wrappers and if so how?
rCharts Attempt 1 Starting with a bubble chart and introducing the 3 required motion options:
library(rCharts) # highcharts wrapper hPlot()
# data
set.seed(1)
df.SO <- data.frame(date = sample(2005:2016, 21, replace = T)
, x = rnorm(21, 10, 4)
, y = rnorm(21, 150, 4)
, z = rbinom(21, 80, .8)
, entities = sample(c("entity1","entity2","entity3"), 21, replace = T))
# chart
h1 <- hPlot( x = "x"
, y = "y"
, size = "z"
, group = "entities"
, data = df.SO
, type = "bubble")
### Motion Charts plugin ###
## 1. include motion js asset in head
h1$addAssets(jshead = "https://rawgit.com/larsac07/Motion-Highcharts-Plugin/master/motion.js")
## 2. add motion object
h1$params$motion <- list(enabled = "true",
labels = unique(sort(df.SO$date)),
loop = "true",
series = 1,
updateInterval = 50,
magnet = list(
round = "round",
step = 0.1))
## 3. sequence data?? Dead end approach??
# view chart - displays bubbles and widget to play animation, but animation fails
print(h1)
rCharts - Attempt 2 Restructure data as sequences then feed into chart.
# 3. sequence data - cast data so entities are series and times are unique entries
library(data.table) ## v >= 1.9.6
test <- dcast(setDT(df.SO), date ~ entities, value.var = c("x", "y", "z"))
# chart
h1 <- Highcharts$new()
h1$chart(type = "bubble", height = 300)
h1$series(
list(name = "entity1",
data = list(
sequence = test$x_length_entity1,
sequence = test$y_length_entity1,
sequence = test$z_length_entity1
)
),
list(name = "entity2",
data = list(
sequence = test$x_length_entity2,
sequence = test$y_length_entity2,
sequence = test$z_length_entity2
)
), replace = T)
## 1. include motion js asset in head
h1$addAssets(jshead = "https://rawgit.com/larsac07/Motion-Highcharts-Plugin/master/motion.js")
## 2. add motion object
h1$params$motion <- list(enabled = "true",
labels = unique(sort(test$date)),
loop = "true",
series = 1,
updateInterval = 50,
magnet = list(
round = "round",
step = 0.1))
# view chart - this approach doesn't display any bubbles
print(h1)