如何将 dateRangeInput 集成到两个带有过滤功能的 layred ggplot 中?

how can one integrate dateRangeInput to two layred ggplot with a filter function?

我想在这种情况下集成 dateRangeInput?

目标是 select 一个日期范围,这样 geom_bar 双方都可以对我的输入做出反应。

这是我处理的数据: ( https://impfdashboard.de/static/data/germany_deliveries_timeseries_v2.tsv )

library(readr)
library(shiny)
library(dplyr)
library(ggplot2)
library(magrittr)

ui <- basicPage(

  # first-input
  selectInput(
    inputId = "sel22",  label = "Möglichkeitfür Histogramm (2)  auswählen",
       list("dosen_biontech_kumulativ","dosen_kumulativ","dosen_biontech_erst_kumulativ")
  ),

  # second-input
  selectInput(inputId = "sel2",  label = "Möglichkeit für Histogramm (1) auswählen",
       list("dosen_kumulativ","dosen_biontech_kumulativ","dosen_biontech_erst_kumulativ")
  ),
 
  # outpot
  plotOutput("plot"),
dateRangeInput("daterange", "Date range:",
               start  = "2020-12-29",
               end    = "2021-12-09",
               min    = "2020-12-28",
               max    = "2021-12-08",
               format = "mm/dd/yy",
               separator = " - "),
)



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

  # Summarize Data and then Plot
  data2 <- reactive({
req(input$sel2)
lf <- germany_vaccinations_timeseries_v2 %>%  group_by(date ) %>% summarise( output = get(input$sel2))
})

datao2 <- reactive({
req(input$sel22)
lf <- germany_vaccinations_timeseries_v2 %>%  group_by(date ) %>% summarise( output = get(input$sel22))
})

output$plot2 <- renderPlot({  

ggplot() +
geom_bar(data = data2(), aes(y = output, x = date), stat = "sum")+
geom_bar(data = datao2(), aes( y = output,x = date), stat = "sum")+
theme(legend.position = "none" ) +  labs(y= "Anzahl der Dosen", x = "Datum")

})
}

shinyApp(ui, server)

以下是您的操作方法。

您首先需要看到的是 df 中的 date 列属于 class date 而不是 character

germany_vaccinations_timeseries_v2$date <- as.Date(germany_vaccinations_timeseries_v2$date, 
                                                   format = "%d/%m/%Y")

我在代码中发现的下一个错误是您用于 selectInput 的标签。您需要指定它们以匹配 df.

中的列

它应该是这样的:

selectInput(
    inputId = "sel22",  label = "Möglichkeitfür Histogramm (2)  auswählen",
    list("impfstoff", "region", "dosen", "einrichtung")
    #list("dosen_biontech_kumulativ","dosen_kumulativ","dosen_biontech_erst_kumulativ")
  ),
  
  # second-input
  selectInput(inputId = "sel2",  label = "Möglichkeit für Histogramm (1) auswählen",
              list("impfstoff", "region", "dosen", "einrichtung")
              #list("dosen_kumulativ","dosen_biontech_kumulativ","dosen_biontech_erst_kumulativ")
  )

ui的全部代码如下:

ui <- basicPage(
  # first-input
  selectInput(
    inputId = "sel22",  label = "Möglichkeitfür Histogramm (2)  auswählen",
    list("impfstoff", "region", "dosen", "einrichtung")
    #list("dosen_biontech_kumulativ","dosen_kumulativ","dosen_biontech_erst_kumulativ")
  ),
  
  # second-input
  selectInput(inputId = "sel2",  label = "Möglichkeit für Histogramm (1) auswählen",
              list("impfstoff", "region", "dosen", "einrichtung")
              #list("dosen_kumulativ","dosen_biontech_kumulativ","dosen_biontech_erst_kumulativ")
  ),
  
  dateRangeInput(inputId = "date", "Date range:",
                 start  = "2020-12-29",
                 end    = "2021-02-20",
                 min    = "2020-12-28",
                 max    = "2021-12-08",
                 separator = " - "),
  
  # outpot
  plotOutput("plot")
)

为了从用户那里得到dateRangeInput,你可以像这样使用filter

df <- reactive({
    filter(germany_vaccinations_timeseries_v2, between(date, input$date[1], input$date[2]))  
  })

服务器的完整代码:

server <- function(input, output, session) {
  # Summarize Data and then Plot
    
  df <- reactive({
    filter(germany_vaccinations_timeseries_v2, between(date, input$date[1], input$date[2]))  
  })
  
  data2 <- reactive({
    req(input$sel2)
    lf <- df() %>%  group_by(date) %>% summarise(output = get(input$sel2))
  })
  
  datao2 <- reactive({
    req(input$sel22)
    lf <- df() %>%  group_by(date) %>% summarise(output = get(input$sel22))
  })
  
  output$plot <- renderPlot({  
     ggplot() +
      geom_bar(data = data2(), aes(y = output, x = date), stat = "sum")+
      #geom_bar(data = datao2(), aes( y = output,x = date), stat = "sum")+
      theme(legend.position = "none" ) +  labs(y= "Anzahl der Dosen", x = "Datum")
  })
}

我不明白为什么你的代码中有两个 geom_bar。您可以将它与另一个 plotOutput 分开绘制,并在那里使用相同的日期范围。