Soohyung Kim
Soohyung Kim

Reputation: 55

How to customize legend labels?

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.

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

Upvotes: 5

Views: 6159

Answers (1)

improcessing
improcessing

Reputation: 61

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
                            )

Upvotes: 6

Related Questions