Take a P2P network query and implement the Leiden community detection method. To run this function, you will require all the pre-requisites of the 'leiden' package installed, which includes Python and 'reticulate'.

  hrvar = "Organization",
  bg_fill = "#000000",
  font_col = "#FFFFFF",
  algorithm = "mds",
  path = "network_p2p_leiden",
  node_alpha = 0.8,
  res = 0.5,
  seed = 1,
  desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"),
  return = "plot-leiden",
  size_threshold = 5000



Data frame containing a person-to-person query.


String containing the label for the HR attribute.


String to specify background fill colour.


String to specify font and link colour.


String to specify the node placement algorithm to be used. Defaults to "mds" for the deterministic multi-dimensional scaling of nodes. See https://rdrr.io/cran/ggraph/man/layout_tbl_graph_igraph.html for a full list of options.


File path for saving the PDF output. Defaults to a timestamped path based on current parameters.


A numeric value between 0 and 1 to specify the transparency of the nodes. Defaults to 0.7.


Resolution parameter to be passed to leiden::leiden(). Defaults to 0.5.


Seed for the random number generator passed to either set.seed() when the Louvain algorithm is used, or leiden::leiden() when the Leiden algorithm is used, to ensure consistency. Only applicable when display is set to "louvain" or "leiden".


Character vector of length 3 containing the HR attributes to use when returning the "describe" output. See network_describe().


String specifying what output to return. Defaults to "plot-leiden". Valid return options include:

  • 'plot-leiden': return a network plot coloured by leiden communities, saving a PDF to path.

  • 'plot-hrvar': return a network plot coloured by HR attribute, saving a PDF to path.

  • 'plot-sankey': return a sankey plot combining communities and HR attribute.

  • 'table': return a vertex summary table with counts in communities and HR attribute.

  • 'data': return a vertex data file that matches vertices with communities and HR attributes.

  • 'describe': return a list of data frames which describe each of the identified communities. The first data frame is a summary table of all the communities.

  • 'network': return 'igraph' object.


Numeric value representing the maximum number of edges before network_leiden() switches to use a more efficient, but less elegant plotting method (native igraph). Defaults to 5000. Set as 0 to coerce to a fast plotting method every time, and Inf to always use the default plotting method (with 'ggraph').


See return.

Simulating and running Leiden Community Detection

Below is an example on how to simulate a network and run the function.

# Simulate a small person-to-person dataset
p2p_data <- p2p_data_sim(size = 50)

# Return leiden, console, plot
p2p_data %>%
  network_leiden(path = NULL,
                 return = "plot")