我正在绘制失业率图,但一次只能看到一个.这使得很难确定状态之间的比较方式.有没有办法在同一张图表上查看多个州的失业率?如果需要,我有一个较长和较宽版本的数据集..

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)

推荐答案

一种可能的方法是在aes()中输入pivot_longer个数据并使用color参数.

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

R相关问答推荐

R Lubridate:舍入/快照日期时间到一天中最近的任意时间?

计算R中的威布尔分布的EDF

如何删除R中除某些特定名称外的所有字符串?

通过使用str_detect对具有相似字符串的组进行分组

如何将移除事件分配给动态创建的按钮?

在GGPLATE中将突出的点放在前面

如何在R中对深度嵌套的tibbles中的非空连续行求和?

如何提取所有完美匹配的10个核苷酸在一个成对的匹配与生物字符串在R?>

R:从geom_ol()中删除轮廓并导出为pdf

DEN扩展包中的RECT树形图出现异常行为

如何移除GGPlot中超出与面相交的任何格网像元

R中Gamma回归模型均方误差的两种计算方法不一致

使用不同的定性属性定制主成分分析中点的 colored颜色 和形状

使用shiny 中的所选要素行下拉菜单

R仅当存在列时才发生变异

通过R:文件名未正确写入[已解决]将.nc文件转换和导出为.tif文件

构建一个6/49彩票模拟系统

如何准确地指出Read_delim所面临的问题?

根据部分名称匹配获取多组列的行求和

如何从R调用Amazon销售合作伙伴API?