Petr
Petr

Reputation: 1817

Cluster color in leaflet Heatmap

Is there a way how to have cluster colors in leaflets addHeatmap function, let say we have some values of variables and cluster them to 8 categories (see example), is there a way how to have also 8 color categories in the heatMap? I know this can be done in ggplot - geom_geom_tile

Is there a way ho to reproduce it in leaflet as well?

Example:

library(ggmap)
library(maptools)
library(ggplot2)


d = data.frame(
  pred_res = runif(2000, -2, 2),
  lat = runif(2000, 49.94, 50.18),
  lon = runif(2000, 14.22, 14.71)

)
#top&bottom coding and discreting pred_res....8
d$res_coded<-replace(d$pred_res,d$pred_res<(-1),8)
d$res_coded<-replace(d$res_coded,d$pred_res>=-1,7)
d$res_coded<-replace(d$res_coded,d$pred_res>=-0.4,6)
d$res_coded<-replace(d$res_coded,d$pred_res>=-0.1,5)
d$res_coded<-replace(d$res_coded,d$pred_res>=0,4)
d$res_coded<-replace(d$res_coded,d$pred_res>=0.1,3)
d$res_coded<-replace(d$res_coded,d$pred_res>=0.4,2)
d$res_coded<-replace(d$res_coded,d$pred_res>=1,1)

d %>% head
d$res_coded %>% table

library(leaflet)
library(leaflet.extras)

leaflet() %>% addProviderTiles("CartoDB.Positron") %>% 
  addHeatmap(lng = d$lon, lat = d$lat, intensity = d$res_coded)

Upvotes: 0

Views: 781

Answers (1)

Jeff
Jeff

Reputation: 209

Please see the gradient function from the documentation here.

Here is an example with a different color palette:

leaflet() %>% addProviderTiles("CartoDB.Positron") %>% 
  addHeatmap(lng = d$lon, lat = d$lat, intensity = d$res_coded, gradient="RdYlGn")

Upvotes: 1

Related Questions