【问题标题】:How to include highcharts motion plugin for bubble plot using R wrapper?如何使用 R 包装器包含用于气泡图的 highcharts 运动插件?
【发布时间】:2016-06-29 00:01:06
【问题描述】:

Highcharts motion plugin - Requires 3 adjustments 到高位图。

  1. 包含 js 资产
  2. motion 的选项对象
  3. 数据位于sequence 数组中。

Highcharts 似乎有两个主要的 R 包装器。 Ramnath 的 rCharts 和最近在 CRAN 上发布的 highcharter

所以我的问题是:是否可以使用当前可用的包装器随着时间的推移为气泡图制作动画,如果可以,如何制作?

rCharts 尝试 1 从气泡图开始并介绍 3 个必需的运动选项:

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 - 尝试 2 将数据重组为序列,然后输入图表。

# 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)

【问题讨论】:

  • 嗨@luke-singham,我会看看如何在highcharter中实现这个功能,如果成功我会告诉你的。
  • @jbkunst 太棒了,谢谢!这可能会有所帮助:motion.js 使用处理数字、数组和对象的update.point。建议运动气泡图所需的多元数据是可能的。

标签: javascript r highcharts rcharts


【解决方案1】:

令人惊讶的是,我找不到任何已发布的示例,说明气泡或散点图是带有 hc_motion 的图表类型。所有示例都是条形图、柱形图、地图图、饼图。

无论如何,在经历了相当多的命中与失误之后,我设法让散点图在序列上执行以下操作。每个向量 c(1,1) 代表 x 和 y 坐标。我仍然试图让它与泡沫一起工作,但没有运气。希望有人可以使用 hcaes() 格式为气泡发布代码 sn-p。

Bubble 对运动很方便,因为您可以制作 Gapminder 样式图表。 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))
【解决方案2】:

卢克,

motion.js 插件已添加到highcharter。处于开发版本(通过devtools 下载),需要更多测试,但这是一个开始。

请查看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))

如果您发现任何可疑行为(也称为错误),请在此处报告:https://github.com/jbkunst/highcharter/issues

希望对你有帮助

【讨论】:

    【解决方案3】:

    您是否将shiny 视为临时解决方案?

    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)
    

    【讨论】:

    • 欢迎来到 SO!感谢您的回答。大约一年半前,我以这种方式使用shiny,并将ggplot 作为可视化。使用slidertInput(... animate = TRUE) 的缺点是气泡不是真的在运动 - 它们只是重新出现。如果使用的数据具有大量周期(如视频中的帧)并且每个实体的周期之间的变化量很小,这可能就足够了 - 两者都不适用于所使用的数据。
    • 在@jbkunst 可以将motion.js 插件添加到很棒的highcharter 库之前,这可能是一个临时解决方案。我经常只用几帧就使用这种方法,而且对于未经训练的人来说,大多数人都不知道其中的区别。
    猜你喜欢
    • 2014-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-05
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多