View source: R/connect_subgraphs.R
connect_subgraphs | R Documentation |
The function returns a neighbour list of class nb
and its associated spatial adjacency matrix
computed by merging disjoint connected subgraphs through its nearest polygon centroids.
connect_subgraphs(carto, ID.area = NULL, nb = NULL, plot = FALSE)
carto |
object of class |
ID.area |
character vector of geographic identifiers. |
nb |
optional argument with the neighbours list of class |
plot |
logical value (default |
This function first calls the add_neighbour
function to search for isolated areas.
This function returns a list with the following two elements:
nb
: the modified neighbours list
W
: associated spatial adjacency matrix of class dgCMatrix
library(spdep)
## Load the Spain colorectal cancer mortality data ##
data(Carto_SpainMUN)
## Select the polygons (municipalities) of the 'Comunidad Valenciana' region ##
carto <- Carto_SpainMUN[Carto_SpainMUN$region=="Comunidad Valenciana",]
carto.nb <- poly2nb(carto)
n.comp.nb(carto.nb)$nc # 2 disjoint connected subgraphs
## Plot the spatial polygons and its neighbourhood graph
op <- par(mfrow=c(1,2), pty="s")
plot(carto$geometry, main="Original neighbourhood graph")
plot(carto.nb, st_centroid(st_geometry(carto), of_largest_polygon=TRUE),
pch=19, cex=0.5, col="red", add=TRUE)
## Use the 'connect_subgraphs' function ##
carto.mod <- connect_subgraphs(carto=carto, ID.area="ID", nb=carto.nb, plot=TRUE)
title(main="Modified neighbourhood graph")
n.comp.nb(carto.mod$nb)$nc==1
par(op)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.