Movatterモバイル変換


[0]ホーム

URL:


Type:Package
Title:A Tidy API for Graph Manipulation
Version:1.3.1
Maintainer:Thomas Lin Pedersen <thomasp85@gmail.com>
Description:A graph, while not "tidy" in itself, can be thought of as two tidy data frames describing node and edge data respectively. 'tidygraph' provides an approach to manipulate these two virtual data frames using the API defined in the 'dplyr' package, as well as provides tidy interfaces to a lot of common graph algorithms.
License:MIT + file LICENSE
URL:https://tidygraph.data-imaginist.com,https://github.com/thomasp85/tidygraph
BugReports:https://github.com/thomasp85/tidygraph/issues
Imports:cli, dplyr (≥ 0.8.5), igraph (≥ 2.0.0), lifecycle, magrittr,pillar, R6, rlang, stats, tibble, tidyr, tools, utils
Suggests:ape, covr, data.tree, graph, influenceR, methods, netrankr,NetSwan, network, seriation, testthat (≥ 3.0.0)
LinkingTo:cpp11
Encoding:UTF-8
RoxygenNote:7.3.1
Config/testthat/edition:3
NeedsCompilation:yes
Packaged:2024-01-30 12:17:51 UTC; thomas
Author:Thomas Lin PedersenORCID iD [cre, aut]
Repository:CRAN
Date/Publication:2024-01-30 13:40:02 UTC

tidygraph: A Tidy API for Graph Manipulation

Description

logo

A graph, while not "tidy" in itself, can be thought of as two tidy data frames describing node and edge data respectively. 'tidygraph' provides an approach to manipulate these two virtual data frames using the API defined in the 'dplyr' package, as well as provides tidy interfaces to a lot of common graph algorithms.

Author(s)

Maintainer: Thomas Lin Pedersenthomasp85@gmail.com (ORCID)

See Also

Useful links:


Determine the context of subsequent manipulations

Description

As atbl_graph can be considered as a collection of two linked tables it isnecessary to specify which table is referenced during manipulations. Theactivate verb does just that and needs affects all subsequent manipulationsuntil a new table is activated.active is a simple query function to getthe currently acitve context. In addition to the use ofactivate it is alsopossible to activate nodes or edges as part of the piping using the⁠%N>%⁠and⁠%E>%⁠ pipes respectively. Do note that this approach somewhat obscureswhat is going on and is thus only recommended for quick, one-line, fixes ininteractive use.

Usage

activate(.data, what)active(x)lhs %N>% rhslhs %E>% rhs

Arguments

.data,x,lhs

A tbl_graph or a grouped_tbl_graph

what

What should get activated? Possible values arenodes oredges.

rhs

A function to pipe into

Value

A tbl_graph

Note

Activate will ungroup a grouped_tbl_graph.

Examples

gr <- create_complete(5) %>%  activate(nodes) %>%  mutate(class = sample(c('a', 'b'), 5, TRUE)) %>%  activate(edges) %>%  arrange(from)# The above could be achieved using the special pipes as wellgr <- create_complete(5) %N>%  mutate(class = sample(c('a', 'b'), 5, TRUE)) %E>%  arrange(from)# But as you can see it obscures what part of the graph is being targeted

A data structure for tidy graph manipulation

Description

Thetbl_graph class is a thin wrapper around anigraph object thatprovides methods for manipulating the graph using the tidy API. As it is justa subclass ofigraph every igraph method will work as expected. Agrouped_tbl_graph is the equivalent of agrouped_df where either thenodes or the edges has been grouped. Thegrouped_tbl_graph is notconstructed directly but by using thegroup_by() verb. After creation of atbl_graph the nodes are activated by default. The context can be changedusing theactivate() verb and affects all subsequent operations. Changingcontext automatically drops any grouping. The current active context canalways be extracted withas_tibble(), which drops the graph structure andjust returns atbl_df or agrouped_df depending on the state of thetbl_graph. The returned context can be overriden by using theactiveargument inas_tibble().

Usage

## S3 method for class 'data.frame'as_tbl_graph(x, directed = TRUE, ...)## S3 method for class 'Node'as_tbl_graph(x, directed = TRUE, mode = "out", ...)## S3 method for class 'dendrogram'as_tbl_graph(x, directed = TRUE, mode = "out", ...)## S3 method for class 'graphNEL'as_tbl_graph(x, ...)## S3 method for class 'graphAM'as_tbl_graph(x, ...)## S3 method for class 'graphBAM'as_tbl_graph(x, ...)## S3 method for class 'hclust'as_tbl_graph(x, directed = TRUE, mode = "out", ...)## S3 method for class 'igraph'as_tbl_graph(x, ...)## S3 method for class 'list'as_tbl_graph(x, directed = TRUE, node_key = "name", ...)## S3 method for class 'matrix'as_tbl_graph(x, directed = TRUE, ...)## S3 method for class 'network'as_tbl_graph(x, ...)## S3 method for class 'phylo'as_tbl_graph(x, directed = NULL, ...)## S3 method for class 'evonet'as_tbl_graph(x, directed = TRUE, ...)tbl_graph(nodes = NULL, edges = NULL, directed = TRUE, node_key = "name")as_tbl_graph(x, ...)## Default S3 method:as_tbl_graph(x, ...)is.tbl_graph(x)

Arguments

x

An object convertible to atbl_graph

directed

Should the constructed graph be directed (defaults toTRUE)

...

Arguments passed on to the conversion function

mode

In casedirected = TRUE should the edge direction be away fromnode or towards. Possible values are"out" (default) or"in".

node_key

The name of the column innodes that character representedto andfrom columns should be matched against. IfNA the first columnis always chosen. This setting has no effect ifto andfrom are given asintegers.

nodes

Adata.frame containing information about the nodes in thegraph. Ifedges$to and/oredges$from are characters then they will bematched to the column named according tonode_key in nodes, if it exists.If not, they will be matched to the first column.

edges

Adata.frame containing information about the edges in thegraph. The terminal nodes of each edge must either be encoded in ato andfrom column, or in the two first columns, as integers. These integers refer tonodes index.

Details

Constructors are provided for most data structures that resembles networks.If a class provides anigraph::as.igraph() method it is automaticallysupported.

Value

Atbl_graph object

Functions

Examples

rstat_nodes <- data.frame(name = c("Hadley", "David", "Romain", "Julia"))rstat_edges <- data.frame(from = c(1, 1, 1, 2, 3, 3, 4, 4, 4),                            to = c(2, 3, 4, 1, 1, 2, 1, 2, 3))tbl_graph(nodes = rstat_nodes, edges = rstat_edges)

Add graphs, nodes, or edges to a tbl_graph

Description

These functions are tbl_graph pendants todplyr::bind_rows() that allowsyou to grow yourtbl_graph by adding rows to either the nodes data, theedges data, or both. As withbind_rows() columns are matched by name andare automatically filled withNA if the column doesn't exist in someinstances. In the case ofbind_graphs() the graphs are automaticallyconverted totbl_graph objects prior to binding. The edges in each graphwill continue to reference the nodes in the graph where they originated,meaning that their terminal node indexes will be shifted to match the newindex of the node in the combined graph. This means thebind_graphs()always result in a disconnected graph. Seegraph_join() for merging graphson common nodes.

Usage

bind_graphs(.data, ...)bind_nodes(.data, ...)bind_edges(.data, ..., node_key = "name")

Arguments

.data

Atbl_graph, or a list oftbl_graph objects (forbind_graphs()).

...

In case ofbind_nodes() andbind_edges() data.frames to add.In the case ofbind_graphs() objects that are convertible totbl_graphusingas_tbl_graph().

node_key

The name of the column innodes that character representedto andfrom columns should be matched against. IfNA the first columnis always chosen. This setting has no effect ifto andfrom are given asintegers.

Value

Atbl_graph containing the new data

Examples

graph <- create_notable('bull')new_graph <- create_notable('housex')# Add nodesgraph %>% bind_nodes(data.frame(new = 1:4))# Add edgesgraph %>% bind_edges(data.frame(from = 1, to = 4:5))# Add graphsgraph %>% bind_graphs(new_graph)

Calculate node and edge centrality

Description

