Learn R Programming

cograph (version 2.7.2)

set_groups: Set Node Groups

Description

Assigns node groupings to a cograph_network object. Groups are stored as metadata with a type column ("layer", "cluster", or "group") for use by specialized plot functions.

Usage

set_groups(
  x,
  groups = NULL,
  type = c("group", "cluster", "layer"),
  nodes = NULL,
  layers = NULL,
  clusters = NULL
)

Value

The modified cograph_network object with node_groups set.

Arguments

x

A cograph_network object.

groups

Node groupings in one of these formats:

  • Character string: Community detection method ("louvain", "walktrap", "fast_greedy", "label_prop", "infomap", "leiden")

  • Named list: Group name -> node vector mapping (e.g., list(A = c("N1","N2"), B = c("N3","N4")))

  • Unnamed vector: Group assignment per node (same order as nodes)

  • Data frame: Must have "node"/"nodes" column plus one of "layer"/"layers", "cluster"/"clusters", or "group"/"groups" (plural forms are automatically normalized to singular)

  • NULL: Use nodes + one of layers/clusters vectors

type

Group type. One of "group" (default), "cluster", or "layer". Ignored when using layers or clusters vector arguments since the type is inferred from which argument is provided.

nodes

Character vector of node labels. Use with layers, clusters, to specify groupings via vectors instead of a data frame.

layers

Character/factor vector of layer assignments (same length as nodes).

clusters

Character/factor vector of cluster assignments (same length as nodes).

See Also

get_groups, splot, detect_communities

Examples

Run this code
set.seed(1)
mat <- matrix(runif(100), 10, 10)
mat <- (mat + t(mat)) / 2; diag(mat) <- 0
rownames(mat) <- colnames(mat) <- paste0("N", 1:10)
net <- as_cograph(mat)

# Named list -> layers
net <- set_groups(net, list(
  Macro = paste0("N", 1:3),
  Meso  = paste0("N", 4:7),
  Micro = paste0("N", 8:10)
), type = "layer")
get_groups(net)

Run the code above in your browser using DataLab