Leaflet: Show values instead of percentile ranges in legends that use colorQuantile palette

Created on 5 Jun 2015  路  2Comments  路  Source: rstudio/leaflet

When using a colorQuantile color palette, is it be possible to have the legend show the numeric values corresponding to the percentile ranges instead of the percentile ranges themselves?

Most helpful comment

You can do it by providing your own labFormat function:

leaflet() %>% addLegend(pal=colorQuantile("Blues", 1:10), values=1:10, labFormat = function(type, cuts, p) {
  n = length(cuts)
  p = paste0(round(p * 100), '%')
  cuts = paste0(formatC(cuts[-n]), " - ", formatC(cuts[-1]))
  # mouse over the legend labels to see the percentile ranges
  paste0(
    '<span title="', p[-n], " - ", p[-1], '">', cuts,
    '</span>'
  )
})

All 2 comments

You can do it by providing your own labFormat function:

leaflet() %>% addLegend(pal=colorQuantile("Blues", 1:10), values=1:10, labFormat = function(type, cuts, p) {
  n = length(cuts)
  p = paste0(round(p * 100), '%')
  cuts = paste0(formatC(cuts[-n]), " - ", formatC(cuts[-1]))
  # mouse over the legend labels to see the percentile ranges
  paste0(
    '<span title="', p[-n], " - ", p[-1], '">', cuts,
    '</span>'
  )
})

Thanks so much for the quick response!

Was this page helpful?
0 / 5 - 0 ratings