The centrality of a node measures the importance of node in the network. Asthe concept of importance is ill-defined and dependent on the network andthe questions under consideration, many centrality measures exist.tidygraph provides a consistent set of wrappers for all the centralitymeasures implemented inigraph for use insidedplyr::mutate() and otherrelevant verbs. All functions provided bytidygraph have a consistentnaming scheme and automatically calls the function on the graph, returning avector with measures ready to be added to the node data. Furthertidygraphprovides access to thenetrankr engine for centrality calculations anddefine a number of centrality measures based on that, as well as provide amanual mode for specifying more-or-less any centrality score. These measuresall only work on undirected graphs.

Usage

centrality_alpha(  weights = NULL,  alpha = 1,  exo = 1,  tol = 1e-07,  loops = FALSE)centrality_authority(weights = NULL, scale = TRUE, options = arpack_defaults())centrality_betweenness(  weights = NULL,  directed = TRUE,  cutoff = -1,  normalized = FALSE)centrality_power(exponent = 1, rescale = FALSE, tol = 1e-07, loops = FALSE)centrality_closeness(  weights = NULL,  mode = "out",  normalized = FALSE,  cutoff = NULL)centrality_eigen(  weights = NULL,  directed = FALSE,  scale = TRUE,  options = arpack_defaults())centrality_hub(weights = NULL, scale = TRUE, options = arpack_defaults())centrality_pagerank(  weights = NULL,  directed = TRUE,  damping = 0.85,  personalized = NULL)centrality_subgraph(loops = FALSE)centrality_degree(  weights = NULL,  mode = "out",  loops = TRUE,  normalized = FALSE)centrality_edge_betweenness(weights = NULL, directed = TRUE, cutoff = NULL)centrality_harmonic(  weights = NULL,  mode = "out",  normalized = FALSE,  cutoff = NULL)centrality_manual(relation = "dist_sp", aggregation = "sum", ...)centrality_closeness_harmonic()centrality_closeness_residual()centrality_closeness_generalised(alpha)centrality_integration()centrality_communicability()centrality_communicability_odd()centrality_communicability_even()centrality_subgraph_odd()centrality_subgraph_even()centrality_katz(alpha = NULL)centrality_betweenness_network(netflowmode = "raw")centrality_betweenness_current()centrality_betweenness_communicability()centrality_betweenness_rsp_simple(rspxparam = 1)centrality_betweenness_rsp_net(rspxparam = 1)centrality_information()centrality_decay(alpha = 1)centrality_random_walk()centrality_expected()

Arguments

weights

The weight of the edges to use for the calculation. Will beevaluated in the context of the edge data.

alpha

Relative importance of endogenous vs exogenous factors (centrality_alpha), the exponent to the power transformation of the distance metric (centrality_closeness_generalised), the base of power transformation (centrality_decay), or the attenuation factor (centrality_katz)

exo

The exogenous factors of the nodes. Either a scalar or a numbernumber for each node. Evaluated in the context of the node data.

tol

Tolerance for near-singularities during matrix inversion

loops

Should loops be included in the calculation

scale

Should the output be scaled between 0 and 1

options

Settings passed on toigraph::arpack()

directed

Should direction of edges be used for the calculations

cutoff

maximum path length to use during calculations

normalized

Should the output be normalized

exponent

The decay rate for the Bonacich power centrality

rescale

Should the output be scaled to sum up to 1

mode

How should edges be followed. Ignored for undirected graphs

damping

The damping factor of the page rank algorithm

personalized

The probability of jumping to a node when abandoning arandom walk. Evaluated in the context of the node data.

relation

The indirect relation measure type to be used innetrankr::indirect_relations

aggregation

The aggregation type to use on the indirect relations to be used innetrankr::aggregate_positions

...

Arguments to pass on tonetrankr::indirect_relations

netflowmode

The return type of the network flow distance, either'raw' or'frac'

rspxparam

inverse temperature parameter

Value

A numeric vector giving the centrality measure of each node.

Functions

Examples

create_notable('bull') %>%  activate(nodes) %>%  mutate(importance = centrality_alpha())# Most centrality measures are for nodes but not allcreate_notable('bull') %>%  activate(edges) %>%  mutate(importance = centrality_edge_betweenness())

Graph games based on connected components

Description

This set of graph creation algorithms simulate the topology by, in some way,connecting subgraphs. The nature of their algorithm is described in detail atthe linked igraph documentation.

Usage

play_blocks(n, size_blocks, p_between, directed = TRUE, loops = FALSE)play_blocks_hierarchy(n, size_blocks, rho, p_within, p_between)play_islands(n_islands, size_islands, p_within, m_between)play_smallworld(  n_dim,  dim_size,  order,  p_rewire,  loops = FALSE,  multiple = FALSE)

Arguments

n

The number of nodes in the graph.

size_blocks

The number of vertices in each block

p_between,p_within

The probability of edges within and between groups/blocks

directed

Should the resulting graph be directed

loops

Are loop edges allowed

rho

The fraction of vertices per cluster

n_islands

The number of densely connected islands

size_islands

The number of nodes in each island

m_between

The number of edges between groups/islands

n_dim,dim_size

The dimension and size of the starting lattice

order

The neighborhood size to create connections from

p_rewire

The rewiring probability of edges

multiple

Are multiple edges allowed

Value

A tbl_graph object

Functions

See Also

Other graph games:evolution_games,sampling_games,type_games

Examples

plot(play_islands(4, 10, 0.7, 3))

Access graph, nodes, and edges directly inside verbs

Description

These three functions makes it possible to directly access either the nodedata, the edge data or the graph itself while computing inside verbs. It ise.g. possible to add an attribute from the node data to the edges based onthe terminating nodes of the edge, or extract some statistics from the graphitself to use in computations.

Usage

.G().N(focused = TRUE).E(focused = TRUE)

Arguments

focused

Should only the attributes of the currently focused nodes oredges be returned

Value

Either atbl_graph (.G()) or atibble (.N())

Functions

Examples

# Get data from the nodes while computing for the edgescreate_notable('bull') %>%  activate(nodes) %>%  mutate(centrality = centrality_power()) %>%  activate(edges) %>%  mutate(mean_centrality = (.N()$centrality[from] + .N()$centrality[to])/2)

Create different types of well-defined graphs

Description

These functions creates a long list of different types of well-defined graphs,that is, their structure is not based on any randomisation. All of thesefunctions are shallow wrappers around a range of⁠igraph::make_*⁠ functionsbut returnstbl_graph rather thanigraph objects.

Usage

create_ring(n, directed = FALSE, mutual = FALSE)create_path(n, directed = FALSE, mutual = FALSE)create_chordal_ring(n, w)create_de_bruijn(alphabet_size, label_size)create_empty(n, directed = FALSE)create_bipartite(n1, n2, directed = FALSE, mode = "out")create_citation(n)create_complete(n)create_notable(name)create_kautz(alphabet_size, label_size)create_lattice(dim, directed = FALSE, mutual = FALSE, circular = FALSE)create_star(n, directed = FALSE, mutual = FALSE, mode = "out")create_tree(n, children, directed = TRUE, mode = "out")

Arguments

n,n1,n2

The number of nodes in the graph

directed

Should the graph be directed

mutual

Should mutual edges be created in case of the graph beingdirected

w

A matrix specifying the additional edges in the chordan ring. Seeigraph::make_chordal_ring()

alphabet_size

The number of unique letters in the alphabet used forthe graph

label_size

The number of characters in each node

mode

In case of a directed, non-mutual, graph should the edges flow'out' or'in'

name

The name of a notable graph. See a complete list inigraph::make_graph()

dim

The dimensions of the lattice

circular

Should each dimension in the lattice wrap around

children

The number of children each node has in the tree (if possible)

Value

A tbl_graph

Functions

Examples

# Create a complete graph with 10 nodescreate_complete(10)

Calculate edge ranking

Description

This set of functions tries to calculate a ranking of the edges in a graph sothat edges sharing certain topological traits are in proximity in theresulting order.

Usage

edge_rank_eulerian(cyclic = FALSE)

Arguments

cyclic

should the eulerian path start and end at the same node

Value

An integer vector giving the position of each edge in the ranking

Functions

Examples

graph <- create_notable('meredith') %>%  activate(edges) %>%  mutate(rank = edge_rank_eulerian())

Querying edge types

Description

These functions lets the user query whether the edges in a graph is of aspecific type. All functions return a logical vector giving whether each edgein the graph corresponds to the specific type.

Usage

edge_is_multiple()edge_is_loop()edge_is_mutual()edge_is_from(from)edge_is_to(to)edge_is_between(from, to, ignore_dir = !graph_is_directed())edge_is_incident(nodes)edge_is_bridge()edge_is_feedback_arc(weights = NULL, approximate = TRUE)

