Reputation: 149
I have a data frame like c = (lat, lng, decile)
which decile has ten levels. obviously, by ggmap and ggplot we can plot some points in given (lat,lng) as if we set color
argument to decile column in geom_point
it will classify the points and add legend automatically. But, I used leaflet to shoe the map. using this
I have written this code:
pal <- colorNumeric(
palette = colorRampPalette(rainbow(10))(length(c$decile)),
domain = c$decile)
c %>% leaflet() %>% addTiles() %>% addCircleMarkers(color = ~ pal(deciles))
and I have points on my map in ten colors. so, my question has two parts:
-is there a simple way like ggplot to classify colors in the leaflet?
-how can I add a legend for these ten colors? in addLegend(labels , colors)
how should I fix the arguments?
UPDATE:
this is my c
> head(c)
lat lon decile
1 35.68705 51.38176 4
2 35.80742 51.48610 6
3 35.69151 51.39816 5
4 35.66665 51.35095 2
5 35.77566 51.40209 7
6 35.70326 51.41348 8
Upvotes: 0
Views: 2401
Reputation: 366
So I'm not totally sure I understand the question, but if I do, the gist is: you are currently using a continuous palette, but you want this to be 10 discrete categories, and you want a legend for this, is that right?
In that case can you not just use colorFactor() instead of colorNumeric()?
So like:
pal <- colorFactor(
palette = colorRampPalette(rainbow(10))(length(c$decile)),
domain = c$decile)
And then
leaflet(c) %>%
addTiles() %>%
addCircleMarkers(lng=c$lon, lat=c$lat, color = ~pal(decile)) %>%
addLegend("bottomright", pal = pal, values = ~decile,
title = "Deciles")
Is that what you were after?
Upvotes: 2