0

I created these shape objects through K-Means clustering but I want them to have touching borders without doing anything too manual. Any assistance would be great. Working with crime data and it doesn't cover every inch of the city, but I want the map I'm making to be able to. Also the only contents of the data sets are lat long coords and clusters.

crime.Brooklyn.Q1 <- crime.Brooklyn.Q1 %>% st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)
crime.Brooklyn.Q2 <- crime.Brooklyn.Q2 %>% st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)
crime.Brooklyn.Q3 <- crime.Brooklyn.Q3 %>% st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)
crime.Brooklyn.Q4 <- crime.Brooklyn.Q4 %>% st_as_sf(coords = c("Longitude", "Latitude"), crs = 4326)

polygons.bk.1 <- crime.Brooklyn.Q1 %>% 
  group_by(cluster) %>% 
  summarise() %>%
  #transform to polygon
  st_cast("POLYGON") %>% 
  #clean up polygon - create a smoothed polygon using outermost points
  #"The convex hull is the smallest convex geometry that encloses all geometries in the input"-definition
  st_convex_hull()

polygons.bk.2 <- crime.Brooklyn.Q2 %>% 
  group_by(cluster) %>% 
  summarise() %>%
  #transform to polygon
  st_cast("POLYGON") %>% 
  #clean up polygon - create a smoothed polygon using outermost points
  #"The convex hull is the smallest convex geometry that encloses all geometries in the input"-definition
  st_convex_hull()

polygons.bk.3 <- crime.Brooklyn.Q3 %>% 
  group_by(cluster) %>% 
  summarise() %>%
  #transform to polygon
  st_cast("POLYGON") %>% 
  #clean up polygon - create a smoothed polygon using outermost points
  #"The convex hull is the smallest convex geometry that encloses all geometries in the input"-definition
  st_convex_hull()

polygons.bk.4 <- crime.Brooklyn.Q4 %>% 
  group_by(cluster) %>% 
  summarise() %>%
  #transform to polygon
  st_cast("POLYGON") %>% 
  #clean up polygon - create a smoothed polygon using outermost points
  #"The convex hull is the smallest convex geometry that encloses all geometries in the input"-definition
  st_convex_hull()

polygons.bk <- rbind(polygons.bk.1, polygons.bk.2, polygons.bk.3, polygons.bk.4)

leaflet() %>%
    addProviderTiles("CartoDB.Positron") %>%
    setView(lng = -80.8858673, lat = 41.1450276, zoom = 5) %>%
    addPolygons(data = polygons.bk)

enter image description here

1
  • 1
    Search SO Delaunay and Voronoï tessellations, that is what you are wanting to achieve anyway. You can create a Voronoi tessellation based on k-means. Check out [this link] for more info. And there are quite a few solutions on SO as well.
    – L Tyrone
    Commented Dec 4, 2023 at 9:37

0

Browse other questions tagged or ask your own question.