我是R中空间数据分析的新手,想要做一些简单的事情,我仍然遇到困难......
我有一张包含latitudes
和longitudes
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级)获得了英国地图。
我希望能够
容易吗?如果没有,你有一些指针(请只有英国) 干杯
答案 0 :(得分:20)
这是你的想法吗?
你的sample
太小而无法展示热图,所以我在(长,纬度)=( - 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