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

4
我该如何在R中绘制一组点周围的区域呢?例如,在地图上。
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")

... 这使我得到了一个多边形,每个点都是其中一个顶点,但它看起来有些糟糕。有没有办法将多边形“平滑”成某种曲线呢?

2个回答

3

一种选择是使用Hmisc包中的bezier函数,制作由贝塞尔曲线限定的多边形。但是我无法使起点/终点完美地连接在一起。例如:

## 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)

0

这里有一种方法,绘制多边形并使其尽可能美观。这与地图上的区域无关,更多的是关于如何生成多边形的顶点。

    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")

否则,您可以插值中间顶点并以某种方式平滑它们,在经度/纬度地图的情况下,可能会使用重投影来获得更真实的线段 - 但这取决于您的目的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接