Plot polygon in R

拈花ヽ惹草 提交于 2019-12-07 21:10:47

问题


I want to plot a polygon from a sample of points (in practice, the polygon is a convex hull) whose coordinates are

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)

When I apply the polygon function I get the following plot:

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

But it is not what I expect... What I want is the following plot:

I obtained this last plot by doing:

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

My question

Basically, my question is: how to obtain the vector of indices (called good order in the code above) which will allow to get the polygon I want?


回答1:


Assuming a convex polygon, just take a central point and compute the angle, then order in increasing angle.

> pts = cbind(x,y)
> polygon(pts[order(atan2(x-mean(x),y-mean(y))),])

Note that any cycle of your good.order will work, mine gives:

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

probably because I've mixed x and y in atan2 and so its thinking about it rotated by 90 degrees, like that matters here.




回答2:


Here is one possibility. The idea is to use the angle around the center for ordering:

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




回答3:


Just use the geometry package with the function convhulln

Here the example they provide (see ?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)

For plotting you need the rgl-package

Result:



来源:https://stackoverflow.com/questions/26756615/plot-polygon-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!