3

I am trying to obtain a polygon from each group of points "G" but I obtain only one single polygon for all the points. I grouped data by the G variable but it doesn't work.

library(sf)
library(tidyverse)

    data.frame(ID = 1:10,
               X = sample(1:10, 10),
               Y = sample(1:10, 10),
               G= c("a","a","a","a", "b","b","b","c","c","c")) %>% 
    st_as_sf(coords = c("X", "Y")) %>% 
    group_by(G) %>% 
    st_union() %>% 
    st_convex_hull() %>% 
    plot()
3

To union on groups use summarise:

data.frame(ID = 1:10,
               X = sample(1:10, 10),
               Y = sample(1:10, 10),
               G= c("a","a","a","a", "b","b","b","c","c","c")) %>% 
    st_as_sf(coords = c("X", "Y")) %>% 
    group_by(G) %>% 
    summarise() %>% 
    st_convex_hull() %>% 
    plot()

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.