Add “rgb” legend to R leaflet heatmap

旧城冷巷雨未停 提交于 2020-01-24 05:50:05

问题


I made some interactive heatmaps using leaflet (particularly the addHeatmap() command from the leaflet.extras package) and shiny. Having created a desired map, I would like to add a legend to it.

What I am interested in is a "rgb" legend, based on density values deduced by addHeatmap() from pure long/lat coords. What I need is something like this map - https://www.patrick-wied.at/static/heatmapjs/example-legend-tooltip.html - unfortunately I have no knowledge of JS and can't rewrite this code in terms of R/include the right fragment of JS code for my problem.

What I tried so far is the addLegend() command, which does not give the desired result, as in this case I would need to specify a variable for which the legend would be prepared. I also tried to extract the color range and assigned values from created leaflet object, however with no success.

Here's link to full data to run the reproducible example on: https://drive.google.com/file/d/1h3jL_PU6DGTtdIWBK02Tt37R7IB2ArH9/view

And here's top 20 records:

structure(list(latitude = c(30.309522, 30.24429616, 30.30038194, 
                            30.27752338, 30.23294081, 30.23038507, 
                            30.34285933, 30.24962237, 30.26594744, 
                            30.20515821, 30.22363485, 30.2759184, 
                            30.28283226, 30.33816909, 30.26611565, 
                            30.18835401, 30.26704789, 30.27456699, 
                            30.19237135, 30.1925213), 
              longitude = c(-97.73171047, -97.77446858, -97.77885789, 
                            -97.71919076, -97.58937812, -97.76581095, 
                            -97.73598704, -97.72215443, -97.74144275, 
                            -97.8782895, -97.78329845, -97.71321066, 
                            -97.70820152, -97.82413058, -97.7327258, 
                            -97.81606795, -97.68989589, -97.7580592, 
                            -97.7816127, -97.73138523)), 
                .Names =  c("latitude", "longitude"), row.names = 
                                c(NA, 20L), class = "data.frame")      

Here's an example code, which I'd like to extend by the mentioned functionality:

library(magrittr)
library(leaflet)
library(leaflet.extras)

data <- read.csv('DATA.csv')
leaflet(data) %>%
  addTiles(group="OSM") %>%
  addHeatmap(group="heat", lng = ~longitude, lat = ~latitude, max=.5, blur = 60)

And here is the result of that code (on whole dataset):

https://i.stack.imgur.com/6VFNC.jpg

So to sum up what I would like to do: based on such picture I would like to extract the range of the drawn colors along with values assigned to them, and draw legend using that information.

Is there something I am missing? It looks like a pretty simple issue, but I've been struggling to find a solution for past few hours. Thanks in advance for any help!

EDIT: extended the reproducible example.

来源:https://stackoverflow.com/questions/48011859/add-rgb-legend-to-r-leaflet-heatmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!