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

发布于 2024-09-12 12:46:39 字数 280 浏览 6 评论 0原文

如何在 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")

......这给了我一个在每个点都有一个顶点的多边形,但它看起来相当蹩脚。有什么方法可以将多边形“平滑”成某种曲线吗?

how do I plot an area around a set of points on a map in R? e.g.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毁梦 2024-09-19 12:46:39

一种选择是使用 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)

One option is to make a polygon bounded by a Bézier curve, using the bezier function in the Hmisc package. 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)
梦回梦里 2024-09-19 12:46:39

这是一种方法,绘制多边形并使其像您喜欢的那样漂亮。这实际上与地图上的区域无关,更多地与如何生成多边形的顶点有关。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文