4

I am creating an interactive HTML map in R using the Leaflet library.

The legend employs the colorBin method for creating the 6 categories to break the data into.

Using the min(values) and max(values), I have defined the domain of the possible values a particular tract of Amercian Community Survey income data might fall into. However, the break points are not pleasing, as you can see from the attached image.

It looks like this

$8,8820.0- 17,708.5 
$17,708.5- 26,535.0

instead of a sane version like this:

$8,8820 - 17,708
$17,809 - 26,536

or

$8,8820.00 - 17,708.00
$17,809.00 - 26,536.00

I would accept it if there HAD to be .00 on each one, just not one decimal place for a dollar amount!

I cannot find a way to format the increments to do away with a silly single decimal...

Here is the code for the palette:

pal3<-colorBin(palette="YlOrBr", domain=c(min(plotMerge$incomePerCapita), max(plotMerge$incomePerCapita)), bins = 6, na.color = NULL, pretty=FALSE, alpha = TRUE)

And here is the function for the legend:

addLegend(pal = pal3,
        values  = plotMerge$incomePerCapita,
        position = "bottomright",
        title = "Income per Capita<br> in 2014 US Dollars ",
        labFormat = labelFormat(prefix="$"))

Just in case this is part of the problem, here is the attribute assigning color to the polygons based on income which applies the palette to the map itself.

fillColor = ~pal3(plotMerge$incomePerCapita),

as far as I can tell the tracts and related data are correct, so I am not particularly worried about the plotting of the map itself. But I want the Legend to look sane and not have one decimal place that overlaps.

I have SCOURED the R boards reading everything about leaflet in here and elsewhere. I cannot see what I need to do. Any help would be greatly appreciated.

Photograph of the legend and map created by the R copy above

MrFlick
  • 195,160
  • 17
  • 277
  • 295
sconfluentus
  • 4,693
  • 1
  • 21
  • 40
  • 1
    You may be able use: labelFormat(prefix="$", digits=0)). Here is the link to the GitHub code for leaftet: https://github.com/rstudio/leaflet/blob/master/R/legend.R. From this you should be able to tweak the format to your liking. – Dave2e Apr 29 '16 at 19:00
  • Dave2e you're my hero! THANK YOU... This worked perfectly. If you need approval, make it an answer instead of a comment and I will give you the green check mark of affirmation! – sconfluentus May 02 '16 at 12:56

1 Answers1

6

You may be able use: labelFormat(prefix="$", digits=0)). Here is the link to the GitHub code for leaftet: github.com/rstudio/leaflet/blob/master/R/legend.R.

Here is the function prototype with all of the possible options:

labelFormat = function(
      prefix = '', suffix = '', between = ' &ndash; ', digits = 3, big.mark = ',',
      transform = identity)

From this you should be able to tweak the format to your liking.

Dave2e
  • 22,192
  • 18
  • 42
  • 50
  • Now (December 2021), it should be `labFormat = labelFormat(prefix = '', suffix = '', between = ' – ', digits = 3, big.mark = ',', transform = identity)` according to the manual. – bathyscapher Dec 09 '21 at 16:28