绘制使用闪亮滑块指定的日期范围内的点

时间:2016-04-21 19:02:21

标签: r shiny

从较大的数据集中,我只想绘制在包含日期范围的闪亮滑块指定的最小和最大日期内的点。这篇文章是从related post linked here构建的。数据使用dput包含在底部。

下面的代码/应用程序按顺序绘制点,因为滑块栏的日期会增加。当我移动第二个滑动条时,我希望不再删除日期范围内的点,这种情况不会发生。

如何对数据进行子集,以便只显示点(和路径)> =最小日期和< =最大日期?我不清楚如何在滑块栏上引用这两个日期。

提前致谢。

library(ggplot2)
library(shiny)
ui <- fluidPage(
  titlePanel("GPS Data Summary"),
  sliderInput(inputId = "Order",
              label = "Sequance of Observations",  
              min = as.Date(min(dat$PosiGMT)), max = as.Date(max(dat$PosiGMT)), 
              value = c(as.Date(min(dat$PosiGMT)), as.Date(min(dat$PosiGMT)))),
  plotOutput("PointPlot")
)

server <- function(input, output) {
  output$PointPlot <- renderPlot({
    p <- ggplot(dat[as.Date(dat$PosiGMT) <= input$Order ,], (aes(x = GPSUTMEasting , y = GPSUTMNorthing ))) + 
      geom_point() + geom_path() + 
      xlim( min(dat$GPSUTMEasting), max(dat$GPSUTMEasting))+
      ylim( min(dat$GPSUTMNorthing), max(dat$GPSUTMNorthing))
    print(p)
  })
}


shinyApp(ui = ui, server = server)

以下数据

dat <- structure(list(GPSUTMNorthing = 
    c(4947787L, 4947945L, 4947957L, 
     4947954L, 4947797L, 4947835L, 4947825L, 4947784L, 4947842L, 4947839L, 
     4947789L, 4947807L, 4947839L, 4947845L, 4947779L, 4947824L, 4947824L, 
     4947772L, 4947824L, 4947821L, 4947816L, 4947809L, 4947840L, 4947829L, 
     4947820L), 
     GPSUTMEasting = c(600201L, 600910L, 600911L, 600907L, 
                      601052L, 601038L, 601031L, 601066L, 600998L, 600995L, 601058L, 
                      601038L, 600987L, 601071L, 601016L, 601002L, 601003L, 601003L, 
                      600917L, 600916L, 600918L, 600923L, 600985L, 600980L, 600914L),
     PosiGMT = structure(c(1360393200, 1360414800, 1360479600, 
                  1360501200, 1360544400, 1360566000, 1360587600, 1360630800, 1360652400, 
                  1360674000, 1360695600, 1360717200, 1360738800, 1360803600, 1360825200, 
                  1360846800, 1360868400, 1360890000, 1360911600, 1360933200, 1360954800, 
                  1360976400, 1360998000, 1361019600, 1361041200), 
                  class = c("POSIXct", "POSIXt"), tzone = "") ), 
                  .Names = c("GPSUTMNorthing", "GPSUTMEasting", "PosiGMT"), 
      row.names = c(1L, 2L, 5L, 6L, 8L, 9L, 10L, 12L, 13L, 14L, 15L, 
                  16L, 17L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L), 
                  class = "data.frame")

1 个答案:

答案 0 :(得分:1)

input$Order是一个长度为2的向量,所以input$Order[1]是最小值,input$Order[2]是最大值,你可以这样做:

library(ggplot2)
library(shiny)
ui <- fluidPage(
  titlePanel("GPS Data Summary"),
  sliderInput(inputId = "Order",
              label = "Sequance of Observations",  
              min = as.Date(min(dat$PosiGMT)), max = as.Date(max(dat$PosiGMT)), 
              value = c(as.Date(min(dat$PosiGMT)), as.Date(min(dat$PosiGMT)))),
  plotOutput("PointPlot")
)

server <- function(input, output) {
  output$PointPlot <- renderPlot({
    ### Filter by date
    dat <- dat[as.Date(dat$PosiGMT) >= input$Order[1] & as.Date(dat$PosiGMT) <= input$Order[2] ,]
    ###
    p <- ggplot(dat, (aes(x = GPSUTMEasting , y = GPSUTMNorthing ))) + 
      geom_point() + geom_path() + 
      xlim( min(dat$GPSUTMEasting), max(dat$GPSUTMEasting))+
      ylim( min(dat$GPSUTMNorthing), max(dat$GPSUTMNorthing))
    print(p)
  })
}


shinyApp(ui = ui, server = server)