Arguments

from,to,nodes

A vector giving node indices

ignore_dir

Is both directions of the edge allowed

weights

The weight of the edges to use for the calculation. Will beevaluated in the context of the edge data.

approximate

Should the minimal set be approximated or exact

Value

A logical vector of the same length as the number of edges in thegraph

Functions

Examples

create_star(10, directed = TRUE, mutual = TRUE) %>%  activate(edges) %>%  sample_frac(0.7) %>%  mutate(single_edge = !edge_is_mutual())

Graph games based on evolution

Description

This games create graphs through different types of evolutionary mechanisms(not necessarily in a biological sense). The nature of their algorithm isdescribed in detail at the linked igraph documentation.

Usage

play_citation_age(  n,  growth = 1,  bins = n/7100,  p_pref = (1:(bins + 1))^-3,  directed = TRUE)play_forestfire(  n,  p_forward,  p_backward = p_forward,  growth = 1,  directed = TRUE)play_growing(n, growth = 1, directed = TRUE, citation = FALSE)play_barabasi_albert(  n,  power,  growth = 1,  growth_dist = NULL,  use_out = FALSE,  appeal_zero = 1,  directed = TRUE,  method = "psumtree")play_barabasi_albert_aging(  n,  power,  power_age,  growth = 1,  growth_dist = NULL,  bins = 300,  use_out = FALSE,  appeal_zero = 1,  appeal_zero_age = 0,  directed = TRUE,  coefficient = 1,  coefficient_age = 1,  window = NULL)

Arguments

n

The number of nodes in the graph.

growth

The number of edges added at each iteration

bins

The number of aging bins

p_pref

The probability that an edge will be made to an age bin.

directed

Should the resulting graph be directed

p_forward,p_backward

Forward and backward burning probability

citation

Should a citation graph be created

power

The power of the preferential attachment

growth_dist

The distribution of the number of added edges at each iteration

use_out

Should outbound edges be used for calculating citation probability

appeal_zero

The appeal value for unconnected nodes

method

The algorithm to use for graph creation. Either'psumtree','psumtree-multiple', or'bag'

power_age

The aging exponent

appeal_zero_age

The appeal value of nodes without age

coefficient

The coefficient of the degree dependent part of attrictiveness

coefficient_age

The coefficient of the age dependent part of attrictiveness

window

The aging window to take into account when calculating the preferential attraction

Value

A tbl_graph object

Functions

See Also

play_traits() andplay_citation_type() for an evolutionaryalgorithm based on different node types

Other graph games:component_games,sampling_games,type_games

Examples

plot(play_forestfire(50, 0.5))

Select specific nodes or edges to compute on

Description

Thefocus()/unfocus() idiom allow you to temporarily tell tidygraphalgorithms to only calculate on a subset of the data, while keeping the fullgraph intact. The purpose of this is to avoid having to calculate timecostly measures etc on all nodes or edges of a graph if only a few is needed.E.g. you might only be interested in the shortest distance from one node toanother so rather than calculating this for all nodes you apply a focus onone node and perform the calculation. It should be made clear that not allalgorithms will see a performance boost by being applied to a few nodes/edgessince their calculation is applied globally and the result for allnodes/edges are provided in unison.

Usage

focus(.data, ...)## S3 method for class 'tbl_graph'focus(.data, ...)## S3 method for class 'morphed_tbl_graph'focus(.data, ...)unfocus(.data, ...)## S3 method for class 'tbl_graph'unfocus(.data, ...)## S3 method for class 'focused_tbl_graph'unfocus(.data, ...)## S3 method for class 'morphed_tbl_graph'unfocus(.data, ...)

Arguments

.data

A data frame, data frame extension (e.g. a tibble), or alazy data frame (e.g. from dbplyr or dtplyr). SeeMethods, below, formore details.

...

<data-masking> Expressions thatreturn a logical value, and are defined in terms of the variables in.data. If multiple expressions are included, they are combined with the& operator. Only rows for which all conditions evaluate toTRUE arekept.

Value

A graph with focus applied

Note

focusing is the lowest prioritised operation on a graph. Applying amorph() or agroup_by() operation will unfocus the graph prior toperforming the operation. The same is true for the inverse operations(unmorph() andungroup()). Further, unfocusing will happen any time somegraph altering operation is performed, such as thearrange() andslice()operations


Fortify a tbl_graph for ggplot2 plotting

Description

In generaltbl_graph objects are intended to be plotted by networkvisualisation libraries such asggraph. However, if you do wish to ploteither the node or edge data directly withggplot2 you can simply add thetbl_graph object as either the global or layer data and the currentlyactive data is passed on as a regular data frame.

Usage

fortify.tbl_graph(model, data, ...)

Register a graph context for the duration of the current frame

Description

This function sets the provided graph to be the context for tidygraphalgorithms, such as e.g.node_is_center(), for the duration of the currentenvironment. It automatically removes the graph once the environment exits.

Usage

.graph_context.register_graph_context(graph, free = FALSE, env = parent.frame()).free_graph_context(env = parent.frame())

Arguments

graph

Atbl_graph object

free

Should the active state of the graph be ignored?

env

The environment where the context should be active

Format

An object of classContextBuilder (inherits fromR6) of length 12.


Join graphs on common nodes

Description

This graph-specific join method makes a full join on the nodes data andupdates the edges in the joining graph so they matches the new indexes of thenodes in the resulting graph. Node and edge data is combined usingdplyr::bind_rows() semantic, meaning that data is matched by column nameand filled withNA if it is missing in either of the graphs.

Usage

graph_join(x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), ...)

Arguments

x

Atbl_graph

y

An object convertible to atbl_graph usingas_tbl_graph()

by

A join specification created withjoin_by(), or a charactervector of variables to join by.

IfNULL, the default,⁠*_join()⁠ will perform a natural join, using allvariables in common acrossx andy. A message lists the variables sothat you can check they're correct; suppress the message by supplyingbyexplicitly.

To join on different variables betweenx andy, use ajoin_by()specification. For example,join_by(a == b) will matchx$a toy$b.

To join by multiple variables, use ajoin_by() specification withmultiple expressions. For example,join_by(a == b, c == d) will matchx$a toy$b andx$c toy$d. If the column names are the same betweenx andy, you can shorten this by listing only the variable names, likejoin_by(a, c).

join_by() can also be used to perform inequality, rolling, and overlapjoins. See the documentation at?join_by for details onthese types of joins.

For simple equality joins, you can alternatively specify a character vectorof variable names to join by. For example,by = c("a", "b") joinsx$atoy$a andx$b toy$b. If variable names differ betweenx andy,use a named character vector likeby = c("x_a" = "y_a", "x_b" = "y_b").

To perform a cross-join, generating all combinations ofx andy, seecross_join().

copy

Ifx andy are not from the same data source,andcopy isTRUE, theny will be copied into thesame src asx. This allows you to join tables across srcs, butit is a potentially expensive operation so you must opt into it.

suffix

If there are non-joined duplicate variables inx andy, these suffixes will be added to the output to disambiguate them.Should be a character vector of length 2.

...

Other parameters passed onto methods.

Value

Atbl_graph containing the merged graph

Examples

gr1 <- create_notable('bull') %>%  activate(nodes) %>%  mutate(name = letters[1:5])gr2 <- create_ring(10) %>%  activate(nodes) %>%  mutate(name = letters[4:13])gr1 %>% graph_join(gr2)

Graph measurements

Description

This set of functions provide wrappers to a number ofìgraphs graphstatistic algorithms. As for the other wrappers provided, they are intendedfor use inside thetidygraph framework and it is thus not necessary tosupply the graph being computed on as the context is known. All of thesefunctions are guarantied to return scalars making it easy to compute withthem.

Usage

graph_adhesion()graph_assortativity(attr, in_attr = NULL, directed = TRUE)graph_automorphisms(sh = "fm", colors = NULL)graph_clique_num()graph_clique_count(min = NULL, max = NULL, subset = NULL)graph_component_count(type = "weak")graph_motif_count(size = 3, cut.prob = rep(0, size))graph_diameter(weights = NULL, directed = TRUE, unconnected = TRUE)graph_girth()graph_radius(mode = "out")graph_mutual_count()graph_asym_count()graph_unconn_count()graph_size()graph_order()graph_reciprocity(ignore_loops = TRUE, ratio = FALSE)graph_min_cut(capacity = NULL)graph_mean_dist(directed = TRUE, unconnected = TRUE, weights = NULL)graph_modularity(group, weights = NULL)graph_efficiency(weights = NULL, directed = TRUE)

