【问题标题】:How to plot a heat map on a spatial map如何在空间图上绘制热图
【发布时间】:2014-02-15 19:07:36
【问题描述】:

我是 R 空间数据分析的新手,想做一些简单的事情,但我仍然遇到困难...... 我有一张大桌子,上面有 latitudeslongitudes

sample = structure(list(Longitude = c(-0.19117, -0.211708, -0.206458, 
-0.173862, -0.156618), Latitude = c(51.489096, 51.520075, 51.525301, 
51.482442, 51.495752), Location_Easting_OSGR = c(525680L, 524170L, 
524520L, 526900L, 528060L), Location_Northing_OSGR = c(178240L, 
181650L, 182240L, 177530L, 179040L)), .Names = c("Longitude", 
"Latitude", "Location_Easting_OSGR", "Location_Northing_OSGR"
), row.names = c(NA, -5L), class = c("data.table", "data.frame"
))

我从GADM(英国地图的第 2 级)那里得到了一张英国地图。

我希望能够

  1. 由地图上的经度/纬度定义的绘图点
  2. 建立一个热图,显示点更集中的位置...

容易吗?如果没有,你有什么建议吗(请仅限英国) 干杯

【问题讨论】:

  • 您想要热图还是等值线图?前者根据点的集中度绘制区域或等高线。后者根据给定区域中的点数为现有地图区域着色。
  • 是的 jihoward 这就是我要找的东西

标签: r ggplot2 geospatial


【解决方案1】:

这是你的想法吗?

您的 sample 太小而无法展示热图,因此我创建了一个更大的样本,其中包含 (long,lat) = (-1,52)、(-2,54) 和 (-4.5) 的人工集群, 56)。如果没有这些点,IMO 地图会提供更多信息。

另外,我下载了 shapefile,而不是 .Rdata,然后将其导入。原因是您更有可能在其他项目中找到 shapefile,并且很容易将它们导入 R。

setwd("< directory with all your files>")
library(rgdal)         # for readOGR(...)
library(ggplot2)
library(RColorBrewer)  # for brewer.pal(...)

sample <- data.frame(Longitude=c(-1+rnorm(50,0,.5),-2+rnorm(50,0,0.5),-4.5+rnorm(50,0,.5)),
                     Latitude =c(52+rnorm(50,0,.5),54+rnorm(50,0,0.5),56+rnorm(50,0,.5)))
UKmap  <- readOGR(dsn=".",layer="GBR_adm2")
map.df <- fortify(UKmap)

ggplot(sample, aes(x=Longitude, y=Latitude)) + 
  stat_density2d(aes(fill = ..level..), alpha=0.5, geom="polygon")+
  geom_point(colour="red")+
  geom_path(data=map.df,aes(x=long, y=lat,group=group), colour="grey50")+
  scale_fill_gradientn(colours=rev(brewer.pal(7,"Spectral")))+
  xlim(-10,+2.5) +
  coord_fixed()

说明:

这种方法使用ggplot 包,它允许您创建图层然后渲染地图。这些调用执行以下操作:

ggplot -         establish `sample` as the default dataset and define (Longitude,Latitude) as (x,y)
stat_density2d - heat map layer; polygons with fill color based on relative frequency of points
geom_point -     the points
geom_path -      the map (boundaries of the admin regions)
scale_fill_gradientn - defines which colors to use for the fill
xlim -           x-axis limits
coord_fixed -    force aspect ratio = 1, so map is not distorted

【讨论】:

  • 非常好,我有一个问题。如果每个点都附加了另一个属性(假设地面高程),您如何复制相同的东西(意味着 ggplot 在空间位置上具有地面高程的填充轮廓)?谢谢,我希望我的问题足够清楚
猜你喜欢
  • 2020-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多