当selectizeInput = TRUE时,在R闪亮图中显示重复的值

时间:2019-10-29 16:32:30

标签: r ggplot2 shiny shiny-reactivity

我很难弄清楚如何在闪亮的应用程序中显示ggplot中的所有值。数据框中存在一些实例,其中某些站点在同一日期具有相同的值。当我尝试在应用程序中选择所有它们并在绘图中显示它们时,它们没有显示。我确定我缺少一些简单的东西,但似乎找不到任何解决方案。我将不胜感激任何帮助。谢谢。

样本数据:

df<-structure(list(`AQS Code` = c(340071001, 340170006, 340010006, 
340070002, 340273001, 340150002, 340290006, 340410007, 340190001, 
340030006, 340110007, 340250005, 340130003, 340315001, 340210005, 
340230011, 340219991, 340071001, 340170006, 340010006), Latitude = c("39.684249999999999", 
"40.670250000000003", "39.464872", "39.934446000000001", "40.787627999999998", 
"39.800339000000001", "40.064830000000001", "40.924579999999999", 
"40.515262", "40.870435999999998", "39.422272999999997", "40.277647000000002", 
"40.720989000000003", "41.058616999999998", "40.283092000000003", 
"40.462181999999999", "40.3125", "39.684249999999999", "40.670250000000003", 
"39.464872"), State = c("NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
"NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", "NJ", 
"NJ", "NJ"), `Site Name` = c("Ancora State Hospital", "Bayonne", 
"Brigantine", "Camden Spruce St", "Chester", "Clarksboro", "Colliers Mills", 
"Columbia", "Flemington", "Leonia", "Millville", "Monmouth University", 
"Newark Firehouse", "Ramapo", "Rider University", "Rutgers University", 
"Washington Crossing", "Ancora State Hospital", "Bayonne", "Brigantine"
), `Elev (m)` = c("33.0", "3.0", "5.0", "4.0", "278.0", "12.0", 
"41.0", "146.0", "45.0", "1.0", "22.0", "8.0", "27.0", "307.0", 
"30.0", "19.0", "61.0", "33.0", "3.0", "5.0"), County = c("Camden, NJ", 
"Hudson, NJ", "Atlantic, NJ", "Camden, NJ", "Morris, NJ", "Gloucester, NJ", 
"Ocean, NJ", "Warren, NJ", "Hunterdon, NJ", "Bergen, NJ", "Cumberland, NJ", 
"Monmouth, NJ", "Essex, NJ", "Passaic, NJ", "Mercer, NJ", "Middlesex, NJ", 
"Mercer, NJ", "Camden, NJ", "Hudson, NJ", "Atlantic, NJ"), Date = structure(c(17956, 
17956, 17956, 17956, 17956, 17956, 17956, 17956, 17956, 17956, 
17956, 17956, 17956, 17956, 17956, 17956, 17956, 17957, 17957, 
17957), class = "Date"), AQI_value = c(36, 30, 36, 32, 34, 30, 
40, 26, 27, 31, 38, 44, 28, 35, 36, 35, 35, 31, 34, 34)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -20L))

示例应用程序:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(selectizeInput("site_name_select",
                                "Choose Site:",choices = df$`Site Name`,
                                multiple = TRUE,options = list(
                                  placeholder = "Please select a site below"
                                )),
  dateRangeInput("dates","Choose Date Range:",
                 start = "2019-03-01",
                 end = "2019-03-02"),
  plotOutput("plot1")))


server <- function(input, output) { 

  df_reac<-reactive({
    req(input$site_name_select)
    req(input$dates)
    df%>%
      filter(`Site Name` ==input$site_name_select,
             between(Date ,input$dates[1], input$dates[2]))
  })

  output$plot1<-renderPlot({
    ggplot(data = df_reac(),aes(x=Date,y=AQI_value,
                                  color = df_reac()$`Site Name`))+
      geom_point(size = 3.5)

  })
}

shinyApp(ui, server)

0 个答案:

没有答案