r热图-stat_density2d(ggmap)与addHeatmap(闪亮的传单) [英] r heatmap - stat_density2d (ggmap) vs. addHeatmap (shiny leaflet)

查看:193
本文介绍了r热图-stat_density2d(ggmap)与addHeatmap(闪亮的传单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用library(ggmap)stat_density2d()函数制作了静态热图.希望在动态leaflet地图上的闪亮应用程序中重新创建它,我找到了addHeatmap().但是,生成的图像并不相似,其中ggmap版本似乎提供了正确的结果.

I made static heatmaps with the library(ggmap) and the stat_density2d() function. Looking to recreate this in a shiny app on a dynamic leaflet map, I found addHeatmap(). However, the resulting images are dissimilar, with the ggmap version seemingly offering the correct result.

GGMAP

造成这种差异的原因是什么?

What is causing this difference?

要运行以下两个可重现的示例,您可以下载我放在此处的一些数据(csv文件). https://drive.google.com/drive/folders/0B8_GTHBuoKSRR1VIRmhOUTJKYU0?usp=共享

To run both of the below reproducible examples, you can download some data (csv file) I put here. https://drive.google.com/drive/folders/0B8_GTHBuoKSRR1VIRmhOUTJKYU0?usp=sharing

请注意,leaflet结果随缩放级别而有所不同,但从不匹配ggmap结果(例如,在最大热量的位置上).

Note that the leaflet result differs with zoom level, but never matches the ggmap result (e.g. in terms location of maximum heat).

这是ggmap代码.

library(ggmap)
data <- read.csv("DATA.csv", sep=";")
data <- subset(data, !is.na(CrdLatDeg))
xmin <- min(data$CrdLonDeg)
xmax <- max(data$CrdLonDeg)
ymin <- min(data$CrdLatDeg)
ymax <- max(data$CrdLatDeg)
lon <- c(xmin,xmax)
lat <- c(ymin,ymax)
map <- get_map(location = c(lon = mean(lon), lat = mean(lat)), zoom = 17,
               maptype = "satellite", source = "google")
ggmap(map) + 
  labs(x="longitude", y="latitude") + 
  stat_density2d(data=data, aes(x=CrdLonDeg, y=CrdLatDeg, alpha= ..level.., fill= ..level..), colour=FALSE,
                 geom="polygon", bins=100) + 
  scale_fill_gradientn(colours=c(rev(rainbow(100, start=0, end=.7)))) + scale_alpha(range=c(0,.8)) + 
  guides(alpha=FALSE,fill=FALSE)

这是leaflet代码.

library(leaflet.extras)
data <- read.csv("DATA.csv", sep=";")
data <- subset(data, !is.na(CrdLatDeg))
leaflet(data) %>%
  addTiles(group="OSM") %>%
  addHeatmap(group="heat", lng=~CrdLonDeg, lat=~CrdLatDeg, max=.6, blur = 60)

推荐答案

由于算法不同,图像看起来也有所不同.

The images look different because the algorithms are different.

stat_density2d()从离散数据中推断出概率密度函数.

stat_density2d() extrapolates a probability density function from the discrete data.

热图的传单实现依赖于 simpleheat webgl-heatmap .这些热图不依赖概率密度. (我不完全确定r-leaflet的addHeatmap使用了其中的哪一个.)

Leaflet implementation of heatmaps rely on libraries like simpleheat, heatmap.js or webgl-heatmap. These heatmaps do not rely on probability density. (I'm not fully sure which of these is used by r-leaflet's addHeatmap).

相反,这些热图的工作原理是为每个点绘制一个模糊的圆圈,将每个像素的值增加一个与该点的强度成正比的量(在您的情况下为常数),而与该点之间的距离成反比和圈子.每个数据点在热图中均显示为一个圆圈.您可以通过在 heatmap.js网页中使用鼠标光标来查看此内容,或者通过查看图片右上角的这个孤独点:

Instead, these heatmaps work by drawing a blurred circle for each point, raising the value of each pixel by an amount directly proportional to the intensity of the point (constant in your case), and inversely proportional to the distance between the point and the circle. Every data point is shown in the heatmap as a circle. You can see this by playing with your mouse cursor in the heatmap.js webpage, or by looking at this lone point in the top-right of your image:

像功能图的可视化一样思考热图

Think of a heatmap like a visualization of the function

f(像素)= ∑(max(0,半径-距离(像素,点))·强度(点))

f(pixel) = ∑ ( max( 0, radius - distance(pixel, point) ) · intensity(point) )

一个人可以调整热图的半径和强度,但结果永远不会与统计密度估计相同.

One can tweak the radius and intensity of heatmaps, but the result will never be the same as a statistical density estimation.

这篇关于r热图-stat_density2d(ggmap)与addHeatmap(闪亮的传单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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