使用Highcharts库存类型功能处理非库存数据(R)

时间:2018-10-15 17:53:01

标签: r r-highcharter

我正在处理一个不是股票市场数据的时间序列。我想在R中使用highcharter进行交互式可视化。我暂时制作了这样的图表:

虚拟数据

library(tidyverse)
library(highcharter)   


data(economics_long, package = "ggplot2")

economics_long2 <- filter(economics_long,
                          variable %in% c("pop", "uempmed", "unemploy"))

hchart(economics_long2, "line", hcaes(x = "date", y = "value01", group = "variable"))

我想知道,有什么方法可以在此图表的顶部添加日期过滤器,例如在highcharter中的type ='stock'图表中的日期过滤器。与此图片相似: enter image description here

1 个答案:

答案 0 :(得分:2)

我认为在基本解决方案中,您可以创建自己的小部件/小工具。 这是它的开始-功能齐全-您可以根据自己的目的更好地设置样式。

library(shiny)
library(miniUI)
library(highcharter)
library(tidyverse)


hightchart_filter <- function(data) {

  ui <- miniPage(
    miniContentPanel(

        # Dates ####
        dateInput("date_start", "start_date", value = "1900-01-01",   width = "25%"),
        dateInput("date_end", "end_date",  value = "2100-01-01", width = "25%"),

        # Highchart ####
        highchartOutput("high_plot", height = "500px")
      )
    )


  server <- function(input, output, session) {

    # update for data boxes
    updateDateInput(session, "date_start", value = data$date %>% min())
    updateDateInput(session, "date_end", value = data$date %>% max())

    # filter data
    data_filtered <- reactive({
      data %>% filter(between(date, input$date_start, input$date_end))
    })

    # Highchart ####
    output$high_plot <- renderHighchart({
      hchart(data_filtered(), "line", hcaes(x = "date", y = "value01", group = "variable"))
    })

  }

  runGadget(ui, server)
}

并运行它:

hightchart_filter(economics_long)