Creating horizontal lines on ggplot graph based on selectizeInput()

I'm trying to create a reactive plot based on two selectizeInput() menus. I want the user to click something from the first drop down menu and then the next drop down menu is filtered(with the multiple argument set = TRUE). I got this to work perfectly fine.. However, I am having difficulty making the second selectizeInput plot horizontal lines based on which inputs are shown in the filtered selectizeInput. I can only get one line at a time, which is the first one that is shown in the input menu. But lets say I have 3 items shown in the second selectizeInput, I want all the three to have horizontal lines on the plot and if the user deletes one from the menu , it will also be deleted off the plot.

Sample Data:

df_test<-structure(list(HUC14 = c("02030103140080", "02040202030070", 
                         "02040206030010", "02040202030070", "02040206030010", "02030103140080", 
                         "02040202030070", "02040202030070", "02030103140080", "02040206030010", 
                         "02040202030070", "02030103140080", "02040202030070", "02040206030010", 
                         "02040202030070", "02040202030070", "02040206030010", "02030103140080", 
                         "02040202030070", "02030103140080"), stdate = structure(c(14096, 
                                                                                   15817, 15931, 16616, 14062, 14084, 16625, 17374, 14117, 14104, 
                                                                                   16519, 14112, 16778, 15875, 16797, 15855, 17252, 14129, 15881, 
                                                                                   14140), class = "Date"), sttime = c("11:00:00", "11:15:00", "10:00:00", 
                                                                                                                       "09:00:00", "11:20:00", "10:30:00", "09:30:00", "11:30:00", "10:20:00", 
                                                                                                                       "08:50:00", "13:45:00", "11:20:00", "10:00:00", "09:30:00", "10:00:00", 
                                                                                                                       "09:30:00", "09:45:00", "13:00:00", "12:00:00", "13:10:00"), 
               val = c(20.3, 10, 22.7, 17, 29.4, 21.9, 16, 16.6, 17.7, 22.9, 
                       3, 18.4, 8.3, 20.9, 10.9, 13.5, 10.1, 21.4, 18.4, 17.4), 
               swqs = c("FW2-TP", "FW2-TP", "FW2-TP", "FW1", "FW2-NT", "FW2-NT", 
                        "FW1", "FW1", "FW2-NT", "FW2-NT", "FW1", "FW2-NT", "FW1", 
                        "FW2-NT", "FW1", "FW1", "FW2-NT", "FW2-NT", "FW1", "FW2-NT"
               )), .Names = c("HUC14", "stdate", "sttime", "val", "swqs"
               ), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
               ))

What I have tried:

impaired_hucs_df<-unique(df_test$HUC14)


ui <- navbarPage(
  tabPanel("Plots",
           sidebarLayout(
             sidebarPanel(
               selectizeInput("huc_input","Select HUC14:",
                              choices = sort(impaired_hucs_df)),
               selectizeInput("swqs","Select Temperature Standard:",
                              choices = NULL,selected = NULL,multiple = T)),
             mainPanel(plotOutput("plot1")))))
server <- function(input, output,session) {
  
  reac_df<-reactive({
    df_test%>%
      filter(HUC14 == input$huc_input)
  })
  
  observe({
    if(input$huc_input != ""){
      swqs_choices<- as.list(reac_df()$swqs[reac_df()$HUC14==input$huc_input])
      names(swqs_choices)<-reac_df()$swqs[reac_df()$HUC14==input$huc_input]
      
      updateSelectizeInput(session,"swqs",choices = swqs_choices,selected = swqs_choices)
    }
  })
  
  output$plot1<-renderPlot({
    if(input$swqs == "FW2-TP"){
        ggplot(data= reac_df(),aes(x=stdate,y=val))+
        geom_point(aes(color = "Water Temperature"),size=1.3)+
          geom_hline(aes(yintercept = 10,color="FW2-TP"),size=1.3)+
          scale_color_manual("",
                             values = c("Water Temperature"="blue","FW2-TP"="red"))
    }
    
    else if (input$swqs == "FW2-NT"){
        ggplot(data= reac_df(),aes(x=stdate,y=val))+
          geom_point(aes(color = "Water Temperature"),size=1.3)+
          geom_hline(aes(yintercept = 15,color="FW2-NT"),size=1.3)+
          scale_color_manual("",
                             values = c("Water Temperature"="blue","FW2-NT"="yellow"))
    }
    
    
    else{
        ggplot(data= reac_df(),aes(x=stdate,y=val))+
        geom_point(aes(color = "Water Temperature"),size=1.3)+
          scale_color_manual("",
                             values = c("Water Temperature"="blue"))
    }
    
  })
  
 
  }

shinyApp(ui = ui, server = server)

I would appreciate any guidance! Thanks in advance !

replace your output$plot1 with the below this works on my end


output$plot1<-renderPlot({
    p <-   ggplot(data= reac_df(),aes(x=stdate,y=val))+
        geom_point(aes(color = "Water Temperature"),size=1.3)
    if ( "FW2-TP" %in% input$swqs ) {
      p <- p +
        geom_hline(aes(yintercept = 10,color="FW2-TP"),size=1.3)
      
    }
    if ( "FW2-NT" %in% input$swqs ) {
      p <- p +
        geom_hline(aes(yintercept = 15,color="FW2-NT"),size=1.3)
      
    }
    p

  })
2 Likes

@smouksassi Thank you for your answer! This works! However, I would like to have certain colors for each line on the plot. Is there anyway you can show me how to implement scale_color_manual() in your above answer? I can't seem to get it to work.

On top of my head ( no laptop handy)

p + scale_color_manual(breaks = c("FW2-NT", "FW2-TP"), 
                       values=c("blue","green"))
2 Likes

@smouksassi worked! Thank you so much!

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