R:如何在一个图上绘制多个图(闪亮)
R: How to plot multiple graphs on one plot (shiny)
我正在绘制失业率,但我一次只能查看一个。这使得很难确定状态之间的比较。有没有办法在同一张图表上查看多个州的失业率?如果需要,我有更长和更宽版本的数据集版本..
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)
一种可能的方法是 pivot_longer
数据并使用 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)
我正在绘制失业率,但我一次只能查看一个。这使得很难确定状态之间的比较。有没有办法在同一张图表上查看多个州的失业率?如果需要,我有更长和更宽版本的数据集版本..
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)
一种可能的方法是 pivot_longer
数据并使用 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)