3

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)

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

3 Comments

Would you happen to know why the states with two names (New York, New Mexico, South Carolina) will not plot?
Yes, because there is a dot separating the words and not a space. A fix is to call unique(q_long$State) in the choices argument from selectInput. I edited the code to reflect that.
Alternatively you can clean the values by running q_long <- q_long %>% mutate(State = str_replace(State, '\\.', ' '))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.