4.15 Create diagrams

There are many separate programs (e.g., Graphviz) that can be used to produce diagrams and flowcharts, but it can be easier to manage them directly inside R code chunks in Rmd documents.

While there are several different packages available for R, we will only briefly introduce the package DiagrammeR (Iannone and Roy 2024), and mention other packages at the end. You can find the full documentation of DiagrammeR at https://rich-iannone.github.io/DiagrammeR/. In this section, we will introduce the basic usages and also how to use R code in diagrams.

4.15.1 Basic diagrams

DiagrammeR provides methods to build graphs for a number of different graphing languages. We will present a Graphviz example in this section,6 but you can also use pure R code to create graphs and diagrams with DiagrammeR.

The RStudio IDE provides native support for Graphviz (.gv) and mermaid (.mmd) files. Editing these types of files in RStudio has the advantage of syntax highlighting. RStudio also allows you to preview the diagrams by clicking the “Preview” button on the toolbar. Figure 4.4 is a simple flowchart example that has four rectangles representing four steps, generated by the code below:

DiagrammeR::grViz("digraph {
  graph [layout = dot, rankdir = TB]
  
  node [shape = rectangle]        
  rec1 [label = 'Step 1. Wake up']
  rec2 [label = 'Step 2. Write code']
  rec3 [label =  'Step 3. ???']
  rec4 [label = 'Step 4. PROFIT']
  
  # edge definitions with the node IDs
  rec1 -> rec2 -> rec3 -> rec4
  }",
  height = 500)

FIGURE 4.4: A diagram showing a programmer’s daydream.

There are extensive controls that can be used to define the shape of nodes, colors, line types, and add additional parameters.

4.15.2 Adding parameters to plots

Graphviz substitution allows for mixing R expressions into a Graphviz graph specification, without sacrificing readability. If you specify a substitution with @@, you must ensure there is a valid R expression for that substitution. The expressions are placed as footnotes and their evaluations must result in an R vector object. The @@ notation is immediately followed by a number, and that number should correspond to the number of the R expression footnote. Figure 4.5 shows an example of embedding and evaluating R code in the diagram.

DiagrammeR::grViz("
  digraph graph2 {
  
  graph [layout = dot, rankdir = LR]
  
  # node definitions with substituted label text
  node [shape = oval]
  a [label = '@@1']
  b [label = '@@2']
  c [label = '@@3']
  d [label = '@@4']
  
  a -> b -> c -> d
  }
  
  [1]: names(iris)[1]
  [2]: names(iris)[2]
  [3]: names(iris)[3]
  [4]: names(iris)[4]
  ",
  height = 100)

FIGURE 4.5: A diagram using parameters input from R.

4.15.3 Other packages for making diagrams

You may also check out these packages for creating diagrams: nomnoml (de Vries and Luraschi 2023), diagram (Soetaert 2020), dagitty (Textor, van der Zander, and Ankan 2023), ggdag (Barrett 2024), and plantuml (https://github.com/rkrug/plantuml).

References

Barrett, Malcolm. 2024. Ggdag: Analyze and Create Elegant Directed Acyclic Graphs. https://github.com/r-causal/ggdag.
de Vries, Andrie, and Javier Luraschi. 2023. Nomnoml: Sassy UML Diagrams. https://rstudio.github.io/nomnoml/.
Iannone, Richard, and Olivier Roy. 2024. DiagrammeR: Graph/Network Visualization. https://rich-iannone.github.io/DiagrammeR/.
Soetaert, Karline. 2020. Diagram: Functions for Visualising Simple Graphs (Networks), Plotting Flow Diagrams.
Textor, Johannes, Benito van der Zander, and Ankur Ankan. 2023. Dagitty: Graphical Analysis of Structural Causal Models. https://www.dagitty.net.

  1. Depending on your background, this section may be a biased introduction to DiagrammeR. Please see its official documentation if you are interested in this package.↩︎