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

查看:23
本文介绍了添加“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/view

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

这是前 20 条记录:

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:扩展可重现的示例.

推荐答案

您的示例数据没有任何值来实际映射密度叠加.

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

您可以使用 colorBin() 指定 bin 的数量,然后使用 pal 函数指定这些 bin.您可以根据 data_values 发行版中的需要以不同方式设置 bin.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天全站免登陆