Skip to content

Instantly share code, notes, and snippets.

@agila5
Created November 9, 2021 09:20
Show Gist options
  • Save agila5/77195078df36afbf539d54516d999067 to your computer and use it in GitHub Desktop.
Save agila5/77195078df36afbf539d54516d999067 to your computer and use it in GitHub Desktop.
# 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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment