my data frame is tableau_jh_df [first 4 lines shown]:
date province_state number_of_cases
1 2020-01-22 Alabama 0
2 2020-01-22 Alaska 0
3 2020-01-22 Arizona 0
Based on user selection of province_state from Shiny control, I want to plot a line graph of total number_of_cases by week using ggplot2. What I am getting so far is all of the cases regardless of which province_state is selected. Here is my code and the graph:
# ui
fluidRow(
column(3,
selectInput("state_terr", "Choose a State/Territory:",
list("Alabama", "Alaska", "Arizona" [,...etc]
# server
output$plot <- renderPlot({
min_date <- as.Date("2020-01-20")
max_date <- NA
ggplot(tableau_jh_df, aes(date, number_of_cases, col = input$state_terr,
group = 1)) +
geom_line() +
labs(x = "Date", y = "Number of Cases") +
theme(axis.text.x = element_text(angle = 45),
axis.text.y = element_text(angle = 90))
You have to filter your dataset according to the chosen state or province. Using filter
from dplyr
try this:
ggplot(filter(tableau_jh_df, province_state == input$state_terr),
aes(date, number_of_cases, col = input$state_terr, group = 1)) +
geom_line() +
labs(x = "Date", y = "Number of Cases") +
theme(axis.text.x = element_text(angle = 45),
axis.text.y = element_text(angle = 90))
BTW: col = input$state_terr
has no effect on the color of the line if that was your intention.