Created
July 18, 2017 09:56
-
-
Save AndrewLJackson/413e3a2abd3519a6406c1cf66948d5a5 to your computer and use it in GitHub Desktop.
use chull to order points correctly for creating polygons.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
theta <- seq(0, 2*pi, length = 360) | |
Z <- data.frame(x = cos(theta), y = sin(theta)) | |
par(mfrow = c(2,2)) | |
# points in order | |
plot(y~x, data = Z, type = "l", main = "Made in order", asp = 1) | |
# jumble the order | |
Z.j <- dplyr::sample_n(Z, size = nrow(Z)) | |
plot(y~x, data = Z.j, type = "l", main = "jumbled", asp = 1) | |
# convert to polygon window | |
ch <- with(Z.j, chull(x, y)) | |
# chull doesnt close the circle so need to add the first index on at the end | |
plot(y[c(ch, ch[1])]~x[c(ch, ch[1])], data = Z.j, type = "l", main = "Re-ordered", asp = 1) | |
# make a polygon in ggplot | |
library(ggplot2) | |
# i think geom_polygon doesnt need the first index repeated | |
ggplot(Z.j[ch,], aes(x,y)) + geom_polygon() |
ah, that's really great, thanks Mark
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Stumbled across this today and it solved my problem - thank you ! Slightly expanded,
ggplot
-suitable version for when you have grouping variables below, in case it's useful: