Pop
Pop

Reputation: 12411

Plot polygon in R

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

enter image description here

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

enter image description here

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?

Upvotes: 2

Views: 11697

Answers (3)

Rentrop
Rentrop

Reputation: 21507

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: enter image description here

Upvotes: 1

Spacedman
Spacedman

Reputation: 94277

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.

Upvotes: 10

Roland
Roland

Reputation: 132969

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

resulting plot

Upvotes: 9

Related Questions