Search code examples
rggplot2shinyshinyapps

multiple histograms in Shinyapp


I want to select multiple variables to see their histogram. The code below only prints one variable's histogram. many thanks in advance.

library(shiny)
library(ggplot2)
library(gridExtra)

ui <- fluidPage(
  titlePanel("title panel"),
  sidebarLayout(position = "left",
                sidebarPanel("sidebar panel",
                             checkboxGroupInput(inputId = "selected_var",
                                                label = "Select variables:",
                                                choices = names(mtcars)) 
                ),
                mainPanel("main panel",
                          column(6, plotOutput(outputId = "plotgraph", 
                                               width = "500px", height = "400px"))
                )
  )
)

server <- function(input, output){
  output$plotgraph <- renderPlot({
    ggplot(data = mtcars, aes_string(x = input$selected_var)) +
      geom_histogram(aes(y = ..density..), bins = 100, col = "darkgreen", 
                     fill = "darkgreen")+
      geom_density(col = "red", alpha = .2, fill = "#FF6666")
    
  })  
}

shinyApp(ui = ui, server = server)

Solution

  • Here's something that I believe gives you close to what you want.

    library(shiny)
    library(ggplot2)
    library(tidyverse)
    
    ui <- fluidPage(
      titlePanel("title panel"),
      sidebarLayout(position = "left",
                    sidebarPanel("sidebar panel",
                                 checkboxGroupInput(inputId = "selected_var",
                                                    label = "Select variables:",
                                                    choices = names(mtcars)) 
                    ),
                    mainPanel("main panel",
                              column(6,plotOutput(outputId="plotgraph", width="500px",height="400px"))
                    )))
    
    
    
    server <- function(input, output){
      # Tidy the data
      tidyCars <- as_tibble(mtcars %>% 
                    rownames_to_column("Model")) %>% 
                    pivot_longer(
                      -Model,
                      names_to="Variable",
                      values_to="Value"
                    )
    
      output$plotgraph <- renderPlot({
        # Suppress warning message when no variables are selected
        req(input$selected_var)
        
        # Modify print request to handle tidy format
        tidyCars %>% 
          # Filter to selected variables
          filter(Variable %in% input$selected_var) %>% 
          # Define the plot
          ggplot(aes(x=Value)) +
          geom_histogram(aes(y = ..density..),bins = 100,col="darkgreen",fill="darkgreen")+
          geom_density(col = "red",alpha=.2, fill="#FF6666") +
          # One plot for each variable
          facet_wrap(vars(Variable))
      })  
    }
    
    shinyApp(ui = ui, server = server)
    

    enter image description here