Arguments

attr

The node attribute to measure on

in_attr

An alternative node attribute to use for incomming node. IfNULL the attribute given bytype will be used

directed

Should a directed graph be treated as directed

sh

The splitting heuristics for the BLISS algorithm. Possible valuesare: ‘f’: first non-singleton cell, ‘fl’: firstlargest non-singleton cell, ‘fs’: first smallest non-singletoncell, ‘fm’: first maximally non-trivially connectednon-singleton cell, ‘flm’: first largest maximallynon-trivially connected non-singleton cell, ‘fsm’: firstsmallest maximally non-trivially connected non-singleton cell.

colors

The colors of the individual vertices of the graph; onlyvertices having the same color are allowed to match each other in anautomorphism. When omitted, igraph uses thecolor attribute of thevertices, or, if there is no such vertex attribute, it simply assumes thatall vertices have the same color. Pass NULL explicitly if the graph has acolor vertex attribute but you do not want to use it.

min,max

The upper and lower bounds of the cliques to be considered.

subset

The indexes of the nodes to start the search from (logical or integer). If provided only the cliques containing these nodes will be counted.

type

The type of component to count, either 'weak' or 'strong'. Ignored for undirected graphs.

size

The size of the motif.

cut.prob

Numeric vector giving the probabilities that the searchgraph is cut at a certain level. Its length should be the same as the sizeof the motif (thesize argument). By default no cuts are made.

weights

Optional positive weight vector for calculating weighteddistances. If the graph has aweight edge attribute, then this isused by default.

unconnected

Logical, what to do if the graph is unconnected. IfFALSE, the function will return a number that is one larger the largestpossible diameter, which is always the number of vertices. If TRUE, thediameters of the connected components will be calculated and the largest onewill be returned.

mode

How should eccentricity be calculated. If"out" only outbound edges are followed. If"in" only inbound are followed. If"all" all edges are followed. Ignored for undirected graphs.

ignore_loops

Logical. Should loops be ignored while calculating the reciprocity

ratio

Should the old "ratio" approach from igraph < v0.6 be used

capacity

The capacity of the edges

group

The node grouping to calculate the modularity on

Value

A scalar, the type depending on the function

Functions

Examples

# Use e.g. to modify computations on nodes and edgescreate_notable('meredith') %>%  activate(nodes) %>%  mutate(rel_neighbors = centrality_degree()/graph_order())

Querying graph types

Description

This set of functions lets the user query different aspects of the graphitself. They are all concerned with wether the graph implements certainproperties and will all return a logical scalar.

Usage

graph_is_simple()graph_is_directed()graph_is_bipartite()graph_is_connected()graph_is_tree()graph_is_forest()graph_is_dag()graph_is_chordal()graph_is_complete()graph_is_isomorphic_to(graph, method = "auto", ...)graph_is_subgraph_isomorphic_to(graph, method = "auto", ...)graph_is_eulerian(cyclic = FALSE)

Arguments

graph

The graph to compare structure to

method

The algorithm to use for comparison

...

Arguments passed on to the comparison methods. Seeigraph::is_isomorphic_to() andigraph::is_subgraph_isomorphic_to()

cyclic

should the eulerian path start and end at the same node

Value

A logical scalar

Functions

Examples

gr <- create_tree(50, 4)with_graph(gr, graph_is_tree())

Group nodes and edges based on community structure

Description

These functions are wrappers around the various clustering functions providedbyigraph. As with the other wrappers they automatically use the graph thatis being computed on, and otherwise passes on its arguments to the relevantclustering function. The return value is always a numeric vector of groupmemberships so that nodes or edges with the same number are part of the samegroup. Grouping is predominantly made on nodes and currently the onlygrouping of edges supported is biconnected components.

Usage

group_components(type = "weak")group_edge_betweenness(weights = NULL, directed = TRUE, n_groups = NULL)group_fast_greedy(weights = NULL, n_groups = NULL)group_infomap(weights = NULL, node_weights = NULL, trials = 10)group_label_prop(weights = NULL, label = NULL, fixed = NULL)group_leading_eigen(  weights = NULL,  steps = -1,  label = NULL,  options = arpack_defaults(),  n_groups = NULL)group_louvain(weights = NULL, resolution = 1)group_leiden(  weights = NULL,  resolution = 1,  objective_function = "CPM",  beta = 0.01,  label = NULL,  n = 2,  node_weights = NULL)group_optimal(weights = NULL)group_spinglass(weights = NULL, ...)group_walktrap(weights = NULL, steps = 4, n_groups = NULL)group_fluid(n_groups = 2)group_biconnected_component()group_color()

Arguments

type

The type of component to find. Either'weak' or'strong'

weights

The weight of the edges to use for the calculation. Will beevaluated in the context of the edge data.

directed

Should direction of edges be used for the calculations

n_groups

Integer scalar, the desired number of communities. If too lowor two high, then an error message is given. The measure is applied to thefull graph so the number of groups returned may be lower for focused graphs

node_weights

The weight of the nodes to use for the calculation. Willbe evaluated in the context of the node data.

trials

Number of times partition of the network should be attempted

label

The initial groups of the nodes. Will be evaluated in thecontext of the node data.

fixed

A logical vector determining which nodes should keep theirinitial groups. Will be evaluated in the context of the node data.

steps

The number of steps in the random walks

options

Settings passed on toigraph::arpack()

resolution

Resolution of the modularity function used internally inthe algorithm

objective_function

Either"CPM" (constant potts model) or"modularity". Sets the objective function to use.

beta

Parameter affecting the randomness in the Leiden algorithm. Thisaffects only the refinement step of the algorithm.

n

The number of iterations to run the clustering

...

arguments passed on toigraph::cluster_spinglass()

Value

a numeric vector with the membership for each node in the graph. Theenumeration happens in order based on group size progressing from the largestto the smallest group

Functions

Examples

create_notable('tutte') %>%  activate(nodes) %>%  mutate(group = group_infomap())

Repeatedly modify a graph by a function

Description

The iterate family of functions allow you to call the same modificationfunction on a graph until some condition is met. This can be used to createsimple simulations in a tidygraph friendly API

Usage

iterate_n(.data, n, .f, ...)iterate_while(.data, cnd, .f, ..., max_n = NULL)

Arguments

.data

Atbl_graph object

n

The number of times to iterate

.f

A function taking in atbl_graph as the first argument andreturning atbl_graph object

...

Further arguments passed on to.f

cnd

A condition that must evaluate toTRUE orFALSE determining ifiteration should continue

max_n

The maximum number of iterations in casecnd never evaluatestoFALSE

Value

Atbl_graph object

Examples

# Gradually remove edges from the least connected nodes while avoiding# isolatescreate_notable('zachary') |>  iterate_n(20, function(gr) {    gr |>      activate(nodes) |>      mutate(deg = centrality_degree(), rank = order(deg)) |>      activate(edges) |>      slice(        -which(edge_is_incident(.N()$rank == sum(.N()$deg == 1) + 1))[1]      )  })# Remove a random edge until the graph is split in twocreate_notable('zachary') |>  iterate_while(graph_component_count() == 1, function(gr) {    gr |>      activate(edges) |>      slice(-sample(graph_size(), 1))  })

Measures based on the neighborhood of each node

Description

These functions wraps a set of functions that all measures quantities of thelocal neighborhood of each node. They all return a vector or list matchingthe node position.

Usage

local_size(order = 1, mode = "all", mindist = 0)local_members(order = 1, mode = "all", mindist = 0)local_triangles()local_ave_degree(weights = NULL)local_transitivity(weights = NULL)

Arguments

order

Integer giving the order of the neighborhood.

mode

Character constant, it specifies how to use the direction ofthe edges if a directed graph is analyzed. For ‘out’ only theoutgoing edges are followed, so all vertices reachable from the sourcevertex in at mostorder steps are counted. For ‘"in"’ allvertices from which the source vertex is reachable in at mostordersteps are counted. ‘"all"’ ignores the direction of the edges. Thisargument is ignored for undirected graphs.

mindist

The minimum distance to include the vertex in the result.

weights

An edge weight vector. Forlocal_ave_degree: If this argumentis given, the average vertex strength is calculated instead of vertex degree.Forlocal_transitivity: if given weighted transitivity using the approach byA. Barrat will be calculated.

