如何使用R包装器为气泡图包含highcharts运动插件?

时间:2016-03-14 03:48:58

标签: javascript r highcharts rcharts

Highcharts motion plugin - Requires 3 adjustments到了一个高级别图。

  1. 包含js资产
  2. motion
  3. 的选项对象
  4. 数据位于sequence数组内。
  5. 似乎R有两个主要的Highcharts包装器。 Ramnath的rCharts和最近在CRAN上发布的highcharter

    所以我的问题是:是否有可能使用当前可用的包装器来设置泡泡高图的动画,如果是这样的话?

    rCharts Attempt 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)
    

3 个答案:

答案 0 :(得分:3)

路,

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

希望有所帮助

答案 1 :(得分:2)

您是否认为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)

答案 2 :(得分:0)

令人惊讶我找不到任何展示气泡或分散的示例作为hc_motion的图表类型。所有示例都是,bar,column,map,pie。

无论如何,经过一段时间的打击&amp;小姐,我设法让散点图在序列上做了以下工作。每个向量c(1,1)代表x&amp; y坐标。我仍然试图让它与泡沫合作而没有运气。希望有人可以使用hcaes()格式发布泡泡的代码片段。

泡泡对于动作很方便,因为你可以做Gapminder风格的图表。 http://www.gapminder.org/

from io import StringIO
csv_file = StringIO("""
1,2
2,4
3,3
4,4
5,6
6,3
7,5
8,6
1,3
2,5
3,7
4,4
5,3
6,5
7,4
8,5
1,3
2,2
3,5
4,4
5,3
6,5
7,6
8,7""")

df = pd.read_csv(csv_file, header=None, usecols=[1])

df.plot()