根据日期范围输入绘制R绘图图 [英] plot a R plotly graph based on date range input

查看:216
本文介绍了根据日期范围输入绘制R绘图图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的绘图.在这里,我将添加一个daterange输入.基于此选择,我需要重新绘制图形.

I have a plotly graph like this. Here I will be adding a daterange input. Based on this selection I need to re plot the graph.

样本数据

dates    ex   act
NOV-17   77    90
DEC-17   98    78 
JAN-18   65    87
FEB-18   77    54
MAR-18   44    34

示例代码:

   age <- plot_ly(data_, x = ~dates, y = ~ex, name = 'Expect', type = 'scatter',mode = 'lines+markers',
                                line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%
        add_trace(y =~act , name = 'Actual',mode = 'lines+markers', line = list(color = 'rgb(170, 255, 102)', width = 4)) %>%
        layout(title = "Mon vs KM",
               xaxis = list(title = "Mon"),
               yaxis = list (title = "KM"),
               legend = list(orientation = 'h'))

如果我的选择是2017-12-01 to 2018-03-01,则我的X轴应来自DEC-17 to MAR-18

IF my selection is 2017-12-01 to 2018-03-01 then my X axis should be from DEC-17 to MAR-18

我知道编码,我只需要知道如何过滤x轴

I know to code I just need to know how to filter x axis

推荐答案

下面是一个有效的示例:

Here is a working example:

PS:您应该小心使用月份名称的缩写,因为它们是特定于语言环境的.

PS: You should be careful using abbreviations of the names of months since they are locale-specific.

library(shiny)
library(plotly)

ui <- fluidPage(
  titlePanel("Plotly - dateRangeInput"),
  sidebarLayout(
    sidebarPanel(
      dateRangeInput(inputId="myDateRange", label="", start = NULL, end = NULL, min = NULL, max = NULL)
    ),
    mainPanel(
      plotlyOutput("age")
    )
  )
)


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

  data_ <- data.frame(stringsAsFactors=FALSE,
                      dates = c("NOV-17", "DEC-17", "JAN-18", "FEB-18", "MAR-18"),
                      ex = c(77L, 98L, 65L, 77L, 44L),
                      act = c(90L, 78L, 87L, 54L, 34L))

  data_$helperDates <- as.Date(paste0(data_$dates, "-01"), format="%b-%y-%d")

  data_ <- data_[order(data_$helperDates, decreasing = FALSE), ]
  data_$dates <- factor(data_$dates, levels = c(as.character(data_$dates)))

  minDate <- min(data_$helperDates, na.rm = TRUE)
  maxDate <- max(data_$helperDates, na.rm = TRUE)
  updateDateRangeInput(session, inputId="myDateRange", start = minDate, end = maxDate, min = minDate, max = maxDate)

  filteredData <- reactive({
    req(input$myDateRange)
    na.omit(data_[data_$helperDates >= input$myDateRange[1] & data_$helperDates <= input$myDateRange[2], ])
  })


  output$age <- renderPlotly({

    req({nrow(filteredData()) > 0})

    age <- plot_ly(filteredData(), x = ~dates, y = ~ex, name = 'Expect', type = 'scatter', mode = 'lines+markers',
                   line = list(color = 'rgb(205, 12, 24)', width = 4)) %>%
      add_trace(y =~act, name = 'Actual', mode = 'lines+markers', line = list(color = 'rgb(170, 255, 102)', width = 4)) %>%
      layout(title = "Mon vs KM",
             xaxis = list(title = "Mon"),
             yaxis = list (title = "KM"),
             legend = list(orientation = 'h'))
  })
}


shinyApp(ui = ui, server = server)

这篇关于根据日期范围输入绘制R绘图图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