Home > database >  multiple histograms in Shinyapp
multiple histograms in Shinyapp

Time:10-24

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)

CodePudding user response:

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

CodePudding user response:

One can use ggarrangefrom package ggpubr and then create a list of plots with lapply:

library(shiny)
library(ggplot2)
library(ggpubr)

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({
    if (is.null(input$selected_var))
      plist <- list(ggplot()   theme_void())
    else
      plist <- lapply(
        input$selected_var,
        function(x) ggplot(data = mtcars, aes_string(x = x))  
          geom_histogram(aes(y = ..density..), bins = 100,
                         col = "darkgreen", fill="darkgreen")  
          geom_density(col = "red", alpha = .2, fill = "#FF6666")
      )
    ggarrange(plotlist = plist)
  })
}

shinyApp(ui = ui, server = server)

multiple plots with ggpubr

  • Related