Search code examples
rggplot2shinyshinydashboardggplotly

How can I filter data from dropdown list?


I would like to filter state on my graph by dropdown list. For instance, I have density graph and dropdown of state list, when I change state graph should be changed as on state. However, with my code when I change the state, it has not something to happen.

My code :

SERVER

  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.

Solution

  • Following up on my comment, try this -

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