Learn R Programming

DiagrammeR (version 0.8.4)

get_similar_nbrs: Get neighboring nodes based on node attribute similarity

Description

With a graph a single node serving as the starting point, get those nodes in a potential neighborhood of nodes (adjacent to the starting node) that have a common or similar (within threshold values) node attribute to the starting node.

Usage

get_similar_nbrs(graph, node, node_attr, tol_abs = NULL, tol_pct = NULL)

Arguments

graph
a graph object of class dgr_graph that is created using create_graph.
node
a single-length vector containing a node ID value.
node_attr
the name of the node attribute to use to compare with adjacent nodes.
tol_abs
if the values contained in the node attribute node_attr are numeric, one can optionally supply a numeric vector of length 2 that provides a lower and upper numeric bound as criteria for neighboring node similarity to the starting node.
tol_pct
if the values contained in the node attribute node_attr are numeric, one can optionally supply a numeric vector of length 2 that specifies lower and upper bounds as negative and positive percentage changes to the value of the starting node. These bounds serve as criteria for neighboring node similarity to the starting node.

Value

a vector of node ID values.

Examples

Run this code
library(magrittr)

# Getting similar neighbors can be done through
# numerical comparisons; start with creating a
# random, directed graph with 18 nodes and 22 edges
random_graph <-
  create_random_graph(
    n = 18,
    m = 22,
    directed = TRUE,
    fully_connected = TRUE,
    set_seed = 20) %>%
  set_global_graph_attrs(
    'graph', 'layout', 'sfdp') %>%
  set_global_graph_attrs(
    'graph', 'overlap', 'false')

# This graph cannot be shown in this help page
# but you may be interested in displaying it with
# `render_graph()`
random_graph %>% render_graph

# The `create_random_graph()` function randomly
# assigns numerical values to all nodes (as the
# `value` attribute) from 0 to 10 and to 1 decimal
# place. By starting with node (`8`), we can test
# whether any nodes adjacent and beyond are
# numerically equivalent in `value`
random_graph %>%
  get_similar_nbrs(
    node = 8,
    node_attr = 'value')
#> [1] NA

# There are no nodes neighboring `8` that have a
# `value` node attribute equal to `1.0` as node does
#
# We can, however, set a tolerance for ascribing
# similarly by using either the `tol_abs` or
# `tol_pct` arguments (the first applies absolute
# lower and upper bounds from the value in the
# starting node and the latter uses a percentage
# difference to do the same); try setting `tol_abs`
# with a fairly large range to determine if several
# nodes can be selected
random_graph %>%
  get_similar_nbrs(
    node = 8,
    node_attr = 'value',
    tol_abs = c(3, 3))
#> [1] "3"  "9"  "10" "13" "17" "18"

# That resulted in a fairly large set of 7
# neigboring nodes; For sake of example, setting the
# range to be very large will effectively return all
# nodes in the graph except for the starting node
random_graph %>%
  get_similar_nbrs(
    node = 8,
    node_attr = 'value',
    tol_abs = c(10, 10)) %>%
    length
#> [1] 17

Run the code above in your browser using DataLab