rstudio / leaflet

R Interface to Leaflet Maps
http://rstudio.github.io/leaflet/
Other
805 stars 508 forks source link

colorQuantile legend problem #211

Open neuwirthe opened 8 years ago

neuwirthe commented 8 years ago

When using colorQuantile to choses colors for fillColor, and combining this with addLegend, the legend will display the percentages corresponding to the quantiles, and not the quantiles and NOT the values of the quantiles. Even using the labels parameter of addLegend with handcrafted values will not produce a useful legend.

jcheng5 commented 8 years ago

I believe the values should be shown as a tooltip if you hover over the percentage label. If you would like it to show the values directly you can either 1) stop passing pal and values, and instead pass color and labels; or 2) keep passing pal and values, and also pass a custom labFormat. You can modify the default labelFormat function for this purpose, see the source code here: https://github.com/rstudio/leaflet/blob/master/R/legend.R

neuwirthe commented 8 years ago

I dug into it and the labels produced as standard are "<span title=\"0.000 – 0.002\">0% – 14%"
"<span title=\"0.002 – 0.003\">14% – 29%" ....

I have not found a way of displaying the content of the title with hovering lr clockin on my Mac in Safarp or in the RStudio Viewer.

It also seems that the transform argument to labelFormat does not do anything when applied in the case of colorQuantiles. It works nicely with other coloring schemes, though.

neuwirthe commented 8 years ago

Further investigation shows that in the case of quantile the transform are applied to the values produces for the title part of the labels, and these are not displayed in my case. I really think that the legend labels should correspond to the values of the variable and not to the percentages of the quantiles. Could that me at least made an option?

neuwirthe commented 8 years ago

An alternative would be adding a "cuts" argument colorBins allowing to explicitly give the bin borders instead of the number of bins. That way, one could simply use quantiles as the bin borders.

jcheng5 commented 8 years ago

If I'm understanding you correctly, the colorBins argument can actually take the bin borders as the bins argument--not just the number of bins.

Furthermore even with your existing quantile palette you can customize labelFormat like this:

... %>%
  addLegend(pal = pal, values = values, labelFormat = function(type, cuts, p) {
    n = length(cuts)
    paste0(cuts[-n], " &ndash; ", cuts[-1])
  })
rudeboybert commented 8 years ago

+1 on this being an option, as this is a common enough task. Also, sorry to be a pedant, but the above code should read:

... %>%
  addLegend(pal = pal, values = values, labFormat = function(type, cuts, p) {
    n = length(cuts)
    paste0(cuts[-n], " &ndash; ", cuts[-1])
  })
jcheng5 commented 7 years ago

I concede that I was dead wrong on the question of whether the quantile's legend should display percentiles vs. values by default. @yihui tried to tell me but I didn't listen.

I have two proposals for how we can fix it at this point:

  1. (More conservative, backwards-compatible) Keep the current default but include the above labFormat as a function in leaflet, so you can say addLegend(..., labFormat = quantileValueFormat).
  2. (More aggressive, breaks back-compat) The opposite: change the default to values, and include a quantileProbFormat (or whatever) function.

@bhaskarvk, or anyone else, please let me know if you have a preference.

ragapack commented 7 years ago

I think we need an options as.integer too. labFormat = function(type, cuts, p) { + n = length(cuts) + paste0(as.integer(cuts)[-n], " to ", as.integer(cuts)[-1])}

1beb commented 5 years ago

@jcheng5 Don't break backwards compat, that's just a bad idea in general. Add an option to the labelFormat "as.is = TRUE" or something simple of that nature. I think that's a much better idea. The functions here will also need to consider rounding. I used a version of @rudeboybert (awesome name by the way) function but added rounding around the cuts.

MonicaFlores commented 5 years ago

For percent scales, replace @ragapack 's as.integer for a scales::percent option:

labFormat = function(type, cuts, p) {
                                   n = length(cuts)
                                   paste0(scales::percent(cuts)[-n], " &ndash; ", scales::percent(cuts)[-1])
                                 }
erstearns commented 4 years ago

Has the suggestion of addLegend(..., labFormat = quantileValueFormat) been enacted by chance? Running into this issue now myself. Thank you!