5

I have a R script for making a shiny app with leaflet map. The map includes a legend based on quartile calculation. The legend shows the range of each quartile, but I would like to make to show like "1st Quartile", "2nd Quartile", and so on. I tried to add "labels" under "AddLegend" but of not use. DO you know how? You can see the script and relevant files from the GitHub link below. Thank you so much.

https://github.com/e5t2o/exploring_shiny/blob/master/InteractiveMap/app.R

Soohyung Kim
  • 55
  • 1
  • 2

1 Answers1

6

I was looking for a solution to this and found one in this comment: Manually adding legend values in leaflet

It is a bit of a workaround, but for some reason it works. You can write:

# Define palette
pal <- colorBin(UNICEF, domain = oo$Value, bins = bins, na.color = "#F1F1F1")
# Define labels
labels <- c("1st Quartile", "2nd Quartile", "3rd Quartile", "4th Quartile")

output$mymap <- renderLeaflet({
            leaflet(data = oo) %>% 

                    addPolygons( # Fill in your parameters
                                ) %>%

                    addLegend( # Legend options
                             pal = pal, # Previously defined palette
                             values = ~Value, # Values from data frame 
                             opacity = 0.7, # Opacity of legend
                             title = NULL, # Title
                             position = "bottomleft",
                             labFormat = function(type, cuts, p) {  # Here's the trick
                                                  paste0(labels)
                             }
                             ) %>%

                    setView( # Fill in your map boundaries
                            )
improcessing
  • 61
  • 1
  • 3