1

Why does my polygon look like two triangles instead of a box? Can some help explain how I can turn the polygon into a box?

data <- structure(list(AREA = c("a", "a", "b", "b"), Lat = c(43.68389835, 
43.68389835, 44.3395883, 44.3395883), Long = c(-88.22909367, 
-88.99888743, -88.22909367, -88.99888743)), row.names = c(NA, 
-4L), spec = structure(list(cols = list(AREA = structure(list(), class = c("collector_character", 
"collector")), Lat = structure(list(), class = c("collector_double", 
"collector")), Long = structure(list(), class = c("collector_double", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), delim = ","), class = "col_spec"), problems = <pointer: 0x000002548f014500>, class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

Code:

library(tidyverse)
ggplot() + geom_polygon(data=data, mapping=aes(x=Long, y=Lat))

1 Answer 1

4

Currently geom_polygon draws the polygon in exactly the order of the points as given in data. To have a closed polygon, you need to order your points appropriately, either clockwise or anti-clockwise.

We can do this by calculating the angle relative to the lat/long centre, and then order points according to that angle.

library(tidyverse)
data %>%
    mutate(angle = atan2(Lat - mean(Lat), Long - mean(Long))) %>%
    arrange(desc(angle)) %>%
    ggplot() + 
    geom_polygon(aes(x = Long, y = Lat))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.