【问题标题】:How to filter data using a time series slider如何使用时间序列滑块过滤数据
【发布时间】:2019-05-12 22:29:12
【问题描述】:

我正在尝试创建一个显示两个图的仪表板:

条形图时间序列图。

时间序列图有一个滑块,我希望条形图只显示滑块范围内的值。

我可以使用闪亮的滑块小部件,但我更愿意使用附加到时间序列图的小部件。

【问题讨论】:

  • 欢迎来到 SO!在提出下一个问题之前,请阅读this

标签: r ggplot2 shiny plotly


【解决方案1】:

这就是我认为你所追求的:

library(shiny)
library(plotly)
library(data.table)

nPoints <- 100
DT <- data.table(x = Sys.time()-seq_len(nPoints), y = runif(nPoints, 0, 10))

ui <- fluidPage(
    plotlyOutput("scatterPlot"),
    plotlyOutput("barPlot")
)

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

    output$scatterPlot <- renderPlotly({
        p_scatter <- plot_ly(
            DT,
            x = ~ x,
            y = ~ y,
            type = "scatter",
            mode = "lines"
        ) %>%
            layout(
                xaxis = list(
                    rangeslider = list(type = "date")
                    ))
    })

    xRangeRaw <- reactiveVal()
    xRange <- xRangeRaw %>% debounce(100)

    observeEvent(event_data("plotly_relayout"), {
        xRangeRaw(event_data("plotly_relayout")$xaxis.range)
        })

    output$barPlot <- renderPlotly({
        if(is.null(xRange())){
            filteredDT <- DT
        } else {
            filteredDT <- DT[x %between% as.POSIXct(xRange(), format = "%Y-%m-%d %H:%M:%OS")]
        }

        p_bar <- plot_ly(
            na.omit(filteredDT),
            x = ~ x,
            y = ~ y,
            type = "bar"
        )

    })
}

shinyApp(ui = ui, server = server)

【讨论】:

    猜你喜欢
    • 2016-02-08
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多