Home > database >  R: How to plot multiple graphs on one plot (shiny)
R: How to plot multiple graphs on one plot (shiny)

Time:04-28

I am plotting the unemployment rates, but I'm only able to view one at a time. This makes it difficult to determine how states compare to each other. Is there a way to view the unemployment rates for multiple states on the same graph? If needed, I have a version of the data set in both the longer and wider version..

library(shiny)
library(ggplot2)
load(url("https://github.com/bandcar/Unemployment-Rate-Pre-and-Post-Covid/blob/main/ue_wider.RData?raw=true"))

ui <- fluidPage(
    
    titlePanel("US Unemployment Rates Before and After COVID"),
    
    sidebarLayout(
        sidebarPanel(
            selectInput(inputId = "y", 
                        label = "State",
                        choices = c("Alabama","Alaska", "Arizona", "Arkansas", "California", "Colorado","Connecticut","Delaware","Florida","Georgia","Hawaii","Idaho","Illinois","Indiana","Iowa","Kansas","Kentucky","Louisiana","Maine","Maryland","Massachusetts","Michigan","Minnesota","Mississippi","Missouri","Montana","Nebraska","Nevada","New Hampshire","New Jersey","New Mexico","New York","North Carolina","North Dakota","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Vermont","Virginia","Washington","West Virginia","Wisconsin","Wyoming"), 
                        selected = "Alabama"),
            Multiple = TRUE,
            
            selectInput(inputId = "x", 
                        label = "X-axis:",
                        choices = c("Year"), 
                        selected = "Year"),
            
            selectInput(inputId = "col_p", 
                        label = "Select a Point Color",
                        choices = c("red", "dark green", "blue", "black"), 
                        selected = "black"),
            selectInput(inputId = "col_l", 
                        label = "Select a Line Color:",
                        choices = c("Red", "Blue", "Black", "Dark Green"), 
                        selected = "blue"),
        ),
        
        mainPanel(
            plotOutput(outputId = "graph") 
        )
    )
)

server <- function(input, output) {
    
    output$graph <- renderPlot({
        ggplot(q, aes_string(x=input$x, y=input$y))   geom_point(colour=input$col_p)   geom_line(colour=input$col_l)    ylim(2,15)
    })
}

shinyApp(ui = ui, server = server)

CodePudding user response:

One possible way to do it is to pivot_longer the data and use the color argument in aes().

library(shiny)
library(tidyverse)

load(url("https://github.com/bandcar/Unemployment-Rate-Pre-and-Post-Covid/blob/main/ue_wider.RData?raw=true"))


# pivot data to long format
q_long <- q %>%
  pivot_longer(cols = -Year, names_to = "State", values_to = "unemployment")


ui <- fluidPage(
  titlePanel("US Unemployment Rates Before and After COVID"),
  sidebarLayout(
    sidebarPanel(
      selectInput(
        inputId = "y",
        label = "State",
        choices = unique(q_long$State),
        selected = "Alabama",
        multiple = TRUE
      ),
      Multiple = TRUE,
      selectInput(
        inputId = "x",
        label = "X-axis:",
        choices = c("Year"),
        selected = "Year"
      ),
      selectInput(
        inputId = "col_p",
        label = "Select a Point Color",
        choices = c("red", "dark green", "blue", "black"),
        selected = "black"
      ),
      selectInput(
        inputId = "col_l",
        label = "Select a Line Color:",
        choices = c("Red", "Blue", "Black", "Dark Green"),
        selected = "blue"
      ),
      actionButton("run_plot", "Render Plot")
    ),
    mainPanel(
      plotOutput(outputId = "graph")
    )
  )
)

server <- function(input, output) {
  q_filtered <- eventReactive(input$run_plot, {
    filter(q_long, State %in% input$y)
  })



  output$graph <- renderPlot({
    ggplot(q_filtered(), aes(x = .data[[input$x]], y = unemployment, color = State))  
      geom_point(color = input$col_p)  
      geom_line()  
      ylim(2, 15)
  })
}

shinyApp(ui = ui, server = server)

enter image description here

  • Related