Highlighting a plot according selectInput

42 views Asked by At

I'm currently working with R Shiny to create an interactive app. I deal with a problem: I have a selectInput(Nom) allowing me to choose a name in my athlete list and I've got this plot below which displays all of my athletes' values. I tried to highlight the selected athlete's values while keeping the entire plot. I'm open to all ideas 'cause I didn't find any solutions that convinced me. I have attached a screen on my current plot. I would be grateful if someone had an idea to highlight my athlete's values selected by my selectInput, while keeping my entire plot. Thank you !

Data_GPS_G3_filtre <- reactive({
    Donnees_globales |>
      filter(Nom == input$Nom)
  })

[...]

    output$highlightedPlot <- renderPlot({
      
      colors <- ifelse(Data_GPS_G3_filtre$Nom == input$Nom, "yellow", scales::col_numeric(c("red", "lightgreen"), domain = NULL)(Data_GPS_G3_filtre$RM_Relatif))
      
      plot_1 <- ggplot(data = Data_GPS_G3_filtre()) +
        aes(x = RM_Relatif, y = Nom, fill = RM_Relatif) +
        geom_col(color = "black", position = "stack") +
        geom_text(aes(label = ifelse(RM_Relatif == 0, "", RM_Relatif)), color = "black", position = position_stack(vjust = 0.5)) +
        facet_grid(cols = vars(Mouvements_performances)) +
        theme(plot.background = element_rect(fill = "white"),
              panel.background = element_rect(fill = "white"),
              panel.grid = element_blank(),
              panel.border = element_blank()) +
        scale_fill_manual(values = colors)
      
      colours <- ifelse(Data_GPS_G3_filtre$Nom == input$Nom, "yellow", scales::col_numeric(c("red", "lightgreen"), domain = NULL)(Data_GPS_G3_filtre$Valeur_Absolu))
      
      plot_2 <- ggplot(data = Data_GPS_G3_filtre()) +
        aes(x = Valeur_Absolu, y = Nom, fill = Valeur_Absolu) +
        geom_col(color = "black", position = "stack") +
        geom_text(aes(label = ifelse(Valeur_Absolu == 0, "", Valeur_Absolu)), color = "black", position = position_stack(vjust = 0.5)) +
        facet_grid(cols = vars(Absolu)) +
        theme(plot.background = element_rect(fill = "white"),
              panel.background = element_rect(fill = "white"),
              panel.grid = element_blank(),
              panel.border = element_blank()) +
        scale_fill_manual(values = colours) 
      
      fig_group <- ggpubr::ggarrange(plot_1, plot_2)
      return(fig_group)
    })

enter image description here

1

There are 1 answers

2
Stéphane Laurent On

I would do a reactive conductor for the plots without the colors:

Plot1 <- reactive({
  ggplot(data = Data_GPS_G3_filtre()) +
    aes(x = RM_Relatif, y = Nom, fill = RM_Relatif) +
    geom_col(color = "black", position = "stack") +
    geom_text(aes(label = ifelse(RM_Relatif == 0, "", RM_Relatif)), color = "black", position = position_stack(vjust = 0.5)) +
    facet_grid(cols = vars(Mouvements_performances)) +
    theme(plot.background = element_rect(fill = "white"),
          panel.background = element_rect(fill = "white"),
          panel.grid = element_blank(),
          panel.border = element_blank())
})

output$highlightedPlot <- renderPlot({
  colors <- ifelse(Data_GPS_G3_filtre$Nom == input$Nom, "yellow", scales::col_numeric(c("red", "lightgreen"), domain = NULL)(Data_GPS_G3_filtre$RM_Relatif))
  Plot1() + scale_fill_manual(values = colors)
})

In this way the plot is not entirely reconstructed each time input$Nom changes.