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 2020), 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:

  1. DiagrammeR::grViz("digraph {
  2. graph [layout = dot, rankdir = TB]
  3. node [shape = rectangle]
  4. rec1 [label = 'Step 1. Wake up']
  5. rec2 [label = 'Step 2. Write code']
  6. rec3 [label = 'Step 3. ???']
  7. rec4 [label = 'Step 4. PROFIT']
  8. # edge definitions with the node IDs
  9. rec1 -> rec2 -> rec3 -> rec4
  10. }",
  11. 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.

  1. DiagrammeR::grViz("
  2. digraph graph2 {
  3. graph [layout = dot, rankdir = LR]
  4. # node definitions with substituted label text
  5. node [shape = oval]
  6. a [label = '@@1']
  7. b [label = '@@2']
  8. c [label = '@@3']
  9. d [label = '@@4']
  10. a -> b -> c -> d
  11. }
  12. [1]: names(iris)[1]
  13. [2]: names(iris)[2]
  14. [3]: names(iris)[3]
  15. [4]: names(iris)[4]
  16. ",
  17. 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 (Luraschi, de Vries, and Kallin 2020), diagram (Soetaert 2020), dagitty (Textor and van der Zander 2016), ggdag (Barrett 2020), and plantuml (https://github.com/rkrug/plantuml).

References

Barrett, Malcolm. 2020. Ggdag: Analyze and Create Elegant Directed Acyclic Graphs. https://github.com/malcolmbarrett/ggdag.

Iannone, Richard. 2020. DiagrammeR: Graph/Network Visualization. https://github.com/rich-iannone/DiagrammeR.

Luraschi, Javier, Andrie de Vries, and Daniel Kallin. 2020. Nomnoml: Sassy UML Diagrams. https://github.com/javierluraschi/nomnoml.

Soetaert, Karline. 2020. Diagram: Functions for Visualising Simple Graphs (networks), Plotting Flow Diagrams. https://CRAN.R-project.org/package=diagram.

Textor, Johannes, and Benito van der Zander. 2016. Dagitty: Graphical Analysis of Structural Causal Models. https://CRAN.R-project.org/package=dagitty.


  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.↩︎