如何在空间图上绘制热图 [英] How to plot a heat map on a spatial map

查看:387
本文介绍了如何在空间图上绘制热图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中是空间数据分析的新手,想要做一些简单的事情,我仍然有困难...
我有一张大桌子,里面有纬度经度

  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. 构建热图显示积分更集中的地方...


  3. 容易吗?如果不是,你有一些指针(只有英国请)
    干杯

    解决方案

    这是你的想法?

    您的样本太小而不能显示热图,所以我创建了一个更大的样本, long,lat)=( - 1.52),( - 2.54)和( - 4.5,56)。海事组织的地图将更具信息性,没有点。



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

    pre $ setwd(<包含所有文件的目录>)
    library(rgdal )#for readOGR(...)
    library(ggplot2)
    library(RColorBrewer)#for brewer.pal(...)

    sample< - data.frame (经度= 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 + rorm(50,0,0.5),56 + rorm(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(color =red)+
    geom_path(data = map.df ,aes(x = long,y = lat,group = group),color =grey50)+
    scale_fill_gradientn(colors = rev(brewer.pal(7,Spectral))+
    xlim(-10,+ 2.5)+
    coord_fixed()

    解释:

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

      ggplot  - 建立`sample`作为默认数据集并将(经度,纬度)定义为(x ,y)
    stat_density2d - 热图图层;具有基于点的相对频率的填充颜色的多边形
    geom_point - 点
    geom_path - 映射(管理区域的边界)
    scale_fill_gradientn - 定义用于填充的颜色
    xlim - x轴限制
    coord_fixed - 强制纵横比= 1,所以地图不失真


    I am new to spatial data analysis in R and would like to do something easy, I am still having difficulties... I have a big table with latitudes and 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"
    ))
    

    I got a map of the UK from GADM (level 2 of UK map).

    I would like to be able to

    1. plot points defined by longitude/latitude on the map
    2. build a heat map that shows where the points are more concentrated...

    Is it easy ? If not do you have some pointers (only UK please) Cheers

    解决方案

    Is this what you had in mind?

    Your sample was too small to demonstrate a heat map, so I created a bigger sample with artificial clusters at (long,lat) = (-1,52), (-2,54) and (-4.5,56). IMO the map would be more informative without the points.

    Also, I downloaded the shapefile, not the .Rdata, and imported that. The reason is that you are much more likely to find shapefiles in other projects, and it is easy to import them into 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()
    

    Explanation:

    This approach uses the ggplot package, which allows you to create layers and then render the map. The calls do the following:

    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
    

    这篇关于如何在空间图上绘制热图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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