Search code examples
rggplot2dplyrshinymagrittr

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


I want to integrate the dateRangeInput in this case?

The goal is to select a date range so the both geom_bar can react to my input.

this is the data that i work on : ( 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)

Solution

  • Here's how you can do it.

    The first thing you need to see is the date column in your df is of class date and not character.

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

    The next thing I found wrong in the code is the labels you have used for selectInput. You need to specify them to match the columns from your df.

    Here's how it should look like:

    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")
      )
    

    The whole code for ui is as follows:

    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")
    )
    

    In order to get dateRangeInput from user, you can use filter like this:

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

    The whole code for the server:

    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")
      })
    }
    

    I didn't understand why you have two geom_bar in your code. You can plot it separately with another plotOutput and using the same date range there as well.