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))
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))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.