Learn R Programming

wpa (version 1.4.3)

network_p2p: Create a network plot with the person-to-person query

Description

[Experimental]

Pass a data frame containing a person-to-person query and return a network visualization. Options are available for community detection using either the Louvain or the Leiden algorithms.

Usage

network_p2p(
  data,
  hrvar = "Organization",
  display = "hrvar",
  return = "plot",
  path = paste0("network_p2p_", display),
  desc_hrvar = c("Organization", "LevelDesignation", "FunctionType"),
  bg_fill = "#000000",
  font_col = "#FFFFFF",
  legend_pos = "bottom",
  palette = "rainbow",
  node_alpha = 0.7,
  res = 0.5,
  seed = 1,
  algorithm = "mds",
  size_threshold = 5000
)

Arguments

data

Data frame containing a person-to-person query.

hrvar

String containing the label for the HR attribute.

display

String determining what output to return. Valid values include:

  • "hrvar" (default): compute analysis or visuals without computing communities.

  • "louvain": compute analysis or visuals with community detection, using the Louvain algorithm.

  • "leiden": compute analysis or visuals with community detection, using the Leiden algorithm. This requires all the pre-requisites of the leiden package installed, which includes Python and reticulate.

return

String specifying what output to return. This must be one of the following strings:

  • 'plot' (default)

  • 'sankey'

  • 'table'

  • 'data'

  • 'describe'

  • 'network'

See Value for more information.

path

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

desc_hrvar

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

bg_fill

String to specify background fill colour.

font_col

String to specify font and link colour.

legend_pos

String to specify position of legend. Defaults to "bottom". See ggplot2::theme().

palette

Function for generating a colour palette with a single argument n. Uses "rainbow" by default.

node_alpha

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

res

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

seed

Seed for the random number generator passed to leiden::leiden() to ensure consistency. Only applicable when display is set to "leiden".

algorithm

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.

size_threshold

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.

Value

A different output is returned depending on the value passed to the return argument:

  • 'plot': return a network plot.

  • 'sankey': return a sankey plot combining communities and HR attribute. This is only valid if a community detection method is selected at display.

  • '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. This is only valid if a community detection method is selected at display.

  • 'network': return 'igraph' object.

Running Leiden communities

Running Leiden communities requires python dependencies installed. You can run the following:

# Return a network plot to console, coloured by Leiden communities
  p2p_data %>%
    network_p2p(display = "leiden",
                path = NULL,
                return = "plot")

See Also

Other Network: external_network_plot(), g2g_data, internal_network_plot(), network_describe(), network_g2g(), network_leiden(), network_louvain(), p2p_data_sim()

Examples

Run this code
# NOT RUN {
# Simulate a small person-to-person dataset
p2p_data <- p2p_data_sim(size = 50)

# Return a network plot to console, coloured by hrvar
p2p_data %>%
  network_p2p(display = "hrvar",
              path = NULL,
              return = "plot")

# Return a network plot to console, coloured by Louvain communities
p2p_data %>%
  network_p2p(display = "louvain",
              path = NULL,
              return = "plot")


# Return a network plot to console
# Coloured by Leiden communities
# Using Fruchterman-Reingold force-directed layout algorithm
# Force the use of fast plotting method
p2p_data %>%
  network_p2p(display = "hrvar",
              path = NULL,
              return = "plot",
              algorithm = "lgl",
              size_threshold = 0)

# Return a data frame matching HR variable and communities to nodes
# Using Louvain communities
p2p_data %>%
  network_p2p(display = "louvain",
              return = "data",
              algorithm = "fr")

# }

Run the code above in your browser using DataLab