I'm trying to use the highcharter
R package "Motion Plugin", to make a Motion Chart for a heatmap. I.e. I would like a heatmap to change over time, using a slider with a play/pause button (see links below).
I'm able to create a simple heatmap, for a specific year, e.g.:
df <- tibble(year = c(rep(2016, 6), rep(2017, 6)),
xVar = rep(c("a", "a", "b", "b", "c", "c"), 2),
yVar = rep(c("d", "e"), 6),
heatVar = rnorm(12))
df %>%
filter(year == 2016) %>%
hchart(type = "heatmap", hcaes(x = xVar, y = yVar, value = heatVar)) %>%
hc_legend(layout = "vertical", verticalAlign = "top", align = "right")
However, I'm struggling with making this a Motion Chart (sliding across 2016, 2017 in this example), using the hc_motion(enabled = TRUE, ...)
function.
I have read and followed these links:
https://www.r-bloggers.com/adding-motion-to-choropleths/
http://jkunst.com/highcharter/plugins.html
But no matter how I define my series, I do not get the expected result. Anyone can point me how the xVar
, yVar
series should be defined and the hc_motion
function be used to make it work?
UPDATE:
Following this answer I managed to this using shiny
, but I'd still prefer to avoid this solution:
server <- shinyServer(function(input, output) {
output$heatmap <- renderHighchart({
df <- tibble(year = c(rep(2016, 6), rep(2017, 6)),
xVar = rep(c("a", "a", "b", "b", "c", "c"), 2),
yVar = rep(c("d", "e"), 6),
heatVar = rnorm(12))
# filter data based on selected year
df.select <- dplyr::filter(df, year == input$year)
# chart
hchart(df.select, type = "heatmap", hcaes(x = xVar, y = yVar, value = heatVar))
})
})
ui <- shinyUI(fluidPage(
# Application title
titlePanel("Highcharts Heatmap Motion Chart"),
# Sidebar with a slider input for the selected year
sidebarLayout(
sidebarPanel(
sliderInput("year",
"Year:",
min = 2016,
max = 2017,
step = 1,
value = 2016,
animate = TRUE,
sep = "")
),
# Show a bubble plot for the selected year
mainPanel(
highchartOutput("heatmap")
)
)
))
shinyApp(ui = ui, server = server)