简体   繁体   English

将绘图限制为R中的shapefile边界

[英]Restricting plot to shapefile boundaries in r

I have analysed a dataset of GPS points using density.ppp to generate a sort of heatmap of intensity of the points, as shown below: 我使用density.ppp分析了GPS点的数据集,以生成点强度的热图,如下所示:

在此处输入图片说明

However, I would like the image to be limited to the borders of the shapefile, similar to below: 但是,我希望图像仅限于shapefile的边界,如下所示:

在此处输入图片说明

The first image is called as 第一张图片称为

x <- readShapePoly("dk.shp")
xlim<-c(min(912),max(920))
ylim<-c(min(8023),max(8030))
a<-ppp(cases@coords[,1], cases@coords[,2], xlim, ylim, unitname=c("km"))
plot(density.ppp(a, 0.1), col=COLORS)
plot(x, add=T, border="white")

where cases@coords are the GPS coordinates of each point of interest, and x is a shapefile which provides the outline for the geographical unit. 其中cases @ coords是每个感兴趣点的GPS坐标,x是一个shapefile,提供了地理单位的轮廓。

The second image is called using this code: 使用此代码调用第二个图像:

plot(x, axes=T, col=COLORS, border="White")

Does anyone know how this might be done? 有谁知道该怎么做? Perhaps it's not possible with plot() and I will need another package. 使用plot()可能无法实现,而我需要另一个软件包。

As an aside, the next step I plan to do will be to overlay this image over a map imported from GoogleEarth. 顺便说一句,我下一步计划将图像叠加在从GoogleEarth导入的地图上。 I'm not yet sure how to do that either, but will post the answer if and when I work it out 我也不确定该怎么做,但是会在我制定出答案时发布答案

many thanks 非常感谢

The result of density.ppp has a matrix (v) that contains the information, if the points outside of the polygon of interst are changed to NA before it is plotted then they will not plot. 结果density.ppp有一个矩阵(V),它包含的信息,如果刘晓丹的多边形外的点改为NA之前绘制那么他们将不会绘制。 Here is an example of doing this: 这是执行此操作的示例:

library(maptools)
library(sp)
library(spatstat)

xx <- readShapePoly(system.file("shapes/sids.shp", package="maptools")[1],
      IDvar="FIPSNO", proj4string=CRS("+proj=longlat +ellps=clrk66"))

x <- rnorm(25, -80, 2)
y <- rnorm(25, 35, 1 )

tmp <- density( ppp(x,y, xrange=range(x), yrange=range(y)) )
plot(tmp)
plot(xx, add=TRUE)
points(x,y)

tmp2 <- SpatialPoints( expand.grid( tmp$yrow, tmp$xcol )[,2:1],
    proj4string=CRS(proj4string(xx)) )

tmp3 <- over( tmp2, xx )

tmp$v[ is.na( tmp3[[1]] ) ] <- NA

plot(tmp)
plot(xx, add=TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM