Learn R Programming

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

With the DiagrammeR package, you can create 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, it can be incorporated in R Markdown, and it can be integrated in shiny web apps. Because we are doing this in R we can and always should add much more R code into the mix.

Go to the project website and view a video walkthrough for a graph diagram that's created with a few lines of text and is just as easily customizable. After being all fired up from that intense video-tutorial extravaganza, have a look at the DiagrammeR Docs to learn more.

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_attr("graph", "overlap", "true") %>%
  set_global_graph_attr("graph", "fixedsize", "true") %>%
  set_global_graph_attr("node", "color", "blue") %>%
  set_global_graph_attr("node", "fontname", "Helvetica") %>%
  add_n_nodes(11) %>%
  select_nodes_by_id(1:4) %>% 
  set_node_attr_with_selection("shape", "box") %>%
  set_node_attr_with_selection("type", "box") %>%
  clear_selection %>%
  select_nodes_by_id(5:7) %>% 
  set_node_attr_with_selection("shape", "circle") %>%
  set_node_attr_with_selection("type", "circle") %>%
  clear_selection %>%
  select_nodes_by_id(8:11) %>% 
  set_node_attr_with_selection("shape", "box") %>%
  set_node_attr_with_selection("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_attr_with_selection("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_attr_with_selection("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:

Graph Example

Let's create a property 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_attr("graph",
                        "output",
                        "visNetwork") %>%
  add_nodes_from_csv(
    system.file("examples/contributors.csv",
                package = "DiagrammeR"),
    set_type = "person",
    label_col = "name") %>%
  add_nodes_from_csv(
    system.file("examples/projects.csv",
                package = "DiagrammeR"),
    set_type = "project",
    label_col = "project") %>%
  add_edges_from_csv(
    system.file("examples/projects_and_contributors.csv",
                package = "DiagrammeR"),
    from_col = "contributor_name",
    from_attr = "name",
    to_col = "project_name",
    to_attr = "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") %>%
  deposit_node_attr_from_selection("age",
                                   "numeric") %>%
  withdraw_values %>% mean
#> [1] 33.6

Get the total number of commits to all software projects:

graph %>% select_edges %>%
  deposit_edge_attr_from_selection("commits",
                                   "numeric") %>%
  withdraw_values %>% 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(c("maintainer", "contributer")) %>%
  deposit_edge_attr_from_selection("commits",
                                   "numeric") %>%
  withdraw_values %>% sum
#> [1] 227

Get the total number of commits from Louisa:

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

Get the names of people in graph above age 32:

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

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

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

Who committed the most to the supercalc project?

graph %>% select_nodes("project", "supercalc") %>%
  trav_in_edge %>%
  deposit_edge_attr_from_selection("commits", "numeric") %>%
  trav_in_node %>%
  trav_in_edge("commits", max(withdraw_values(.))) %>%
  trav_out_node %>%
  deposit_node_attr_from_selection("name") %>%
  withdraw_values
#> [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 %>%
  deposit_edge_attr_from_selection("commits", "numeric") %>%
  trav_in_node %>%
  trav_in_edge("commits", min(withdraw_values(.))) %>%
  trav_out_node %>%
  deposit_node_attr_from_selection("email") %>%
  withdraw_values
#> [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_attr_with_selection("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 %>%
  deposit_node_attr_from_selection("email", "character") %>%
  withdraw_values
#> [1] "lhe99@mailing-fun.com"  "josh_ch@megamail.kn"
#> [3] "roger_that@whalemail.net"  "the_simone@a-q-w-o.net"
#> [5] "kim_3251323@ohhh.ai"  "the_will@graphymail.com"
#> [7] "j_2000@ultramail.io"

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

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

Which people have committed to more than one project?

graph %>%
  select_nodes_by_degree("out", ">1",
                         "type", "person") %>%
  deposit_node_attr_from_selection("name") %>%
  withdraw_values
#> [1] "Louisa"  "Josh"  "Kim"

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.1 release from CRAN.

install.packages('DiagrammeR')

Copy Link

Version

Install

install.packages('DiagrammeR')

Monthly Downloads

34,997

Version

0.8.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Richard Iannone

Last Published

February 1st, 2016

Functions in DiagrammeR (0.8.2)

image_icon

Icons and their download locations
set_edge_attr

Set edge attributes
trigger_script

Trigger a script embedded in a graph series object
combine_edges

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

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

Add edges and attributes from a data frame
get_paths

Get paths from a specified node in a directed graph
deposit_edge_attr

Deposit edge attributes in the graph
render_graph_from_series

Render a graph available in a series
import_graph

Import a graph from various graph formats
add_nodes_from_csv

Add nodes and attributes from a CSV file
set_graph_name

Set graph name
get_predecessors

Get node IDs for predecessor nodes to the specified node
DiagrammeROutput

Widget output function for use in Shiny
set_node_attr

Set node attributes
select_edges

Select edges in a graph
series_info

Get detailed information on a graph series
grVizOutput

Widget output function for use in Shiny
deposit_edge_attr_from_selection

Deposit edge attributes (based on a selection of edges) in the graph
edge_info

Get detailed information on edges
create_series

Create a graph series object
add_n_nodes

Add a multiple of new nodes to the graph
add_edges_from_csv

Add edges and attributes from a CSV file
get_nodes

Get a vector of node ID values
deposit_node_attr

Deposit node attributes in the graph
edge_present

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

Add graph object to a graph series object
DiagrammeR

R + mermaid.js
edge_count

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

Get global graph attributes
get_successors

Get node IDs for successor nodes to the specified node
get_selection

Get the current selection available in a graph object
trav_both

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

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

Get detailed information on nodes
get_graph_from_series

Get a graph available in a series
delete_node

Delete a node from an existing graph object
create_graph

Create a graph object using data frames representative of nodes and edges
delete_edge

Delete an edge from an existing graph object
get_node_df

Get a node data frame from a graph
edge_rel

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

Get count of all nodes or certain types of nodes
remove_from_series

Remove graph object from a graph series object
renderDiagrammeR

Widget render function for use in Shiny
render_graph

Render the graph or output in various formats
rescale_edge_attr_in_selection

Rescale numeric edge attribute values for edges in a selection
select_nodes_by_degree

Get node degree values from nodes available in a selection
combine_nodes

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

Set edge attributes based on a selection of edges
trav_out

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

Create a data frame with edges and their attributes
node_type

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

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

Withdraw a stored vector from a graph object
add_n_nodes_to_selection

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

Render graph with VivaGraphJS
combine_graphs

Combine two graphs into a single graph
create_random_graph

Create a randomized graph
deposit_node_count_in_selection

Deposit a count of nodes (available in a selection) in the graph
add_node_df

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

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

Set global graph attributes
set_node_attr_with_selection

Set node attributes based on a selection of nodes
set_graph_time

Set graph date-time and timezone
is_graph_empty

Is the graph empty?
trav_out_edge

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

R + viz.js
x11_hex

X11 colors and hexadecimal color values
add_node

Add a node to an existing graph object
create_nodes

Create a data frame with nodes and their attributes
get_edges

Get node IDs associated with edges
clear_selection

Clear a selection of nodes or edges in a graph
is_graph_directed

Is the graph a directed graph?
renderGrViz

Widget render function for use in Shiny
trav_in_edge

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

Select nodes in a graph
get_edge_df

Get an edge data frame from a graph
subset_series

Subset a graph series object
delete_nodes_in_selection

Delete all selected nodes
replace_in_spec

Razor-like template for diagram specification
add_edge

Add an edge between nodes in a graph object
deposit_node_attr_from_selection

Deposit node attributes (based on a selection of nodes) in the graph
graph_count

Count graphs in a graph series object
invert_selection

Invert selection of nodes or edges in a graph
select_nodes_by_id

Select nodes in a graph by ID values
select_edges_by_node_id

Select edges in a graph using node ID values
deposit_edge_count_in_selection

Deposit a count of edges (available in a selection) in the graph
create_subgraph_from_selection

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

Delete all selected edges
visnetwork

Render graph with visNetwork
add_n_nodes_from_selection

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

Add nodes and attributes from a data frame
mermaid

R + mermaid.js
select_last_node

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

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

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

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

Rescale numeric node attribute values for nodes in a selection
select_nodes_in_neighborhood

Select nodes based on a walk distance from a specified node