我正在尝试使用highcharter
R 包“运动插件”来制作热图的运动图表。即我希望热图随时间变化,使用带有播放/暂停按钮的滑块(参见下面的链接)。
我可以为特定年份创建一个简单的热图,例如:
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")
hc_motion(enabled = TRUE, ...)
但是,我正在努力使用该函数将其制作为动态图表(在此示例中滑过 2016 年、2017 年) 。
我已阅读并关注以下链接:
https://www.r-bloggers.com/adding-motion-to-choropleths/
http://jkunst.com/highcharter/plugins.html
但无论我如何定义我的系列,我都没有得到预期的结果。任何人都可以指出我应该如何定义xVar
,yVar
系列以及如何hc_motion
使用该功能使其工作?
更新:
按照这个答案,我设法做到了这一点shiny
,但我仍然希望避免这种解决方案:
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)