如何从下拉列表中过滤数据?

How can I filter data from dropdown list?

我想通过下拉列表过滤图表上的状态。例如,我有密度图和状态列表下拉列表,当我更改状态图时,应将其更改为状态。但是,当我更改状态时使用我的代码,它没有发生任何事情。

我的代码:

服务器

  output$overat <- renderPlot({
   filtered <-
   Medi_sum_small %>%
   filter(State == input$e1)
  ggplot(Medi_sum_small, aes(Hospital_Ownership)) +
    geom_density(aes(fill=factor(Hospital_overall_rating)), alpha=0.7) + 
    labs(x="Ownership",
         fill="Overall rating") +
    scale_x_discrete(labels = function(x) str_wrap(x,width=0.3))
  
  })

UI

box(
              title = "Select State"
              ,width = 3
              ,solidHeader = TRUE
              ,status = "primary"
              ,selectInput(
                'e1', 'State',
                c("All",unique(Medi_sum_small$State))
              )

Graphs should be changed when I change the stage.

根据我的评论,试试这个 -

ggplot(filtered, aes(Hospital_Ownership)) +
    geom_density(aes(fill=factor(Hospital_overall_rating)), alpha=0.7) + 
    labs(x="Ownership",
         fill="Overall rating") +
    scale_x_discrete(labels = function(x) str_wrap(x,width=0.3))

现在我可以做到了,我将代码更改为:

 output$overat <- renderPlot({
    ggplot(filtered <-
             Medi_sum_small %>%
             filter(State == input$e1), 
           aes(Hospital_Ownership)) +
      geom_density(aes(fill=factor(Hospital_overall_rating)), alpha=0.7) + 
      labs(x="Ownership",
           fill="Overall rating") +
      scale_x_discrete(labels = function(x) str_wrap(x,width=0.3))

  })

非常感谢