在R中绘制多边形

在R中绘制多边形,r,polygon,R,Polygon,我想从一个点样本(实际上,多边形是一个凸包)中绘制一个多边形,其坐标为 x <- c(0.66, 0.26, 0.90, 0.06, 0.94, 0.37) y <- c(0.99, 0.20, 0.38, 0.77, 0.71, 0.17) 但这不是我所期望的。。。我想要的是下面的情节: plot(x,y,type="n") polygon(x,y) text(x,y,1:length(x)) 我通过以下方式获得了最后一个图: good.order <- c(1,5,

我想从一个点样本(实际上,多边形是一个凸包)中绘制一个多边形,其坐标为

x <- c(0.66, 0.26, 0.90, 0.06, 0.94, 0.37)
y <- c(0.99, 0.20, 0.38, 0.77, 0.71, 0.17)

但这不是我所期望的。。。我想要的是下面的情节:

plot(x,y,type="n")
polygon(x,y)
text(x,y,1:length(x))

我通过以下方式获得了最后一个图:

good.order <- c(1,5,3,6,2,4)
plot(x,y,type="n")
polygon(x[good.order], y[good.order])
text(x,y,1:length(x))

good.order这里有一种可能性。其想法是使用围绕中心的角度进行排序:

x <- c(0.66, 0.26, 0.90, 0.06, 0.94, 0.37)
y <- c(0.99, 0.20, 0.38, 0.77, 0.71, 0.17)

xnew <- x[order(Arg(scale(x) + scale(y) * 1i))]
ynew <- y[order(Arg(scale(x) + scale(y) * 1i))]

plot(xnew, ynew, type = "n")
polygon(xnew ,ynew)
text(x, y, 1:length(x))

x假设一个凸多边形,只需取一个中心点并计算角度,然后按增加角度的顺序排列

> pts = cbind(x,y)
> polygon(pts[order(atan2(x-mean(x),y-mean(y))),])
请注意,您的
良好。订单的任何循环都将起作用,我的订单给出:

> order(atan2(x-mean(x),y-mean(y)))
[1] 6 2 4 1 5 3

可能是因为我在
atan2
中混合了
x
y
,所以它的想法旋转了90度,就像这里的问题一样。

只要使用
几何体
包和函数
convhulln

ps <- matrix(rnorm(3000), ncol=3) # generate points on a sphere
ps <- sqrt(3)*ps/drop(sqrt((ps^2) %*% rep(1, 3)))
ts.surf <- t(convhulln(ps)) # see the qhull documentations for the options
rgl.triangles(ps[ts.surf,1],ps[ts.surf,2],ps[ts.surf,3],col="blue",alpha=.2)
这里是他们提供的示例(请参见
?convhulln


ps@Pop您有责任提供一个具有代表性的示例。但是,使用围绕中心的角度的基本思想可以扩展到更高的维度。