tidygraph R Package Documentation

repository·main·Indexed 20 days ago

https://github.com/thomasp85/tidygraph

An R package for tidy manipulation of graphs using tidyverse principles. It provides a dplyr-compatible interface for igraph functionality, treating graphs as relational tables for nodes and edges. Key features include the activate() function for context switching, graph expansion verbs like bind_edges() and bind_nodes(), and the ability to convert relational data into tbl_graph objects via as_tbl_graph().

Tokens
869
Snippets
3
Records
8
Agent score
19%

What's inside tidygraph

  1. How tidygraph and dplyr work together

    main

    tidygraph provides a tidy API for graph/network manipulation by treating a graph as two relational tables: one for node data and one for edge data.

    It wraps igraph functionality into a dplyr-compatible interface. You use the activate() function to switch the context of your verbs between nodes and edges. Once activated, dplyr verbs like mutate() can be used to add properties to nodes or edges using graph algorithms that are context-aware.

    library(tidygraph)
    
    play_gnp(10, 0.5) %>%
      activate(nodes) %>%
      mutate(degree = centrality_degree()) %>%
      activate(edges) %>%
      mutate(centrality = centrality_edge_betweenness()) %>%
      arrange(centrality)
  2. Transform graphs using morph() and unmorph()

    main

    The morph() and unmorph() verbs allow you to temporarily change the representation of a graph (e.g., contracting nodes or working on a linegraph), perform manipulations on that representation, and then automatically merge those changes back into the original graph structure.

    If you want to keep the transformed representation as a permanent tbl_graph instead of merging it back, use the crystallise() verb.

  3. Use graph algorithms in a tidy workflow

    main

    tidygraph wraps igraph algorithms into functions that are designed to be used inside dplyr verbs. These functions are context-aware, meaning they automatically detect the graph and the relevant node/edge IDs from the current tidygraph object, resulting in cleaner code.

    Example usage:

    # Inside a mutate call, the algorithm knows which nodes/edges to operate on
    graph %>% 
      activate(nodes) %>% 
      mutate(deg = centrality_degree())
  4. Install tidygraph

    main

    You can install the stable version of tidygraph from CRAN using install.packages(). For the development version from GitHub, it is recommended to use the pak package.

    To install the development version:

    pak::pak('thomasp85/tidygraph')
    pak::pak('thomasp85/tidygraph')
  5. Use activate() to switch between nodes and edges

    main

    The activate() function is used to define whether subsequent operations are performed on node data or edge data. This is a prerequisite for using dplyr verbs like mutate() or filter() on specific parts of the graph.

    Common arguments for activate():

    • nodes
    • edges
    # Switch to node context
    graph %>% activate(nodes) %>% mutate(new_col = ...)
    
    # Switch to edge context
    graph %>% activate(edges) %>% mutate(new_col = ...)
  6. Expand and merge graphs with tidy verbs

    main

    tidygraph provides several verbs to modify the structure of a graph beyond simple attribute manipulation:

    • bind_edges(): Expand the graph structure by adding edges.
    • bind_nodes(): Expand the graph structure by adding nodes.
    • bind_graphs(): Combine multiple graphs.
    • graph_join(): Merge two graphs based on a shared node identifier.
    • reroute(): Change the terminal nodes of the edges in the graph.
  7. Convert relational data to tbl_graph with as_tbl_graph()

    main

    The as_tbl_graph() function converts various relational and network objects into a tidygraph object (tbl_graph). Supported types include:

    • network
    • phylo
    • dendrogram
    • data.tree
    • graph (igraph objects)