Value

A numeric vector or a list (forlocal_members) with elementscorresponding to the nodes in the graph.

Functions

Examples

# Get all neighbors of each graphcreate_notable('chvatal') %>%  activate(nodes) %>%  mutate(neighborhood = local_members(mindist = 1))# These are equivalentcreate_notable('chvatal') %>%  activate(nodes) %>%  mutate(n_neighbors = local_size(mindist = 1),         degree = centrality_degree()) %>%  as_tibble()

Apply a function to nodes in the order of a breath first search

Description

These functions allow you to map over the nodes in a graph, by firstperforming a breath first search on the graph and then mapping over eachnode in the order they are visited. The mapping function will have access tothe result and search statistics for all the nodes between itself and theroot in the search. To map over the nodes in the reverse direction usemap_bfs_back().

Usage

map_bfs(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_lgl(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_chr(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_int(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_dbl(root, mode = "out", unreachable = FALSE, .f, ...)

Arguments

root

The node to start the search from

mode

How should edges be followed?'out' only follows outboundedges,'in' only follows inbound edges, and'all' follows all edges. Thisparameter is ignored for undirected graphs.

unreachable

Should the search jump to an unvisited node if the searchis completed without visiting all nodes.

.f

A function to map over all nodes. See Details

...

Additional parameters to pass to.f

Details

The function provided to.f will be called with the following arguments inaddition to those supplied through...:

Instead of spelling out all of these in the function it is possible to simplyname the ones needed and use... to catch the rest.

Value

map_bfs() returns a list of the same length as the number of nodesin the graph, in the order matching the node order in the graph (that is, notin the order they are called).⁠map_bfs_*()⁠ tries to coerce its result intoa vector of the classeslogical (map_bfs_lgl),character(map_bfs_chr),integer (map_bfs_int), ordouble (map_bfs_dbl).These functions will throw an error if they are unsuccesful, so they are typesafe.

See Also

Other node map functions:map_bfs_back(),map_dfs(),map_dfs_back()

Examples

# Accumulate values along a searchcreate_tree(40, children = 3, directed = TRUE) %>%  mutate(value = round(runif(40)*100)) %>%  mutate(value_acc = map_bfs_dbl(node_is_root(), .f = function(node, path, ...) {    sum(.N()$value[c(node, path$node)])  }))

Apply a function to nodes in the reverse order of a breath first search

Description

These functions allow you to map over the nodes in a graph, by firstperforming a breath first search on the graph and then mapping over eachnode in the reverse order they are visited. The mapping function will haveaccess to the result and search statistics for all the nodes following itselfin the search. To map over the nodes in the original direction usemap_bfs().

Usage

map_bfs_back(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_back_lgl(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_back_chr(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_back_int(root, mode = "out", unreachable = FALSE, .f, ...)map_bfs_back_dbl(root, mode = "out", unreachable = FALSE, .f, ...)

Arguments

root

The node to start the search from

mode

How should edges be followed?'out' only follows outboundedges,'in' only follows inbound edges, and'all' follows all edges. Thisparameter is ignored for undirected graphs.

unreachable

Should the search jump to an unvisited node if the searchis completed without visiting all nodes.

.f

A function to map over all nodes. See Details

...

Additional parameters to pass to.f

Details

The function provided to.f will be called with the following arguments inaddition to those supplied through...:

Instead of spelling out all of these in the function it is possible to simplyname the ones needed and use... to catch the rest.

Value

map_bfs_back() returns a list of the same length as the number ofnodes in the graph, in the order matching the node order in the graph (thatis, not in the order they are called).⁠map_bfs_back_*()⁠ tries to coerceits result into a vector of the classeslogical (map_bfs_back_lgl),character (map_bfs_back_chr),integer (map_bfs_back_int), ordouble(map_bfs_back_dbl). These functions will throw an error if they areunsuccesful, so they are type safe.

See Also

Other node map functions:map_bfs(),map_dfs(),map_dfs_back()

Examples

# Collect values from childrencreate_tree(40, children = 3, directed = TRUE) %>%  mutate(value = round(runif(40)*100)) %>%  mutate(child_acc = map_bfs_back_dbl(node_is_root(), .f = function(node, path, ...) {    if (nrow(path) == 0) .N()$value[node]    else {      sum(unlist(path$result[path$parent == node]))    }  }))

Apply a function to nodes in the order of a depth first search

Description

These functions allow you to map over the nodes in a graph, by firstperforming a depth first search on the graph and then mapping over eachnode in the order they are visited. The mapping function will have access tothe result and search statistics for all the nodes between itself and theroot in the search. To map over the nodes in the reverse direction usemap_dfs_back().

Usage

map_dfs(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_lgl(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_chr(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_int(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_dbl(root, mode = "out", unreachable = FALSE, .f, ...)

Arguments

root

The node to start the search from

mode

How should edges be followed?'out' only follows outboundedges,'in' only follows inbound edges, and'all' follows all edges. Thisparameter is ignored for undirected graphs.

unreachable

Should the search jump to an unvisited node if the searchis completed without visiting all nodes.

.f

A function to map over all nodes. See Details

...

Additional parameters to pass to.f

Details

The function provided to.f will be called with the following arguments inaddition to those supplied through...:

Instead of spelling out all of these in the function it is possible to simplyname the ones needed and use... to catch the rest.

Value

map_dfs() returns a list of the same length as the number of nodesin the graph, in the order matching the node order in the graph (that is, notin the order they are called).⁠map_dfs_*()⁠ tries to coerce its result intoa vector of the classeslogical (map_dfs_lgl),character(map_dfs_chr),integer (map_dfs_int), ordouble (map_dfs_dbl).These functions will throw an error if they are unsuccesful, so they are typesafe.

See Also

Other node map functions:map_bfs(),map_bfs_back(),map_dfs_back()

Examples

# Add a random integer to the last value along a searchcreate_tree(40, children = 3, directed = TRUE) %>%  mutate(child_acc = map_dfs_int(node_is_root(), .f = function(node, path, ...) {    last_val <- if (nrow(path) == 0) 0L else tail(unlist(path$result), 1)    last_val + sample(1:10, 1)  }))

Apply a function to nodes in the reverse order of a depth first search

Description

These functions allow you to map over the nodes in a graph, by firstperforming a depth first search on the graph and then mapping over eachnode in the reverse order they are visited. The mapping function will haveaccess to the result and search statistics for all the nodes following itselfin the search. To map over the nodes in the original direction usemap_dfs().

Usage

map_dfs_back(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_back_lgl(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_back_chr(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_back_int(root, mode = "out", unreachable = FALSE, .f, ...)map_dfs_back_dbl(root, mode = "out", unreachable = FALSE, .f, ...)

Arguments

root

The node to start the search from

mode

How should edges be followed?'out' only follows outboundedges,'in' only follows inbound edges, and'all' follows all edges. Thisparameter is ignored for undirected graphs.

unreachable

Should the search jump to an unvisited node if the searchis completed without visiting all nodes.

.f

A function to map over all nodes. See Details

...

Additional parameters to pass to.f

Details

The function provided to.f will be called with the following arguments inaddition to those supplied through...:

Instead of spelling out all of these in the function it is possible to simplyname the ones needed and use... to catch the rest.

Value

map_dfs_back() returns a list of the same length as the number ofnodes in the graph, in the order matching the node order in the graph (thatis, not in the order they are called).⁠map_dfs_back_*()⁠ tries to coerceits result into a vector of the classeslogical (map_dfs_back_lgl),character (map_dfs_back_chr),integer (map_dfs_back_int), ordouble(map_dfs_back_dbl). These functions will throw an error if they areunsuccesful, so they are type safe.

See Also

Other node map functions:map_bfs(),map_bfs_back(),map_dfs()

Examples

# Collect values from the 2 closest layers of children in a dfs searchcreate_tree(40, children = 3, directed = TRUE) %>%  mutate(value = round(runif(40)*100)) %>%  mutate(child_acc = map_dfs_back(node_is_root(), .f = function(node, path, dist, ...) {    if (nrow(path) == 0) .N()$value[node]    else {      unlist(path$result[path$dist - dist <= 2])    }  }))

Map a function over a graph representing the neighborhood of each node

Description

This function extracts the neighborhood of each node as a graph and maps overeach of these neighborhood graphs. Conceptually it is similar toigraph::local_scan(), but it borrows the type safe versions available inmap_bfs() andmap_dfs().

Usage

map_local(order = 1, mode = "all", mindist = 0, .f, ...)map_local_lgl(order = 1, mode = "all", mindist = 0, .f, ...)map_local_chr(order = 1, mode = "all", mindist = 0, .f, ...)map_local_int(order = 1, mode = "all", mindist = 0, .f, ...)map_local_dbl(order = 1, mode = "all", mindist = 0, .f, ...)

Arguments

order

Integer giving the order of the neighborhood.

mode

Character constant, it specifies how to use the direction ofthe edges if a directed graph is analyzed. For ‘out’ only theoutgoing edges are followed, so all vertices reachable from the sourcevertex in at mostorder steps are counted. For ‘"in"’ allvertices from which the source vertex is reachable in at mostordersteps are counted. ‘"all"’ ignores the direction of the edges. Thisargument is ignored for undirected graphs.

mindist

The minimum distance to include the vertex in the result.

.f

A function to map over all nodes. See Details

...

Additional parameters to pass to.f

Details

The function provided to.f will be called with the following arguments inaddition to those supplied through...:

Theneighborhood graph will contain an extra node attribute called.central_node, which will beTRUE for the node that the neighborhood isexpanded from andFALSE for everything else.

Value

map_local() returns a list of the same length as the number ofnodes in the graph, in the order matching the node order in the graph.⁠map_local_*()⁠ tries to coerce its result into a vector of the classeslogical (map_local_lgl),character (map_local_chr),integer(map_local_int), ordouble (map_local_dbl). These functions will throwan error if they are unsuccesful, so they are type safe.

Examples

# Smooth out values over a neighborhoodcreate_notable('meredith') %>%  mutate(value = rpois(graph_order(), 5)) %>%  mutate(value_smooth = map_local_dbl(order = 2, .f = function(neighborhood, ...) {    mean(as_tibble(neighborhood, active = 'nodes')$value)  }))

Create a temporary alternative representation of the graph to compute on

Description

Themorph/unmorph verbs are used to create temporary representations ofthe graph, such as e.g. its search tree or a subgraph. A morphed graph willaccept any of the standarddplyr verbs, and changes to the data isautomatically propagated to the original graph when unmorphing. Tidygraphcomes with a range ofmorphers, but is it also possible to supply your own.See Details for the requirement for custom morphers. Thecrystallise verbis used to extract the temporary graph representation into a tibblecontaining one separate graph per row and aname andgraph column holdingthe name of each graph and the graph itself respectively.convert() is ashorthand for performing bothmorph andcrystallise along with extractinga singletbl_graph (defaults to the first). For morphs were you know theyonly create a single graph, and you want to keep it, this is an easy way.

Usage

morph(.data, .f, ...)unmorph(.data)crystallise(.data)crystallize(.data)convert(.data, .f, ..., .select = 1, .clean = FALSE)

Arguments

.data

Atbl_graph or amorphed_tbl_graph

.f

A morphing function. Seemorphers for a list of provided one.

...

Arguments passed on to the morpher

.select

The graph to return duringconvert(). Either an index or thename as created duringcrystallise().

.clean

Should references to the node and edge indexes in the originalgraph be removed when usingconvert

Details

It is only possible to change and add to node and edge data from amorphed state. Any filtering/removal of nodes and edges will not result inremoval from the main graph. However, nodes and edges not present in themorphed state will be unaffected in the main graph when unmorphing (if newcolumns were added during the morhped state they will be filled withNA).

Morphing an already morhped graph will unmorph prior to applying the newmorph.

During a morphed state, the mapping back to the original graph is stored in.tidygraph_node_index and.tidygraph_edge_index columns. These areaccesible but protected, meaning that any changes to them with e.g. mutatewill be ignored. Furthermore, if the morph results in the merging of nodesand/or edges the original data is stored in a.data column. This isprotected as well.

When supplying your own morphers the morphing function should accept atbl_graph as its first input. The provided graph will already have nodesand edges mapped with a.tidygraph_node_index and.tidygraph_edge_indexcolumn. The return value must be atbl_graph or a list oftbl_graphs andthese must contain either a.tidygraph_node_index column or a.tidygraph_edge_index column (or both). Note that it is possible for themorph to have the edges mapped back to the original nodes and vice versa(e.g. as withto_linegraph). In that case the edge data in the morphedgraph(s) will contain a.tidygraph_node_index column and/or the node data a.tidygraph_edge_index column. If the morphing results in the collapse ofmultiple columns or edges the index columns should be converted to listcolumns mapping the new node/edge back to all the nodes/edges it represents.Furthermore the original node/edge data should be collapsed to a list oftibbles, with the row order matching the order in the index column element.

Value

Amorphed_tbl_graph

Examples

create_notable('meredith') %>%  mutate(group = group_infomap()) %>%  morph(to_contracted, group) %>%  mutate(group_centrality = centrality_pagerank()) %>%  unmorph()

Functions to generate alternate representations of graphs

Description

These functions are meant to be passed intomorph() to create a temporaryalternate representation of the input graph. They are thus not meant to becalled directly. See below for detail of each morpher.

Usage

to_linegraph(graph)to_subgraph(graph, ..., subset_by = NULL)to_subcomponent(graph, node)to_split(graph, ..., split_by = NULL)to_components(graph, type = "weak", min_order = 1)to_largest_component(graph, type = "weak")to_complement(graph, loops = FALSE)to_local_neighborhood(graph, node, order = 1, mode = "all")to_dominator_tree(graph, root, mode = "out")to_minimum_spanning_tree(graph, weights = NULL)to_random_spanning_tree(graph)to_shortest_path(graph, from, to, mode = "out", weights = NULL)to_bfs_tree(graph, root, mode = "out", unreachable = FALSE)to_dfs_tree(graph, root, mode = "out", unreachable = FALSE)to_simple(graph, remove_multiples = TRUE, remove_loops = TRUE)to_contracted(graph, ..., simplify = TRUE)to_unfolded_tree(graph, root, mode = "out")to_directed(graph)to_undirected(graph)to_hierarchical_clusters(graph, method = "walktrap", weights = NULL, ...)

Arguments

graph

Atbl_graph

...

Arguments to pass on tofilter(),group_by(), or the clusteralgorithm (seeigraph::cluster_walktrap(),igraph::cluster_leading_eigen(),andigraph::cluster_edge_betweenness())

subset_by,split_by

Whether to create subgraphs based on nodes or edges

node

The center of the neighborhood forto_local_neighborhood() andthe node to that should be included in the component forto_subcomponent()

type

The type of component to split into. Either'weak' or'strong'

min_order

The minimum order (number of vertices) of the component.Components below this will not be created

loops

Should loops be included. Defaults toFALSE

order

The radius of the neighborhood

mode

How should edges be followed?'out' only follows outboundedges,'in' only follows inbound edges, and'all' follows all edges. Thisparameter is ignored for undirected graphs.

root

The root of the tree

weights

Optional edge weights for the calculations

from,to

The start and end node of the path

unreachable

Should the search jump to a node in a new component whenstuck.

remove_multiples

Should edges that run between the same nodes bereduced to one

remove_loops

Should edges that start and end at the same node be removed

simplify

Should edges in the contracted graph be simplified? DefaultstoTRUE

method

The clustering method to use. Either'walktrap','leading_eigen', or'edge_betweenness'

Value

A list oftbl_graphs

Functions

Examples

# Compute only on a subgraph of every even nodecreate_notable('meredith') %>%  morph(to_subgraph, seq_len(graph_order()) %% 2 == 0) %>%  mutate(neighbour_count = centrality_degree()) %>%  unmorph()

Base implementation of mutate

Description

This implementation of mutate is slightly faster thanmutate at the expenseof the graph only being updated in the end. This means that graph algorithmswill not take changes happening during the mutate call into account.

Usage

mutate_as_tbl(.data, ...)

Arguments

.data

Atbl_graph object

...

columns to mutate

Details

The order of speed increase are rather small and in the ~1 millisecond permutateed column order, so for regular use this should not be a choice. Theoperations not supported bymutate_as_tbl are e.g.

gr %>%  activate(nodes) %>%  mutate(weights = runif(10), degree = centrality_degree(weights))

asweights will only be made available in the graph at the end of themutate call.

Value

Atbl_graph object


Querying node measures

Description

These functions are a collection of node measures that do not really fallinto the class ofcentrality measures. For lack of a better place they arecollected under the⁠node_*⁠ umbrella of functions.

Usage

node_eccentricity(mode = "out")node_constraint(weights = NULL)node_coreness(mode = "out")node_diversity(weights)node_efficiency(weights = NULL, directed = TRUE, mode = "all")node_bridging_score()node_effective_network_size()node_connectivity_impact()node_closeness_impact()node_fareness_impact()

Arguments

mode

How edges are treated. Innode_coreness() it chooses which kindof coreness measure to calculate. Innode_efficiency() it defines how thelocal neighborhood is created

weights

The weights to use for each node during calculation

directed

Should the graph be treated as a directed graph if it is infact directed

Value

A numeric vector of the same length as the number of nodes in thegraph.

Functions

Examples

# Calculate Burt's Constraint for each nodecreate_notable('meredith') %>%  mutate(b_constraint = node_constraint())

Calculate node ranking

Description

This set of functions tries to calculate a ranking of the nodes in a graph sothat nodes sharing certain topological traits are in proximity in theresulting order. These functions are of great value when composing matrixlayouts and arc diagrams but could concievably be used for other things aswell.

Usage

node_rank_hclust(  method = "average",  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_anneal(  cool = 0.5,  tmin = 1e-04,  swap_to_inversion = 0.5,  step_multiplier = 100,  reps = 1,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_branch_bound(  weighted_gradient = FALSE,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_traveller(  method = "two_opt",  ...,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_two(  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_mds(  method = "cmdscale",  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_leafsort(  method = "average",  type = "OLO",  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_visual(  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_spectral(  normalized = FALSE,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_spin_out(  step = 25,  nstart = 10,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_spin_in(  step = 5,  sigma = seq(20, 1, length.out = 10),  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_quadratic(  criterion = "2SUM",  reps = 1,  step = 2 * graph_order(),  step_multiplier = 1.1,  temp_multiplier = 0.5,  maxsteps = 50,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_genetic(  ...,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")node_rank_dendser(  ...,  dist = "shortest",  mode = "out",  weights = NULL,  algorithm = "automatic")

Arguments

method

The method to use. SeeFunctions section for reference

dist

The algorithm to use for deriving a distance matrix from thegraph. One of

  • "shortest" (default): Use the shortest path between all nodes

  • "euclidean": Calculate the L2 norm on the adjacency matrix of the graph

  • "manhattan": Calculate the L1 norm on the adjacency matrix of the graph

  • "maximum": Calculate the supremum norm on the adjacenecy matrix of the graph

  • "canberra": Calculate a weighted manhattan distance on the adjacency matrix of the graph

  • "binary": Calculate distance as the proportion of agreement between nodes based on the adjacency matrix of the graph

or a function that takes atbl_graph and return adist object with a sizematching the order of the graph.

mode

Which edges should be included in the distance calculation. Fordistance measures based on the adjacency matrix,'out' will use the matrixas is,'in' will use the transpose, and'all' will take the mean of thetwo. Defaults to'out'. Ignored for undirected graphs.

weights

An edge variable to use as weight for the shortest pathcalculation ifdist = 'shortest'

algorithm

The algorithm to use for the shortest path calculation ifdist = 'shortest'

cool

cooling rate

tmin

minimum temperature

swap_to_inversion

Proportion of swaps in local neighborhood search

step_multiplier

Multiplication factor for number of iterations per temperature

reps

Number of repeats with random initialisation

weighted_gradient

minimize the weighted gradient measure? Defaults toFALSE

...

Arguments passed on to other algorithms. SeeFunctions section for reference

type

The type of leaf reordering, either'GW' to use the "GW" method or'OLO' to use the "OLO" method (both inseriation)

normalized

Should the normalized laplacian of the similarity matrix be used?

step

The number iterations to run per initialisation

nstart

The number of random initialisations to perform

sigma

The variance around the diagonal to use for the weight matrix. Either a single number or a decreasing sequence.

criterion

The criterion to minimize. Either "LS" (Linear Seriation Problem), "2SUM" (2-Sum Problem), "BAR" (Banded Anti-Robinson form), or "Inertia" (Inertia criterion)

temp_multiplier

Temperature multiplication factor between 0 and 1

maxsteps

The upper bound of iterations

Value

An integer vector giving the position of each node in the ranking

Functions

Examples

graph <- create_notable('zachary') %>%  mutate(rank = node_rank_hclust())

Node properties related to the graph topology

Description

These functions calculate properties that are dependent on the overalltopology of the graph.

Usage

node_dominator(root, mode = "out")node_topo_order(mode = "out")

Arguments

root

The node to start the dominator search from

mode

How should edges be followed. Either'in' or'out'

Value

A vector of the same length as the number of nodes in the graph

Functions

Examples

# Sort a graph based on its topological ordercreate_tree(10, 2) %>%  arrange(sample(graph_order())) %>%  mutate(old_ind = seq_len(graph_order())) %>%  arrange(node_topo_order())

Querying node types

Description

These functions all lets the user query whether each node is of a certaintype. All of the functions returns a logical vector indicating whether thenode is of the type in question. Do note that the types are not mutuallyexclusive and that nodes can thus be of multiple types.

Usage

node_is_cut()node_is_root()node_is_leaf()node_is_sink()node_is_source()node_is_isolated()node_is_universal(mode = "out")node_is_simplical(mode = "out")node_is_center(mode = "out")node_is_adjacent(to, mode = "all", include_to = TRUE)node_is_keyplayer(k, p = 0, tol = 1e-04, maxsec = 120, roundsec = 30)node_is_connected(nodes, mode = "all", any = FALSE)

Arguments

mode

The way edges should be followed in the case of directed graphs.

to

The nodes to test for adjacency to

include_to

Should the nodes into be marked as adjacent as well

k

The number of keyplayers to identify

p

The probability to accept a lesser state

tol

Optimisation tolerance, below which the optimisation will stop

maxsec

The total computation budget for the optimization, in seconds

roundsec

Number of seconds in between synchronizing workers' answer

nodes

The set of nodes to test connectivity to. Can be a list to usedifferent sets for different nodes. If a list it will be recycled asnecessary.

any

Logical. IfTRUE the node only needs to be connected to a singlenode in the set for it to returnTRUE

Value

A logical vector of the same length as the number of nodes in thegraph.

Functions

Examples

# Find the root and leafs in a treecreate_tree(40, 2) %>%  mutate(root = node_is_root(), leaf = node_is_leaf())

Calculate node pair properties

Description

This set of functions can be used for calculations that involve node pairs.If the calculateable measure is not symmetric the function will come in twoflavours, differentiated with⁠_to⁠/⁠_from⁠ suffix. The⁠*_to()⁠ functionswill take the provided node indexes as the target node (recycling ifnecessary). For the⁠*_from()⁠ functions the provided nodes are taken asthe source. As for the other wrappers provided, they are intendedfor use inside thetidygraph framework and it is thus not necessary tosupply the graph being computed on as the context is known.

Usage

node_adhesion_to(nodes)node_adhesion_from(nodes)node_cohesion_to(nodes)node_cohesion_from(nodes)node_distance_to(nodes, mode = "out", weights = NULL, algorithm = "automatic")node_distance_from(  nodes,  mode = "out",  weights = NULL,  algorithm = "automatic")node_cocitation_with(nodes)node_bibcoupling_with(nodes)node_similarity_with(nodes, mode = "out", loops = FALSE, method = "jaccard")node_max_flow_to(nodes, capacity = NULL)node_max_flow_from(nodes, capacity = NULL)

Arguments

nodes

The other part of the node pair (the first part is the nodedefined by the row). Recycled if necessary.

mode

How should edges be followed? If'all' all edges areconsidered, if'in' only inbound edges are considered, and if'out' onlyoutbound edges are considered

weights

The weights to use for calculation

algorithm

The distance algorithms to use. By default it will try toselect the fastest suitable algorithm. Possible values are"automatic","unweighted","dijkstra","bellman-ford", and"johnson"

loops

Should loop edges be considered

method

The similarity measure to calculate. Possible values are:"jaccard","dice", and"invlogweighted"

capacity

The edge capacity to use

Value

A numeric vector of the same length as the number of nodes in thegraph

Functions

Examples

# Calculate the distance to the center nodecreate_notable('meredith') %>%  mutate(dist_to_center = node_distance_to(node_is_center()))

Perform a random walk on the graph and return encounter rank

Description

A random walk is a traversal of the graph starting from a node and going anumber of steps by picking an edge at random (potentially weighted).random_walk() can be called both when nodes and edges are active and willadapt to return a value fitting to the currently active part. As thewalk order cannot be directly encoded in the graph the return value is a listgiving a vector of positions along the walk of each node or edge.

Usage

random_walk_rank(n, root = NULL, mode = "out", weights = NULL)

Arguments

n

The number of steps to perform. If the walk gets stuck beforereaching this number the walk is terminated

root

The node to start the walk at. IfNULL a random node will beused

mode

How edges are followed in the search if the graph is directed."out" only follows outbound edges,"in" only follows inbound edges, and"all" or"total" follows all edges. This is ignored for undirectedgraphs.

weights

The weights to use for edges when selecting the next step ofthe walk. Currently only used when edges are active

Value

A list with an integer vector for each node or edge (depending onwhat is active) each element encode the time the node/edge is encounteredalong the walk

Examples

graph <- create_notable("zachary")# Random walk returning node ordergraph |>  mutate(walk_rank = random_walk_rank(200))# Rank edges insteadgraph |>  activate(edges) |>  mutate(walk_rank = random_walk_rank(200))

Objects exported from other packages

Description

These objects are imported from other packages. Follow the linksbelow to see their documentation.

dplyr

anti_join,arrange,contains,distinct,ends_with,everything,filter,full_join,group_by,group_data,group_indices,group_keys,group_size,group_vars,groups,inner_join,left_join,matches,mutate,mutate_all,mutate_at,n,n_groups,num_range,one_of,pull,rename,right_join,sample_frac,sample_n,select,semi_join,slice,slice_head,slice_max,slice_min,slice_sample,slice_tail,starts_with,tbl_vars,top_n,transmute,ungroup

igraph

as.igraph

magrittr

%>%

tibble

as_tibble

tidyr

drop_na,replace_na


Change terminal nodes of edges

Description

The reroute verb lets you change the beginning and end node of edges byspecifying the new indexes of the start and/or end node(s). Optionally onlya subset of the edges can be rerouted using the subset argument, which shouldbe an expression that are to be evaluated in the context of the edge data andshould return an index compliant vector (either logical or integer).

Usage

reroute(.data, from = NULL, to = NULL, subset = NULL)

Arguments

.data

A tbl_graph or morphed_tbl_graph object. grouped_tbl_graph willbe ungrouped prior to rerouting

from,to

The new indexes of the terminal nodes. IfNULL nothing willbe changed

subset

An expression evaluating to an indexing vector in the contextof the edge data. IfNULL it will use focused edges if available or alledges

Value

An object of the same class as .data

Examples

# Switch direction of edgescreate_notable('meredith') %>%  activate(edges) %>%  reroute(from = to, to = from)# Using subsetcreate_notable('meredith') %>%  activate(edges) %>%  reroute(from = 1, subset = to > 10)

Graph games based on direct sampling

Description

This set of graph games creates graphs directly through sampling of differentattributes, topologies, etc. The nature of their algorithm is described indetail at the linked igraph documentation.

Usage

play_degree(out_degree, in_degree = NULL, method = "simple")play_dotprod(position, directed = TRUE)play_fitness(m, out_fit, in_fit = NULL, loops = FALSE, multiple = FALSE)play_fitness_power(  n,  m,  out_exp,  in_exp = -1,  loops = FALSE,  multiple = FALSE,  correct = TRUE)play_gnm(n, m, directed = TRUE, loops = FALSE)play_gnp(n, p, directed = TRUE, loops = FALSE)play_geometry(n, radius, torus = FALSE)play_erdos_renyi(n, p, m, directed = TRUE, loops = FALSE)

Arguments

out_degree,in_degree

The degrees of each node in the graph

method

The algorithm to use for the generation. Either'simple','vl', or'simple.no.multiple'

position

The latent position of each node by column.

directed

Should the resulting graph be directed

m

The number of edges in the graph

out_fit,in_fit

The fitness of each node

loops

Are loop edges allowed

multiple

Are multiple edges allowed

n

The number of nodes in the graph.

out_exp,in_exp

Power law exponent of degree distribution

correct

Use finite size correction

p

The probabilty of an edge occuring

radius

The radius within which vertices are connected

torus

Should the vertices be distributed on a torus instead of a plane

Value

A tbl_graph object

Functions

See Also

Other graph games:component_games,evolution_games,type_games

Examples

plot(play_erdos_renyi(20, 0.3))

Search a graph with depth first and breath first

Description

These functions wraps theigraph::bfs() andigraph::dfs() functions toprovide a consistent return value that can be used indplyr::mutate()calls. Each function returns an integer vector with values matching the orderof the nodes in the graph.

Usage

bfs_rank(root, mode = "out", unreachable = FALSE)bfs_parent(root, mode = "out", unreachable = FALSE)bfs_before(root, mode = "out", unreachable = FALSE)bfs_after(root, mode = "out", unreachable = FALSE)bfs_dist(root, mode = "out", unreachable = FALSE)dfs_rank(root, mode = "out", unreachable = FALSE)dfs_rank_out(root, mode = "out", unreachable = FALSE)dfs_parent(root, mode = "out", unreachable = FALSE)dfs_dist(root, mode = "out", unreachable = FALSE)

Arguments

root

The node to start the search from

mode

How edges are followed in the search if the graph is directed."out" only follows outbound edges,"in" only follows inbound edges, and"all" or"total" follows all edges. This is ignored for undirectedgraphs.

unreachable

Should the search jump to a new component if the search isterminated without all nodes being visited? Default toFALSE (only reachconnected nodes).

Value

An integer vector, the nature of which is determined by the function.

Functions

Examples

# Get the depth of each node in a treecreate_tree(10, 2) %>%  activate(nodes) %>%  mutate(depth = bfs_dist(root = 1))# Reorder nodes based on a depth first search from node 3create_notable('franklin') %>%  activate(nodes) %>%  mutate(order = dfs_rank(root = 3)) %>%  arrange(order)

Graph games based on different node types

Description

This set of games are build around different types of nodes and simulatingtheir interaction. The nature of their algorithm is described indetail at the linked igraph documentation.

Usage

play_preference(  n,  n_types,  p_type = rep(1, n_types),  p_pref = matrix(1, n_types, n_types),  fixed = FALSE,  directed = TRUE,  loops = FALSE)play_preference_asym(  n,  n_types,  p_type = matrix(1, n_types, n_types),  p_pref = matrix(1, n_types, n_types),  loops = FALSE)play_bipartite(n1, n2, p, m, directed = TRUE, mode = "out")play_traits(  n,  n_types,  growth = 1,  p_type = rep(1, n_types),  p_pref = matrix(1, n_types, n_types),  callaway = TRUE,  directed = TRUE)play_citation_type(  n,  growth,  types = rep(0, n),  p_pref = rep(1, length(unique(types))),  directed = TRUE)

Arguments

n,n1,n2

The number of nodes in the graph. For bipartite graphsn1andn2 specifies the number of nodes of each type.

n_types

The number of different node types in the graph

p_type

The probability that a node will be the given type. Either avector or a matrix, depending on the game

p_pref

The probability that an edge will be made to a type. Either avector or a matrix, depending on the game

fixed

Should n_types be understood as a fixed number of nodes for eachtype rather than as a probability

directed

Should the resulting graph be directed

loops

Are loop edges allowed

p

The probabilty of an edge occuring

m

The number of edges in the graph

mode

The flow direction of edges

growth

The number of edges added at each iteration

callaway

Use the callaway version of the trait based game

types

The type of each node in the graph, enumerated from 0

Value

A tbl_graph object

Functions

See Also

Other graph games:component_games,evolution_games,sampling_games

Examples

plot(play_bipartite(20, 30, 0.4))

Evaluate a tidygraph algorithm in the context of a graph

Description

All tidygraph algorithms are meant to be called inside tidygraph verbs suchasmutate(), where the graph that is currently being worked on is known andthus not needed as an argument to the function. In the off chance that youwant to use an algorithm outside of the tidygraph framework you can usewith_graph() to set the graph context temporarily while the algorithm isbeing evaluated.

Usage

with_graph(graph, expr)

Arguments

graph

Thetbl_graph to use as context

expr

The expression to evaluate

Value

The value ofexpr

Examples

gr <- play_gnp(10, 0.3)with_graph(gr, centrality_degree())

[8]ページ先頭

©2009-2025 Movatter.jp