Skip to contents

These functions forms a spatial extension to the grouping functions in tidygraph, allowing to detect communities with spatial clustering algorithms.

Usage

group_spatial_dbscan(epsilon, min_pts = 1, use_network_distance = TRUE, ...)

Arguments

epsilon

The value of the epsilon parameter for the DBSCAN clustering algorithm, defining the radius of the neighborhood of a node.

min_pts

The value of the minPts parameter for the DBSCAN clustering algorithm, defining the minimum number of points in the neighborhood to be considered a core point.

use_network_distance

Should the distance between nodes be computed as the distance over the network (using st_network_distance? Defaults to TRUE. If set to FALSE, the straight-line distance (using st_distance) is computed instead.

...

Additional arguments passed on to the clustering algorithm.

Value

A numeric vector with the membership for each node in the network. The enumeration happens in order based on group size progressing from the largest to the smallest group.

Details

Just as with all grouping functions in tidygraph, spatial grouping functions are meant to be called inside tidygraph verbs such as mutate or filter, where the network that is currently being worked on is known and thus not needed as an argument to the function. If you want to use an algorithm outside of the tidygraph framework you can use with_graph to set the context temporarily while the algorithm is being evaluated.

Functions

  • group_spatial_dbscan(): Uses density-based spatial clustering as implemented in the dbscan function of the dbscan package. This requires the dbscan package to be installed. Each node marked as noise will form its own cluster.

Examples

library(tidygraph, quietly = TRUE)

play_geometric(10, 0.5) |>
  activate(nodes) |>
  mutate(group = group_spatial_dbscan(0.25))
#> # A sfnetwork: 10 nodes and 23 edges
#> #
#> # An undirected simple graph with 1 component and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 0.03743103 ymin: 0.1712643 xmax: 0.9709666 ymax: 0.9735399
#> # CRS: NA
#> #
#> # Node data: 10 × 2 (active)
#>                 geometry group
#>                  <POINT> <int>
#> 1 (0.03743103 0.4357716)     2
#> 2 (0.03893649 0.9735399)     1
#> 3   (0.261088 0.9575766)     1
#> 4  (0.3334272 0.6399788)     1
#> 5  (0.3795592 0.6188382)     1
#> 6  (0.3984854 0.3467482)     3
#> # ℹ 4 more rows
#> #
#> # Edge data: 23 × 3
#>    from    to                                    geometry
#>   <int> <int>                                <LINESTRING>
#> 1     1     4 (0.03743103 0.4357716, 0.3334272 0.6399788)
#> 2     1     5 (0.03743103 0.4357716, 0.3795592 0.6188382)
#> 3     1     6 (0.03743103 0.4357716, 0.3984854 0.3467482)
#> # ℹ 20 more rows