简体   繁体   English

从R中的一组点绘制地图上的平滑区域

[英]Plotting a smoothed area on a map from a set of points in R

how do I plot an area around a set of points on a map in R? 如何在R中的地图上绘制一组点周围的区域? eg 例如

map('world')
map.axes()
p <- matrix(c(50, 50, 80, 100, 70, 40, 25, 60), ncol=2) # make some points
points(p, pch=19, col="red")

polygon(p, col="blue")

... which gives me a polygon with a vertex at each of the points, but it looks rather crappy. ...它给了我一个在每个点都有一个顶点的多边形,但看起来很糟糕。 Is there any way to "smooth" the polygon into some sort of curve? 有没有办法将多边形“平滑”成某种曲线?

One option is to make a polygon bounded by a Bézier curve, using the bezier function in the Hmisc package. 一种选择是使用Hmisc包中的bezier函数制作由Bézier曲线限定的多边形。 However I cannot get the start/end point to join up neatly. 但是我不能得到开始/结束点整齐地加入。 For example: 例如:

## make some points
p <- matrix(c(50, 50, 80, 100, 70, 40, 25, 60), ncol=2)
## add the starting point to the end
p2 <- cbind(1:5,p[c(1:4,1),])
## linear interpolation between these points                            
t.coarse <- seq(1,5,0.05)
x.coarse <- approx(p2[,1],p2[,2],xout=t.coarse)$y
y.coarse <- approx(p2[,1],p2[,3],xout=t.coarse)$y
## create a Bezier curve                                           
library(Hmisc)
bz <- bezier(x.coarse,y.coarse)
library(maps)
map('world')
map.axes()
polygon(bz$x,bz$y, col=rgb(0,0,1,0.5),border=NA)

Here's one way, draw the polygon and make it as pretty as you like. 这是一种方法,绘制多边形并使其尽可能漂亮。 This really has nothing to do with areas on maps, more about how you generate the vertices of your polygon. 这实际上与地图上的区域无关,更多的是关于如何生成多边形的顶点。

    library(maps)
    p <- matrix(c(50, 50, 80, 100, 70, 40, 25, 60), ncol=2)     
    plot(p, pch = 16, col = "red", cex = 3, xlim = range(p[,1]) + c(-10,10), ylim = range(p[,2]) + c(-5, 5))
    map(add = TRUE)
    #click until happy, right-click "stop" to finish
    p <- locator(type = "l")
    map()
    polygon(cbind(p$x, p$y), col = "blue")

Otherwise you could interpolate intermediate vertices and smooth them somehow, and in the context of a lon/lat map maybe with use reprojection to get more realistic line segments - but depends on your purpose. 否则你可以插入中间顶点并以某种方式平滑它们,并且在lon / lat地图的上下文中可以使用重新投影来获得更真实的线段 - 但取决于你的目的。

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

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