Filtering my plot according to user selection (Shiny)

Hello everyone,

I am working on a shiny app that contains:
1-facet_grid chart(each row of facets contains a coral type)
2-drop down menu for 5 coral types.

What I would like to do is to link the menu options to the facets. For example: if the user selects 'blue corals' my graph would only show the facets that contains this coral type.

this is my code (app.R):

library(shiny)
library(ggplot2)
mydata  <- read.csv("myfile.csv")

#ui
ui<-fluidPage(
  headerPanel("Coral Type Rates"),
  sidebarPanel(
    selectInput("CoralType", "Please Select Coral Type",
                choices=c("blue corals","soft corals",
                          "hard Coral","sea Pens","sea Fans")),
    
    
    selectInput("Smoothers","Please select smoother",
                choices=c("loose","lm")
                
    ),
    
    mainPanel(
      plotOutput("myplot")
    )
  ))


#server
server<-function(input, output){
  
  #converting types
  mydata$value <-as.numeric(sub("%", "", as.character(mydata$value)))
  mydata$value<-mydata$value/100
  mydata$location<-as.character((mydata$location))
  
  #sorting by latitude
  mydata<-mydata[order(mydata$latitude),]
  mydatalocation<-unique(mydata$location)
  mydatalocation
  mydata$location<-factor((mydata$location),levels=mydatalocation)
  
  #ggplot graph
  output$myplot <- renderPlot({
    ggplot(mydata,aes(mydata$year,mydata$value,colour=coralType))+
      geom_point(size=2)+facet_grid(mydata$coralType~mydata$location,scales="free")+
      geom_smooth(method="loess", size=1, color="black")+
      geom_smooth(method="lm", size=1, color="blue")+
      theme_bw()
     
    })
    
  }
  

shinyApp(ui=ui, server=server)

this is the facet_grid:

image

I tried using reactivevalues() and if statement, but since I'm new to R I could not make it work.

Thank you,

Hello everyone,

I am working on a shiny app that contains:
1-facet_grid chart(each row of facets contains a coral type)
2-drop down menu for 5 coral types.

What I would like to do is to link the menu options to the facets. For example: if the user selects 'blue corals' my graph would only show the facets that contains this coral type.

this is my code (app.R):

library(shiny)
library(ggplot2)
mydata  <- read.csv("myfile.csv")

#ui
ui<-fluidPage(
  headerPanel("Coral Type Rates"),
  sidebarPanel(
    selectInput("CoralType", "Please Select Coral Type",
                choices=c("blue corals","soft corals",
                          "hard Coral","sea Pens","sea Fans")),
    
    
    selectInput("Smoothers","Please select smoother",
                choices=c("loose","lm")
                
    ),
    
    mainPanel(
      plotOutput("myplot")
    )
  ))


#server
server<-function(input, output){
  
  #converting types
  mydata$value <-as.numeric(sub("%", "", as.character(mydata$value)))
  mydata$value<-mydata$value/100
  mydata$location<-as.character((mydata$location))
  
  #sorting by latitude
  mydata<-mydata[order(mydata$latitude),]
  mydatalocation<-unique(mydata$location)
  mydatalocation
  mydata$location<-factor((mydata$location),levels=mydatalocation)
  
  #ggplot graph
  output$myplot <- renderPlot({
    ggplot(mydata,aes(mydata$year,mydata$value,colour=coralType))+
      geom_point(size=2)+facet_grid(mydata$coralType~mydata$location,scales="free")+
      geom_smooth(method="loess", size=1, color="black")+
      geom_smooth(method="lm", size=1, color="blue")+
      theme_bw()
     
    })
    
  }
  

shinyApp(ui=ui, server=server)

this is the facet_grid:

image

I tried using reactivevalues() and if statement, but since I'm new to R I could not make it work.

Thank you,

Untested as I don't have your data, but this should work.

Use a temporary reactive value for the filtered data according to your dropdown. Then use the filtered data for the plot.

server<-function(input, output){
  
  #converting types
  mydata$value <-as.numeric(sub("%", "", as.character(mydata$value)))
  mydata$value<-mydata$value/100
  mydata$location<-as.character((mydata$location))
  
  #sorting by latitude
  mydata<-mydata[order(mydata$latitude),]
  mydatalocation<-unique(mydata$location)
  mydatalocation
  mydata$location<-factor((mydata$location),levels=mydatalocation)

## filter the data
  filtered_data <- reactive({
    dplyr::filter(mydata, coralType == input$coralType)
  })
  
  #ggplot graph
  output$myplot <- renderPlot({
    ggplot(filtered_data(),aes(year, value, colour=coralType))+
      geom_point(size=2)+facet_grid(coralType ~ location, scales = "free")+
      geom_smooth(method="loess", size=1, color="black")+
      geom_smooth(method="lm", size=1, color="blue")+
      theme_bw() 
  })
    
}

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.