Learn R Programming

tidygraph

This package provides a tidy API for graph/network manipulation. While network data itself is not tidy, it can be envisioned as two tidy tables, one for node data and one for edge data. tidygraph provides a way to switch between the two tables and provides dplyr verbs for manipulating them. Furthermore it provides access to a lot of graph algorithms with return values that facilitate their use in a tidy workflow.

An example

library(tidygraph)

play_gnp(10, 0.5) %>% 
  activate(nodes) %>% 
  mutate(degree = centrality_degree()) %>% 
  activate(edges) %>% 
  mutate(centrality = centrality_edge_betweenness()) %>% 
  arrange(centrality)
#> # A tbl_graph: 10 nodes and 51 edges
#> #
#> # A directed simple graph with 1 component
#> #
#> # Edge Data: 51 × 3 (active)
#>     from    to centrality
#>    <int> <int>      <dbl>
#>  1     2     7       1.25
#>  2     6     5       1.33
#>  3     1     3       1.4 
#>  4     2    10       1.53
#>  5     2     8       1.58
#>  6     8     9       1.65
#>  7     2     3       1.67
#>  8     2     5       1.73
#>  9     3     5       1.73
#> 10     8     5       1.73
#> # ℹ 41 more rows
#> #
#> # Node Data: 10 × 1
#>   degree
#>    <dbl>
#> 1      6
#> 2      7
#> 3      6
#> # ℹ 7 more rows

Overview

tidygraph is a huge package that exports 280 different functions and methods. It more or less wraps the full functionality of igraph in a tidy API giving you access to almost all of the dplyr verbs plus a few more, developed for use with relational data.

More verbs

tidygraph adds some extra verbs for specific use in network analysis and manipulation. The activate() function defines whether one is manipulating node or edge data at the moment as shown in the example above. bind_edges(), bind_nodes(), and bind_graphs() let you expand the graph structure you’re working with, while graph_join() lets you merge two graphs on some node identifier. reroute(), on the other hand, lets you change the terminal nodes of the edges in the graph.

More algorithms

tidygraph wraps almost all of the graph algorithms from igraph and provides a consistent interface and output that always matches the sequence of nodes and edges. All tidygraph algorithm wrappers are intended for use inside verbs where they know the context they are being called in. In the example above it is not necessary to supply the graph nor the node/edge IDs to centrality_degree() and centrality_edge_betweenness() as they are aware of them already. This leads to much clearer code and less typing.

More maps

tidygraph goes beyond dplyr and also implements graph centric version of the purrr map functions. You can now call a function on the nodes in the order of a breadth or depth first search while getting access to the result of the previous calls.

More morphs

tidygraph lets you temporarily change the representation of your graph, do some manipulation of the node and edge data, and then change back to the original graph with the changes being merged in automatically. This is powered by the new morph()/unmorph() verbs that let you e.g. contract nodes, work on the linegraph representation, split communities to separate graphs etc. If you wish to continue with the morphed version, the crystallise() verb lets you freeze the temporary representation into a proper tbl_graph.

More data structure support

While tidygraph is powered by igraph underneath it wants everyone to join the fun. The as_tbl_graph() function can easily convert relational data from all your favourite objects, such as network, phylo, dendrogram, data.tree, graph, etc. More conversion will be added in the order I become aware of them.

Visualisation

tidygraph itself does not provide any means of visualisation, but it works flawlessly with ggraph. This division makes it easy to develop the visualisation and manipulation code at different speeds depending on where the needs arise.

Installation

tidygraph is available on CRAN and can be installed simply, using install.packages('tidygraph'). For the development version available on GitHub, use the devtools package for installation:

# install.packages('pak')
pak::pak('thomasp85/tidygraph')

Thanks

tidygraph stands on the shoulders of particularly the igraph and dplyr/tidyverse teams. It would not have happened without them, so thanks so much to them.

Code of Conduct

Please note that the tidygraph project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

Copy Link

Version

Install

install.packages('tidygraph')

Monthly Downloads

64,077

Version

1.3.1

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

January 30th, 2024

Functions in tidygraph (1.3.1)

graph-context

Register a graph context for the duration of the current frame
map_bfs

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

Graph measurements
iterate

Repeatedly modify a graph by a function
map_bfs_back

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

Querying graph types
graph_join

Join graphs on common nodes
local_graph

Measures based on the neighborhood of each node
fortify.tbl_graph

Fortify a tbl_graph for ggplot2 plotting
group_graph

Group nodes and edges based on community structure
map_dfs

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

Functions to generate alternate representations of graphs
node_topology

Node properties related to the graph topology
node_types

Querying node types
map_dfs_back

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

Calculate node ranking
mutate_as_tbl

Base implementation of mutate
node_measures

Querying node measures
map_local

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

Create a temporary alternative representation of the graph to compute on
as_tbl_graph.data.frame

A data structure for tidy graph manipulation
type_games

Graph games based on different node types
sampling_games

Graph games based on direct sampling
search_graph

Search a graph with depth first and breath first
reroute

Change terminal nodes of edges
pair_measures

Calculate node pair properties
tidygraph-package

tidygraph: A Tidy API for Graph Manipulation
with_graph

Evaluate a tidygraph algorithm in the context of a graph
random_walk_rank

Perform a random walk on the graph and return encounter rank
reexports

Objects exported from other packages
edge_rank

Calculate edge ranking
focus

Select specific nodes or edges to compute on
component_games

Graph games based on connected components
centrality

Calculate node and edge centrality
context_accessors

Access graph, nodes, and edges directly inside verbs
activate

Determine the context of subsequent manipulations
evolution_games

Graph games based on evolution
create_graphs

Create different types of well-defined graphs
bind_graphs

Add graphs, nodes, or edges to a tbl_graph
edge_types

Querying edge types