Skip to contents

Detects communities in a network with one of the community detection algorithms of igraph. Each method calls the matching community_*() function.

Usage

communities(
  x,
  method = c("louvain", "leiden", "fast_greedy", "walktrap", "infomap",
    "label_propagation", "edge_betweenness", "leading_eigenvector", "spinglass",
    "optimal", "fluid"),
  community = NULL,
  weights = NULL,
  resolution = 1,
  directed = NULL,
  seed = NULL,
  ...
)

Arguments

x

Network input: matrix, igraph, network, CographNetwork, cograph_network, or tna object.

method

Community detection algorithm. One of "louvain" (default; Louvain modularity optimization), "leiden" (Leiden algorithm), "fast_greedy" (greedy modularity optimization), "walktrap" (random walks), "infomap" (map equation), "label_propagation" (label propagation), "edge_betweenness" (Girvan-Newman), "leading_eigenvector" (leading eigenvector of the modularity matrix), "spinglass" (spinglass model), "optimal" (exact modularity maximization) or "fluid" (fluid communities).

community

Optional integer or character vector. If supplied, the returned data frame is filtered to rows whose community column matches one of the given values. Default NULL (keep all communities).

weights

Edge weights. NULL (default) uses the edge weights of the network when present and otherwise runs unweighted. NA runs unweighted.

resolution

Resolution parameter of the louvain and leiden methods. For louvain, higher values yield more communities. Default 1.

directed

Logical. Whether the edge betweenness method treats the network as directed. NULL (default) uses the direction of the network. The other methods ignore it.

seed

Random seed for reproducibility. It applies to the stochastic methods (louvain, leiden, infomap, label_propagation, spinglass).

...

Additional arguments passed to the community_*() function of the chosen method, for example no.of.communities for "fluid".

Value

A cograph_communities data frame with one row per node and the columns

node

Node label (character).

community

Community number (numeric).

The attributes "algorithm" (method name), "modularity" (modularity of the partition, NA when igraph does not compute it), "network" (the input x) and "igraph_result" (the igraph communities object) hold the metadata. When community is supplied, only the matching rows are kept.

Details

The louvain, leiden, fast_greedy, leading_eigenvector and fluid methods require an undirected graph. For a directed input this function prints a message and runs "walktrap" instead. Called directly, community_louvain() and community_leiden() raise an igraph error on a directed graph, while community_fast_greedy(), community_leading_eigenvector() and community_fluid() collapse it to an undirected graph with summed weights.

Negative edge weights are replaced by their absolute values for all methods except spinglass and optimal, which receive the weights as they are.

MethodTypical use
louvainLarge undirected networks
leidenLarge undirected networks, well-connected communities
fast_greedyMedium-sized networks, hierarchical merges
walktrapDirected or undirected networks, hierarchical merges
infomapDirected networks with flow structure
label_propagationVery large networks
edge_betweennessSmall networks, hierarchical splits
leading_eigenvectorUndirected networks, hierarchical splits
spinglassSmall connected networks, negative weights
optimalNetworks of at most about 50 nodes
fluidConnected networks with a known number of communities

Printing and plotting

Printing the result shows the algorithm, the number of nodes and communities, the modularity, the community sizes and the node table. The result is itself a data frame. plot() on the result is documented in plot-results.

Examples

communities(regulation_net, method = "walktrap")
#> Community structure (walktrap)
#>   Nodes: 10  | Communities: 2  | Modularity: 0.1976 
#>   Sizes: 5, 5 
#> 
#>        node community
#>     Explore         1
#>        Plan         2
#>     Monitor         2
#>       Adapt         1
#>     Reflect         1
#>     Discuss         1
#>  Synthesize         1
#>    Evaluate         2
#>      Create         2
#>       Share         2