linking two plots to one drop down menu in Shiny

I am working on a shiny app and I have two plots that I would like to link to one selectInput. So the user selection will update both graphs.

Is there a way to achieve this?

my code

library(shiny)
library(plotly)
library(dplyr)
library(tidyr)

#ui
shinyUI(
  pageWithSidebar(
    headerPanel("NYC Flights Delays 2013"),
    sidebarPanel(
      selectInput("select", "Select a month", 
                  choices = list("2013","January" ,"February",
                                 "March","April",
                                 "May", "June",
                                 "July","August",
                                 "Septemeber","October",
                                 "November","December")
      )),
    
    mainPanel(
      plotlyOutput("monthlyavg",width="900",height = "400px")
    )
    ))


#server
function(input, output){
  output$monthlyavg<-renderPlotly({
    if (input$select =="January") {
      #calculate avg delay for departures and arrivals each month
        average_delay_month<-flight%>%
        group_by(flight_month)%>%
        summarise(Average_Departure_Delay=mean(dep_delay[dep_delay>0]),Average_Arrival_Delay=mean(arr_delay[arr_delay>0]))
      #filtering according to month
      average_delay_month<-subset(average_delay_month,flight_month=="January")
      
      #average delay by month(January)
      c<-average_delay_month %>%gather("metrics", "value", -flight_month)%>%
        ggplot(aes(flight_month, value, group= metrics, fill = metrics)) +
        geom_bar(stat = "identity", width=.5, position = "dodge")+
        labs(title="Average Delay by Month", x="Flight Month",y="Average Delay in Minutes")
      print(c)
      
      #--January--#
      #--All Months Flight count--#
      flight$flight_month<-factor(flight$flight_month, levels=c("January", "February", "March", "April", "May", 
                                                                "June","July","August",'September',"October","November","December"))
      #count number of flights per month
      flight_month_count<-setNames(aggregate(year~flight_month,data=flight,FUN=length),
                                   c('Month','Flight_Count'))
      flight_month_count<-subset(flight_month_count,Month=="January")
      #flight month_count_graph
      p<-ggplot(data=flight_month_count)+
        geom_bar(stat="identity",fill="#008080",aes(Month,Flight_Count),width=0.5)+
        labs(title="Total Number of Flights per Month",x="Flight Month",y="Number of Flights")
      print(p)
      }

I tried adding the two graphs (c) and (p) under one renderPlotly but it doesn't seem to do the trick.

Thanks

Here is an example of generating two plots from the selection of a single input. The flights data set is large, so the app runs slowly, at least on my system. Be patient!

Each plot gets its own slot in the output list. I think it is possible to make single plot with the two sub plots in plotly, but I am not familiar with the syntax and that is a solution that is independent of shiny.

library(shiny)
library(plotly)
library(dplyr)
library(tidyr)
library(nycflights13)

data("flights")
flights$flight_month <- factor(flights$month, 
                               labels = c("January" ,"February",
                                          "March","April",
                                          "May", "June",
                                          "July","August",
                                          "Septemeber","October",
                                          "November","December"))
#ui
ui <- shinyUI(
  pageWithSidebar(
    headerPanel("NYC Flights Delays 2013"),
    sidebarPanel(
      selectInput("select", "Select a month", 
                  choices = list("January" ,"February",
                                 "March","April",
                                 "May", "June",
                                 "July","August",
                                 "Septemeber","October",
                                 "November","December")
      )),
    
    mainPanel(
      plotlyOutput("Delays",width="900",height = "400px"),
      plotlyOutput("Dest",width="900",height = "400px")
    )
  ))


#server
server <- function(input, output){
  
  output$Delays <- renderPlotly({
    df <- filter(flights, flight_month == input$select)
    p <- ggplot(df, aes(dep_delay, arr_delay)) + geom_point()
    ggplotly(p)
  })
  
  output$Dest <- renderPlotly({
    df <- filter(flights, flight_month == input$select)
    df <- df %>% group_by(dest) %>% summarize(Avg = mean(arr_delay))
    p <- ggplot(df, aes(dest, Avg)) + geom_col() + 
      theme(axis.text.x = element_text(angle = 90))
    ggplotly(p)
  })
  
}

shinyApp(ui = ui, server = server)
1 Like

Thanks for sharing this. Your codes are exactly what I am looking for my project.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.