我试图返回一组点的边界(如下所示)。我尝试了三种使用 {sf} 包的方法。
创建测试数据
t <-
crossing(x=1:5, y=1:5) %>%
filter(!(x > 3 & y > 3)) %>%
st_as_sf(coords=c("x", "y"))
Run Code Online (Sandbox Code Playgroud)
ggplot()+
geom_sf(data=t)+
geom_segment(aes(x = c(1, 1, 3, 3, 5, 5),
xend = c(1, 3, 3, 5, 5, 1),
y = c(1, 5, 5, 3, 3, 1),
yend = c(5, 5, 3, 3, 1, 1),
col = "What I want"))
Run Code Online (Sandbox Code Playgroud)
ggplot()+
geom_sf(data=t)+
geom_sf(data = t %>% st_union() %>% st_convex_hull(), aes(col="st_convex_hull"), fill=NA)+
geom_sf(data = t %>% st_union() %>% st_cast("POLYGON") %>% st_boundary(), …Run Code Online (Sandbox Code Playgroud)