Highlight a bar with input in a ggplot / renderPlotly


I have as output in R shiny a ggplot / geom bar.

I would like to highlight a bar when the user choose an input corresponding (meaning he choose a modality and the corresponding bar changes color)

I try to figure out for few days but I can't get.

This is UI :

checkboxGroupInput(inputId = "chauffage", 
                                              label = "Votre mode de chauffage :",
                                              choices = list("électrique"=1, "gaz naturel individuel"=2, "gaz naturel collectif"=3, 
                                                          "charbon"=4, "fioul domestique collectif"=5, "fioul domestique individuel"=6,
                                                          "GPL"=7, "réseau de chaleur"=8))),
box(id = "res2", title = "Estimer le chauffage :", 
                       status = "success", solidHeader = FALSE, collapsible = FALSE, 
                       width = NULL, 

and server :

output$graph_chauffage <- renderPlotly ({

    cols <- c("électrique"= "#e72f3f", "gaz naturel individuel"="#e72f3f", "gaz naturel collectif"= "#e72f3f", "charbon"="#e72f3f",
              "fioul domestique individuel"="#e72f3f","GPL"=  "#e72f3f", "réseau de chaleur"="#e72f3f", "fioul domestique collectif"="#e72f3f")

    cols['input$chauffage'] <- "#9184be"

    g <- ggplot(periode(), aes(x = type_chauffage, y = chauffage_en_kWh_par_logement, fill=type_chauffage)) +
      geom_bar( stat = "identity") +
      xlab("Type de chauffage" ) + ylab("Consommation énergétique en kWh/m²") +
      theme(axis.text.x = element_text(angle = 45, hjust = 1),panel.background = element_rect(fill = "transparent"), legend.position = "none")+
      geom_text(aes(label=chauffage_en_euro),vjust=1.6,size=3.5, color = "navy")

    ggplotly(g) %>%
      config(displaylogo = FALSE, collaborate = FALSE,
             displayModeBar = FALSE)

Thanks for your help !

Could you format this into a reproducible example? That is a set of code that folks can easily get up and running to replicate your issue? Currently, this is only part of a shiny app.

IF you aren't familiar with best practices for shiny reprexes, check out

This will make it easier for folks to replicate your issue and offer suggestions to solve it.

This topic was automatically closed 54 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.