How do I enable an R-wrapped bubble chart engine plugin?
Highcharts Generation Plugin - Requires 3 adjustments to highchart.
- Including a js resource
- Option object for
motion
- The data is in an array
sequence
.
There seems to Highcharts
be two main wrappers for R
. Ramnath rCharts
and recently released on CRAN highcharter
.
So my question is, is it possible to animate a bubble chart 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 parameters:
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 the data as sequences, then load it into a 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)
source to share
Luke,
motion.js
added plugin to highcharter
. Is the development version (download via devtools
) it needs more testing, but this is a start.
Please check an example at http://jkunst.com/highcharter/plugins.html#motion :
highchart() %>%
hc_chart(type = "column") %>%
hc_yAxis(max = 6, min = 0) %>%
hc_add_series(name = "A", data = c(2,3,4), zIndex = -10) %>%
hc_add_series(name = "B",
data = list(
list(sequence = c(1,2,3,4)),
list(sequence = c(3,2,1,3)),
list(sequence = c(2,5,4,3))
)) %>%
hc_add_series(name = "C",
data = list(
list(sequence = c(3,2,1,3)),
list(sequence = c(2,5,4,3)),
list(sequence = c(1,2,3,4))
)) %>%
hc_motion(enabled = TRUE,
labels = 2000:2003,
series = c(1,2))
If you find any suspicion (like bugs) please report here: https://github.com/jbkunst/highcharter/issues
Hope it helps
source to share
Have you considered shiny as a workaround?
library(shiny)
library(rCharts)
library(dplyr)
server <- shinyServer(function(input, output) {
output$bubblePlot <- renderChart2({
# 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))
# filter data based on selected year
df.SO.select <- dplyr::filter(df.SO, date == input$date)
# chart
h1 <- hPlot( x = "x"
, y = "y"
, size = "z"
, group = "entities"
, data = df.SO.select
, type = "bubble")
h1$addParams(dom = "bubbleChart")
h1$plotOptions(series = list(animation = FALSE))
h1$xAxis(min = min(df.SO$x), max = max(df.SO$x))
h1$yAxis(min = min(df.SO$y), max = max(df.SO$y))
h1
})
})
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Highcharts Bubble Motion Chart"),
# Sidebar with a slider input for the selected year
sidebarLayout(
sidebarPanel(
sliderInput("date",
"Date:",
min = 2007,
max = 2016,
value = 2007,
animate = TRUE,
sep = "")
),
# Show a bubble plot for the selected year
mainPanel(
showOutput("bubblePlot", "highcharts")
)
)
))
shinyApp(ui = ui, server = server)
source to share
Surprisingly, I have not been able to find a single posted example that demonstrates bubble or scatter as a chart type using hc_motion. All examples: bar, column, map, pie.
Anyway, after a fair hit and miss, I managed to get a scatter plot working on the following order. each vector c (1,1) represents the x and y coordinates. I am still trying to get it working with a bubble with no luck. Hoping that someone can post the code snippet for the bubble using hcaes () format.
Bubble would be handy for movement as then you can use Gapminder style charts. http://www.gapminder.org/
library(highcharter)
highchart() %>%
hc_chart(type = "scatter") %>%
hc_yAxis(max = 6, min = 0) %>%
hc_xAxis(max = 6, min = 0) %>%
hc_add_series(name = "Australia",
data = list(
list(sequence = list(c(1,1),c(2,2),c(3,3),c(4,4)))
)) %>%
hc_add_series(name = "United States",
data = list(
list(sequence = list(c(0,0),c(3,2),c(4,3),c(4,1)))
)) %>%
hc_add_series(name = "China",
data = list(
list(sequence = list(c(3,2),c(2,2),c(1,1),c(2,5)))
)) %>%
hc_motion(enabled = TRUE,
labels = 2000:2003,
series = c(0,1,2))
source to share