Learn R Programming

⚠️There's a newer version (1.0.11) of this package.Take me there.

With the DiagrammeR package you can create, modify, analyze, and visualize network graph diagrams. You can either use Markdown-like text to describe and render a diagram, or, use a collection of functions to create graph objects. The output can be viewed in the RStudio Viewer, incorporated in R Markdown, or integrated in shiny web apps.

It's possible to make the above graph diagram using using Graphviz DOT code (as text within the DiagrammeR grViz() function) or through a combination of DiagrammeR functions strung together with the magrittr %>% pipe.

So, with Graphviz:

library(DiagrammeR)

grViz("
digraph DAG {
      
  # Intialization of graph attributes
  graph [overlap = true]
      
  # Initialization of node attributes
  node [shape = box,
        fontname = Helvetica,
        color = blue,
        type = box,
        fixedsize = true]
      
  # Initialization of edge attributes
  edge [color = green,
        rel = yields]
      
  # Node statements
  1; 2; 3; 4; 8; 9; 10; 11
      
  # Revision to node attributes
  node [shape = circle]
      
  # Node statements
  5; 6; 7
      
  # Edge statements
  1->5; 2->6; 3->9; 4->7; 5->8; 5->10; 7->11

  # Revision to edge attributes
  edge [color = red]

  # Edge statements
  1->8; 3->6; 3->11; 3->7; 5->9; 6->10
}
")

With magrittr and DiagrammeR's graph functions:

library(DiagrammeR)
library(magrittr)

graph <-
  create_graph() %>%
  set_graph_name("DAG") %>%
  set_global_graph_attrs("graph", "overlap", "true") %>%
  set_global_graph_attrs("graph", "fixedsize", "true") %>%
  set_global_graph_attrs("node", "color", "blue") %>%
  set_global_graph_attrs("node", "fontname", "Helvetica") %>%
  add_n_nodes(11) %>%
  select_nodes_by_id(1:4) %>% 
  set_node_attrs_ws("shape", "box") %>%
  set_node_attrs_ws("type", "box") %>%
  clear_selection %>%
  select_nodes_by_id(5:7) %>% 
  set_node_attrs_ws("shape", "circle") %>%
  set_node_attrs_ws("type", "circle") %>%
  clear_selection %>%
  select_nodes_by_id(8:11) %>% 
  set_node_attrs_ws("shape", "box") %>%
  set_node_attrs_ws("type", "box") %>%
  clear_selection %>%
  add_edge(1, 5) %>% 
  add_edge(2, 6) %>%
  add_edge(3, 9) %>% 
  add_edge(4, 7) %>%
  add_edge(5, 8) %>% 
  add_edge(5, 10) %>%
  add_edge(7, 11) %>% 
  select_edges %>%
  set_edge_attrs_ws("color", "green") %>%
  add_edge(1, 8) %>% 
  add_edge(3, 6) %>%
  add_edge(3, 11) %>% 
  add_edge(3, 7) %>%
  add_edge(5, 9) %>% 
  add_edge(6, 10) %>%
  select_edges("color", "^$") %>%
  set_edge_attrs_ws("color", "red") %>%
  clear_selection

render_graph(graph)

The graph functions allow you create graph objects, render those graphs, modify those graphs, get information from the graphs, create a series of graphs, perform scaling of attribute values with data values, and do other useful things.

This functionality makes it possible to generate a network graph with data available in tabular datasets. The general idea is to build specialized data frames that contain either node data and attributes (node data frames) and those data frames that contain edge data and edge attributes (edge data frames). These data frames are permitted to have node and edge attributes and also columns of other data. Because the attributes are always kept alongside the node and edge definitions (within the graph object itself), we can easily work with them and modify the values of the styling attributes and differentiate nodes and edges by size, color, shape, opacity, length, etc. Here is a listing of the available graph functions:

Network Graph Example

Let's create a network graph by combining CSV data that pertains to contributors to three software projects. The CSV files (contributors.csv, projects.csv, and projects_and_contributors.csv) are available in the DiagrammeR package. Together they provide the properties name, age, join_date, email, follower_count, following_count, and starred_count to the person nodes; project, start_date, stars, and language to the project nodes; and the contributor_role and commits properties to the edges.

library(DiagrammeR)
library(magrittr)

graph <-
  create_graph() %>%
  set_graph_name("software_projects") %>%
  set_global_graph_attrs(
    "graph", "output", "visNetwork") %>%
  add_nodes_from_table(
    system.file(
      "examples/contributors.csv",
      package = "DiagrammeR"),
    set_type = "person",
    label_col = "name") %>%
  add_nodes_from_table(
    system.file(
      "examples/projects.csv",
      package = "DiagrammeR"),
    set_type = "project",
    label_col = "project") %>%
  add_edges_from_table(
    system.file(
      "examples/projects_and_contributors.csv",
      package = "DiagrammeR"),
    from_col = "contributor_name",
    from_mapping = "name",
    to_col = "project_name",
    to_mapping = "project",
    rel_col = "contributor_role")

View the property graph.

graph %>% render_graph

Now that the graph is set up, you can construct queries with magrittr pipelines to get specific answers from the graph.

Get the average age of all the contributors:

graph %>% 
  select_nodes("type", "person") %>%
  cache_node_attrs_ws("age", "numeric") %>%
  get_cache %>% 
  mean
#> [1] 33.6

Get the total number of commits to all software projects:

graph %>% 
  select_edges %>%
  cache_edge_attrs_ws("commits", "numeric") %>%
  get_cache %>% 
  sum
#> [1] 5182

Get the total number of commits from Josh as a maintainer and as a contributor:

graph %>% 
  select_nodes("name", "Josh") %>%
  trav_out_edge %>%
  cache_edge_attrs_ws("commits", "numeric") %>%
  get_cache %>% 
  sum
#> [1] 227

Get the total number of commits from Louisa:

graph %>% 
  select_nodes("name", "Louisa") %>%
  trav_out_edge %>%
  cache_edge_attrs_ws("commits", "numeric") %>%
  get_cache %>% 
  sum
#> [1] 615

Get the names of people in graph above age 32:

graph %>% 
  select_nodes("type", "person") %>%
  select_nodes("age", ">32", "intersect") %>%
  cache_node_attrs_ws("name") %>%
  get_cache %>%
  sort
#> [1] "Jack"   "Jon"    "Kim"    "Roger"  "Sheryl"

Get the total number of commits from all people to the supercalc project:

graph %>% 
  select_nodes("project", "supercalc") %>%
  trav_in_edge %>%
  cache_edge_attrs_ws("commits", "numeric") %>%
  get_cache %>% 
  sum
#> [1] 1676

Who committed the most to the supercalc project?

graph %>% 
  select_nodes("project", "supercalc") %>%
  trav_in_edge %>%
  cache_edge_attrs_ws("commits", "numeric") %>%
  trav_in_node %>%
  trav_in_edge("commits", max(get_cache(.))) %>%
  trav_out_node %>%
  cache_node_attrs_ws("name") %>%
  get_cache
#> [1] "Sheryl"

What is the email address of the individual that contributed the least to the randomizer project?

graph %>% 
  select_nodes("project", "randomizer") %>%
  trav_in_edge %>%
  cache_edge_attrs_ws("commits", "numeric") %>%
  trav_in_node %>%
  trav_in_edge("commits", min(get_cache(.))) %>%
  trav_out_node %>%
  cache_node_attrs_ws("email") %>%
  get_cache
#> [1] "the_will@graphymail.com"

Kim is now a contributor to the stringbuildeR project and has made 15 new commits to that project. Modify the graph to reflect this and view the updated graph:

graph %<>%
  add_edge(
    get_nodes(.,
      "name", "Kim"),
    get_nodes(.,
      "project", "stringbuildeR"),
    "contributor") %>%
  select_last_edge %>%
  set_edge_attrs_ws("commits", 15) %>%
  clear_selection

graph %>% render_graph

Get all email addresses to contributors (but not maintainers) of the randomizer and supercalc88 projects:

graph %>% 
  select_nodes("project", "randomizer") %>%
  select_nodes("project", "supercalc") %>%
  trav_in_edge("rel", "contributor") %>%
  trav_out_node %>%
  cache_node_attrs_ws("email", "character") %>%
  get_cache %>% 
  sort
#> [1] "j_2000@ultramail.io"      "josh_ch@megamail.kn"     
#> [3] "kim_3251323@ohhh.ai"      "lhe99@mailing-fun.com"   
#> [5] "roger_that@whalemail.net" "the_simone@a-q-w-o.net"  
#> [7] "the_will@graphymail.com" 

Which committer to the randomizer project has the highest number of followers?

graph %>% 
  select_nodes("project", "randomizer") %>%
  trav_in %>%
  cache_node_attrs_ws("follower_count", "numeric") %>%
  select_nodes("project", "randomizer") %>%
  trav_in("follower_count", max(get_cache(.))) %>%
  cache_node_attrs_ws("name") %>%
  get_cache
#> [1] "Kim"

Which people have committed to more than one project?

graph %>%
  select_nodes_by_degree("out", ">1") %>%
  cache_node_attrs_ws("name") %>%
  get_cache %>% 
  sort
#> [1] "Josh"  "Kim"  "Louisa"

Installation

DiagrammeR is used in an R environment. If you don't have an R installation, it can be obtained from the Comprehensive R Archive Network (CRAN). It is recommended that RStudio be used as the R IDE to take advantage of its rendering capabilities and the code-coloring support for Graphviz and mermaid diagrams.

You can install the development version of DiagrammeR from GitHub using the devtools package.

devtools::install_github('rich-iannone/DiagrammeR')

Or, get the v0.8.2 release from CRAN.

install.packages('DiagrammeR')

Copy Link

Version

Install

install.packages('DiagrammeR')

Monthly Downloads

36,092

Version

0.8.4

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Richard Iannone

Last Published

July 17th, 2016

Functions in DiagrammeR (0.8.4)

add_node_df

Add nodes from a node data frame to an existing graph object
add_balanced_tree

Add a balanced tree of nodes to the graph
add_cycle

Add a cycle of nodes to the graph
add_n_nodes

Add one or several unconnected nodes to the graph
add_edges_from_table

Add edges and attributes to graph from a table
add_edge_df

Add edges from an edge data frame to an existing graph object
add_node

Add a node to an existing graph object
add_n_nodes_ws

Add a multiple of new nodes with edges to or from one or more selected nodes
add_edge

Add an edge between nodes in a graph object
add_edges_w_string

Add one or more edges using a text string
cache_edge_count_ws

Cache a count of edges (available in a selection) in the graph
cache_node_attrs

Cache node attributes in the graph
add_prism

Add a prism of nodes to the graph
add_star

Add a star of nodes to the graph
cache_node_attrs_ws

Cache node attributes (based on a selection of nodes) in the graph
add_path

Add a path of nodes to the graph
cache_edge_attrs_ws

Cache edge attributes (based on a selection of edges) in the graph
add_to_series

Add graph object to a graph series object
add_nodes_from_table

Add nodes and attributes to graph from a table
cache_edge_attrs

Cache edge attributes in the graph
cache_node_count_ws

Cache a count of nodes (available in a selection) in the graph
combine_nodes

Combine multiple node data frames into a single node data frame
combine_edges

Combine multiple edge data frames into a single edge data frame
create_random_graph

Create a randomized graph
combine_graphs

Combine two graphs into a single graph
country_graph

Create a graph object that contains the boundaries of a country
create_nodes

Create a data frame with nodes and their attributes
create_graph

Create a graph object
create_edges

Create a data frame with edges and their attributes
clear_selection

Clear a selection of nodes or edges in a graph
do_dfs

Perform a depth-first search
edge_count

Get count of all edges or edges with distinct relationship types
create_series

Create a graph series object
get_betweenness

Get betweenness centrality scores
DiagrammeR

R + mermaid.js
create_subgraph_ws

Create a subgraph based on a selection of nodes or edges
get_articulation_points

Get articulation points
get_node_df

Get a node data frame from a graph
DiagrammeROutput

Widget output function for use in Shiny
get_nodes

Get a vector of node ID values
delete_node

Delete a node from an existing graph object
export_graph

Export a graph to various file formats
delete_nodes_ws

Delete all selected nodes in a node selection
get_global_graph_attrs

Get global graph attributes
get_all_connected_nodes

Get all nodes connected to a specified node
get_edges

Get node IDs associated with edges
get_graph_diameter

Get the graph diameter
grVizOutput

Widget output function for use in Shiny
get_graph_from_series

Get a graph available in a series
grViz

R + viz.js
get_connected_components

Get all nodes associated with connected components
get_constraint

Get constraint scores for one or more graph nodes
get_paths

Get paths from a specified node in a directed graph
join_edge_attrs

Join new edge attribute values using a data frame
get_non_nbrs

Get non-neighbors of a node in a graph
delete_edge

Delete an edge from an existing graph object
join_node_attrs

Join new node attribute values using a data frame
delete_edges_ws

Delete all selected edges in an edge selection
export_csv

Export a graph to CSV files
edge_rel

Create, read, update, delete, or report status of an edge relationship
get_closeness

Get closeness centrality values
get_common_nbrs

Get all common neighbors between two or more nodes
get_similar_nbrs

Get neighboring nodes based on node attribute similarity
get_graph_name

Get graph name
get_graph_time

Get the graph date-time or timezone
get_successors

Get node IDs for successor nodes to the specified node
image_icon

Icons and their download locations
import_graph

Import a graph from various graph formats
node_type

Create, read, update, delete, or report status of a node type definition
remove_from_series

Remove a graph from a graph series
rescale_node_attrs_ws

Rescale numeric node attribute values for nodes in a selection
reverse_edge_direction

Reverse the graph's edge direction
get_cache

Get a cached vector from a graph object
trav_both

Traverse from one or more selected nodes to predecessors and successors, irrespective of edges, creating a new node selection
get_bridging

Get bridging scores
to_igraph

Convert a DiagrammeR graph to an igraph one
get_edge_df

Get an edge data frame from a graph
get_edge_attrs

Get edge attribute values
get_node_attrs

Get node attribute values
get_nbrs

Get all neighbors of one or more nodes
get_periphery

Get nodes that form the graph periphery
get_predecessors

Get node IDs for predecessor nodes to the specified node
node_count

Get count of all nodes or certain types of nodes
mermaid

R + mermaid.js
render_graph

Render the graph or output in various formats
render_graph_from_series

Render a graph available in a series
trav_in_edge

Traverse from one or more selected nodes onto adjacent, inward edges
set_edge_attrs_ws

Set edge attributes with an edge selection
series_info

Get information on a graph series
trav_in_node

Traverse from one or more selected edges toward adjacent inward nodes
graph_count

Count graphs in a graph series object
invert_selection

Invert selection of nodes or edges in a graph
graph_info

Get metrics for a graph
is_graph_connected

Is the graph a connected graph?
node_info

Get detailed information on nodes
node_present

Determine whether a specified node is present in an existing graph object
set_graph_undirected

Convert graph to an undirected graph
select_edges

Select edges in a graph
select_edges_by_node_id

Select edges in a graph using node ID values
set_node_attrs_ws

Set node attributes with a node selection
trigger_script

Trigger a script embedded in a graph series object
visnetwork

Render graph with visNetwork
set_graph_name

Set graph name
select_last_edge

Select last edge in a series of edges defined in a graph
select_last_node

Select last node in a series of node IDs in a graph
set_graph_time

Set graph date-time and timezone
renderDiagrammeR

Widget render function for use in Shiny
renderGrViz

Widget render function for use in Shiny
select_nodes_in_neighborhood

Select nodes based on a walk distance from a specified node
select_nodes

Select nodes in a graph
subset_series

Subset a graph series object
set_node_attrs

Set node attributes
trav_in

Traverse inward to a selected node, skipping over edges, and creating a new node selection
vivagraph

Render graph with VivaGraphJS
trav_out_edge

Traverse from one or more selected nodes onto adjacent, outward edges
x11_hex

X11 colors and hexadecimal color values
edge_info

Get detailed information on edges
get_degree_histogram

Get histogram data for a graph's degree frequency
edge_present

Determine whether a specified edge is present in an existing graph object
get_eccentricity

Get node eccentricities
get_s_connected_components

Get nodes within strongly connected components
get_selection

Get the current selection available in a graph object
is_graph_empty

Is the graph empty?
rescale_edge_attrs_ws

Rescale numeric edge attribute values for edges in a selection
replace_in_spec

Razor-like template for diagram specification
is_graph_directed

Is the graph a directed graph?
select_nodes_by_degree

Select nodes in the graph based on their degree values
set_edge_attrs

Set edge attributes
select_nodes_by_id

Select nodes in a graph by ID values
set_global_graph_attrs

Set global graph attributes
trav_out_node

Traverse from one or more selected edges toward adjacent outward nodes
trav_out

Traverse outward from a selected node, skipping over edges, and creating a new node selection