# packages
library(tidygraph)
#>
#> Attaching package: 'tidygraph'
#> The following object is masked from 'package:stats':
#>
#> filter
library(sfnetworks)
# data
set.seed(1)
my_graph <- play_geometry(n = 200, radius = 0.10)
my_sfn <- as_sfnetwork(my_graph, coords = c("x", "y"))
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
# morph using to_components (i.e. divide the graph into a list where each
# element represents one component) and plot each component
par(mar = rep(0, 4))
plot(my_sfn)
mapply(
FUN = function(x, y) plot(x, col = y, add = TRUE),
x = morph(my_sfn, to_components),
y = 1:6
)
Created on 2021-11-09 by the reprex package (v2.0.1)