| ward_spatial | R Documentation |
Performs spatially-constrained hierarchical clustering using Ward's minimum variance method. Only spatially contiguous areas can be merged, ensuring all resulting regions are spatially connected.
ward_spatial(
data,
attrs = NULL,
n_regions,
weights = "queen",
bridge_islands = FALSE,
scale = TRUE,
verbose = FALSE
)
data |
An sf object with polygon or point geometries. |
attrs |
Character vector of column names to use for clustering
(e.g., |
n_regions |
Integer. Number of regions (clusters) to create. |
weights |
Spatial weights specification. Can be:
|
bridge_islands |
Logical. If TRUE, automatically connect disconnected components (e.g., islands) using nearest-neighbor edges. If FALSE (default), the function will error when the spatial weights graph is disconnected. |
scale |
Logical. If TRUE (default), standardize attributes before clustering. |
verbose |
Logical. Print progress messages. |
This function implements spatially-constrained agglomerative hierarchical clustering using Ward's minimum variance criterion. Unlike standard Ward clustering, this version enforces spatial contiguity by only allowing clusters that share a border to be merged.
The algorithm:
Starts with each observation as its own cluster
At each step, finds the pair of adjacent clusters with minimum Ward distance (increase in total within-cluster variance)
Merges them into a single cluster
Repeats until the desired number of regions is reached
The result guarantees that all regions are spatially contiguous.
An sf object with a .region column containing cluster assignments.
Metadata is stored in the "spopt" attribute.
library(sf)
nc <- st_read(system.file("shape/nc.shp", package = "sf"))
# Cluster into 8 spatially-contiguous regions
result <- ward_spatial(nc, attrs = c("SID74", "SID79"), n_regions = 8)
plot(result[".region"])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.