簡體   English   中英

使用 3 個變量——date、state_territory 和離散變量制作 R/Shiny/ggplot2 線圖

[英]Make R/Shiny/ggplot2 linegraph using 3 variables--date, state_territory, and discrete variable

我的數據框是 tableau_jh_df [顯示的前 4 行]:

日期 省_州 number_of_cases
1 2020-01-22 阿拉巴馬州 0
2 2020-01-22 阿拉斯加 0
3 2020-01-22 亞利桑那 0

基於用戶從 Shiny 控件中選擇的 Province_state,我想使用 ggplot2 按周繪制總 number_of_cases 的折線圖。 到目前為止,無論選擇哪個省州,我得到的都是所有情況。 這是我的代碼和圖表:

# 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))  

按日期分類的案例數

您必須根據所選的州或省過濾數據集。 使用來自dplyr filter試試這個:

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))

順便說一句: col = input$state_terr如果這是您的意圖,則對線條的顏色沒有影響。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM