Open yyz1989 opened 9 years ago
If a value of 2 is passed in, which bin does it belong to? It looks like the result of .bincode
is to simply throw away one of the bins, I'm not sure that's a better result than an error...?
I could maybe imagine performing your loop, with a warning message...
Hi @jcheng5
Thank you very much for your reply. Yeah, using .bincode will keep the problem silent, but I am also not sure about the consequence :smile:. If there is no other way to get around this issue, I also agree to throw out a warning message and try less breaks because this issue is really difficult to detect. It works for 99% of the time but only fails in really exceptional case. I guess at least it should be better to avoid throwing out an error and terminating the program when the problem is not critical. How do you think about it?
Thank you very much and have a nice day.
Regards, Yang
I'd like this feature as well. I think if breaks are not unique, I'd be happy to re-allocate on the fly rather than stop the program or try to program around every edge case. I mostly just want to put colors on my map.
I'm also running into this issue, and while changing the number of breaks is a workaround, I just wanted to add another voice of support for this feature.
Was this ever figured out?
Bringing attention to this topic again! Seems to still be an issue.
Likewise adding my voice to this.
In the meantime I'm using colorNumeric()
from the cases where this is an issue.
yeah, it's been a problem for me too.
Still a problem for me as well.
I would also like for this issue to throw out a warning instead of an error as well!
This workaround uses getJenksBreaks
from BAMMtools.
This does not fix the issue with quantile, but Jenks Breaks are a commonly used bin method in GIS programs. Therefore, I think is a reasonable alternative in the meantime. That is, it puts the number of colors that I want on the map.
factor_domain <- cut(
tb$numericvar,
breaks = getJenksBreaks(tb$numericvar, n_breaks),
include.lowest = T
)
pal <- colorFactor(
palette = "YlOrRd",
domain = factor_domain
)
m <- leaflet(tb) %>%
addProviderTiles("CartoDB") %>%
addPolygons(
fillColor = ~pal(factor_domain),
weight = 2,
opacity = 1,
color = "white",
fillOpacity = 0.1
)
m
Edit:
This is actually easier to do with colorBin.
pal <- colorBin(
palette = "YlOrRd",
domain = tb$numericvar,
bins = getJenksBreaks(tb$numericvar, 5)
)
m <- leaflet(tb) %>%
addProviderTiles("CartoDB") %>%
addPolygons(fillColor = ~pal(tb$numericvar),
weight = 2,
opacity = 1,
color = "white",
fillOpacity = 0.1
)
I also determined that this issue is likely due to the difference between quantile classification, which is popular in GIS software, and the quantile
function in R which divides the range into intervals of equal probabilities.
Maybe others did not misinterpret this function, but it is how I found this thread. It might be helpful to add named bin methods to the bin function: quantile_classes (existing behavior for bins = n), jenks, etc.
Hi there,
First of all thank you very much for the efforts to develop leaflet for R!
Recently I encountered an issue when trying to decide quantiles for data when coloring the map. The data is not well distributed so it cannot even select 9 quantiles in 306 values. Here is the code.
Then I looked into the source code of colorQuantile function and tried to debug it step by step.
So this is the reason! There are 2 same quantiles causing the problem in cut. If I decrease the number of quantile to 8, then there won't be any problem. However, based on different filter criteria, query results cannot be predicted so it is difficult to predefine the number of quantiles. So what I am doing is as follows:
It looks quite ugly and may lead to performance issues, but it works. After some investigation, I also discovered that it seems we can also use .bincode to categorize data:
So maybe we can replace cut with .bincode? However I don't know if there is any other side-effect. If we should not, what can I do to get around this issue?
Thank you very much and have a nice day.
Regards, Yang