添加"rgb"图例到R传单热图 [英] Add "rgb" legend to R leaflet heatmap

查看:142
本文介绍了添加"rgb"图例到R传单热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 leaflet (尤其是 leaflet.extras 软件包中的 addHeatmap()命令)制作了一些交互式热图,并且使用了 shiny .创建了所需的地图后,我想在其中添加图例.

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.

我感兴趣的是一个"rgb"图例,它基于由 addHeatmap()从纯长/纬度坐标推导出的密度值. 我需要的是这样的地图- https://www .patrick-wied.at/static/heatmapjs/example-legend-tooltip.html -不幸的是,我对JS不了解,因此无法在R方面重写此代码/为该代码添加正确的JS代码片段我的问题.

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.

到目前为止,我尝试的是 addLegend()命令,该命令无法提供所需的结果,因为在这种情况下,我需要指定一个变量,以便为其准备图例.我还尝试从创建的传单对象中提取颜色范围和分配的值,但是没有成功.

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.

这里是完整数据的链接,可在以下位置运行可复制的示例: https://drive.google.com/file/d/1h3jL_PU6DGTtdIWBK02Tt37R7IB2ArH9/viewa >

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

这是前20条记录:

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.

推荐答案

您的样本数据没有任何值可以实际映射密度叠加层.

Your sample data does not have any values to it to actually map a density overlay.

您可以使用colorBin()指定垃圾箱的数量,然后使用pal函数指定那些垃圾箱.您可以根据需要在data_values发行版中不同地设置垃圾箱. colorBin()的帮助部分可帮助您确定满足您需求的正确参数.

You can specify the number of bins with colorBin() and then specify those bins with your pal function. You can set the bins differently depending on your needs at the data_values distributions. The help section of colorBin() is helpful in identifying the correct parameters for your needs.

bins <- c(0,1,2,3,4)
pal <- colorBin("Spectral", domain = data_value, bins = bins, na.color = "transparent")

m <-leaflet() %>% 
      addTiles() %>% 
      addHeatmap(lng= long_cords, lat = lat_cords, intensity = data_value,
                 blur = 20, max = 400, radius = 15, cellSize = 3) %>%     
      addLegend(pal = pal, values = data_value,
                title="Heat map legend")

您必须使用addHeatmap参数来获得正确的透明度和密度设置.

You'll have to play around with the addHeatmap arguments to get an the right transparency and density settings.

这篇关于添加"rgb"图例到R传单热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