如何在 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")
...这给了我一个多边形,每个点都有一个顶点,但它看起来相当糟糕。有没有办法将多边形“平滑”成某种曲线?
bezier
一种选择是使用包中的函数制作一个由贝塞尔曲线界定的多边形Hmisc
。但是我无法让起点/终点整齐地加入。例如:
## 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)
这是一种方法,绘制多边形并使其尽可能漂亮。这实际上与地图上的区域无关,更多的是关于如何生成多边形的顶点。
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")
否则,您可以插入中间顶点并以某种方式对其进行平滑处理,并且在 lon/lat 地图的上下文中,可能会使用重投影来获得更逼真的线段 - 但取决于您的目的。