sconfluentus
sconfluentus

Reputation: 4993

R Leaflet Legend: colorBin- removing decimals in between breaks

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

Upvotes: 4

Views: 3015

Answers (1)

Dave2e
Dave2e

Reputation: 24069

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.

Upvotes: 6

Related Questions