Data-driven flowcharts in R using DiagrammeR · Michael Harper (2024)

The old cliché goes that “A picture is worth a thousand words”. This may be overused, but I feel this is particularly relevant in data analysis, and we often use diagrams as tools to quickly communicate the results of our complex statistical models. However, whilst we often prioritise on explaining the results of the analysis, we are often less inclined to explain the stages used to reach the results to the same level of detail.

In this post, I highlight some of the great features of making diagrams in R, and highlight how they can be easily connected to data from your analysis to produce dynamic data flowchart. This can make it much easier for readers to engage with your data analysis and understand how your complex models work.

Making diagrams in R

The DiagrammeR package (Iannone 2018) is a package which allows graphs to be generated for a simple coding syntax. Graphs are primarily drawn in the DOT language using the GraphViz and `mermaid styles. The package also provides a useful interface for creating graphs directly from R code. To get started we first need to install the package:

install.packages("DiagrammeR")

The creation of figures in R is further simplified by the integration with RStudio, which provides native support for .dot, .mmd or .gv files which can be used for containing charts. RStudio provides syntax highlighting for any of these files, and allows you to quickly preview the results of the diagram within the view window, as shown in Figure 1

While this guide by no means attempt to replace the detailed guidance provided on the package website, it is important to highlight a few of the basic features of the plots. DiagrammeR uses the processing function called grViz(). What you pass into grViz() is a valid graph specification in the DOT language, with the figure being supplied as either an external file or inline code within R.

There are many different types of graphs which can be drawn, but in this post we will focus on directed graphs (diagraph), which are most typically used within flowcharts. This must be specified at the start of the plot.Graphs can be controlled by three functions:

  • graph: this sets the overall layout, themes of the plot
  • node: the boxes in the flowchart
  • edges: the lines used to connect each of the boxes

A minimal example is shown in Figure 2:

# A minimal plotDiagrammeR::grViz("digraph { graph[layout = dot, rankdir = LR]abca -> b -> c}")

Figure 2: A minimal example

You can see above that the plot is very simple, and it is likely that you will want to change the style of the boxes by adding colours etc. To do this, you can define as many styles as we wish within square brackets following the object using name-value pairs. You may wish to see the full list of node shapes available here

For example, if we wanted to set the fill colour of node box1, or change the style of the arrowhead of a connector we can use the following code:

box1 [fillcolor = red]abc -> def [arrowhead = diamond]

A crucial option to specify is the label option, which lets us change the text within the shape. . We will piece together some of the examples below to highlight a more complex graph:

DiagrammeR::grViz("digraph {graph [layout = dot, rankdir = LR]# define the global styles of the nodes. We can override these in box if we wishnode [shape = rectangle, style = filled, fillcolor = Linen]data1 [label = 'Dataset 1', shape = folder, fillcolor = Beige]data2 [label = 'Dataset 2', shape = folder, fillcolor = Beige]process [label = 'Process \n Data']statistical [label = 'Statistical \n Analysis']results [label= 'Results']# edge definitions with the node IDs{data1 data2} -> process -> statistical -> results}")

Figure 3: A basic flowchart

Parameterized figures

A great benefit of designing figures within R is that we are able to connect the figures directly with our analysis by reading R values directly into our flowcharts. For example, suppose you have created a filtering process which removes values after each stage of a process, you can have a figure show the number of values left in the dataset after each stage of your process. To do this we, you can use the @@X symbol directly within the figure, then refer to this in the footer of the plot using [X]:, where X is the a unique numeric index. Here is a basic example:

# Define some sample datadata <- list(a=1000, b=800, c=600, d=400)DiagrammeR::grViz("digraph graph2 {graph [layout = dot]# node definitions with substituted label textnode [shape = rectangle, width = 4, fillcolor = Biege]a [label = '@@1']b [label = '@@2']c [label = '@@3']d [label = '@@4']a -> b -> c -> d}[1]: paste0('Raw Data (n = ', data$a, ')')[2]: paste0('Remove Errors (n = ', data$b, ')')[3]: paste0('Identify Potential Customers (n = ', data$c, ')')[4]: paste0('Select Top Priorities (n = ', data$d, ')')")

Figure 4: A flowchart containing R code replacement

The code for the chart in the header image is also available here

Subgraphs and clusters

This post is not designed to be a comprehensive guide of DiagrammeR, but it is worth highlight that we can use subgraphs and clusters to demonstrates groups within the data. Figure 5 highlights an example of a graph I have produced within my analysis to show the classification of data across different categories within my model. The code used to make this graph is available through the GitHub repository here.

Figure 5: An example of subgraphs used to highlight grouping within a dataset

Further Resources

If you want to read the full documentation of the package you should check out the website here: http://rich-iannone.github.io/DiagrammeR/ . The features extend well beyond what is explained within this brief post.

If you have made any data-driven R Markdown graphs, I would love to see them! Feel free to share any examples of using it in the comments below.

References

Iannone, Richard. 2018. DiagrammeR: Graph/Network Visualization. https://CRAN.R-project.org/package=DiagrammeR.

Data-driven flowcharts in R using DiagrammeR  · Michael Harper (2024)

References

Top Articles
Barefoot Rentals Key Largo
’You’ Season 5: Everything To Know About Stalker Joe’s Last Stand
Pwc Transparency Report
Episode 163 – Succession and Legacy • History of the Germans Podcast
799: The Lives of Others - This American Life
Fbsm Berkeley
Academic Calendar Biola
Mashle: Magic And Muscles Gogoanime
Food And Grocery Walmart Job
Craigslist Pinellas County Rentals
Aces Charting Ehr
Find The Eagle Hunter High To The East
Craigslist Farm And Garden Yakima Wa
Mayo Webscheduler
Join MileSplit to get access to the latest news, films, and events!
Sandra Sancc
Where to Buy Fresh Masa (and Masa Harina) in the U.S.
When His Eyes Opened Chapter 2981
Hướng Dẫn Trade Bittrex
Rick Harrison Daughter Ciana
洗面台用 アクセサリー セットの商品検索結果 | メチャ買いたい.com
Lexington Park Craigslist
Fandango Movies And Shows
General Kearny Inn Motel & Event Center
Naval Academy Baseball Roster
Eotech Eflx Torque Specs
Palindromic Sony Console For Short Crossword Clue 6 Letters: Composer Of
3 Hour Radius From Me
Aig Cyberedge Policy Wording
Visit Lake Oswego! - Lake Oswego Chamber Of Commerce
Newton Chevrolet Of Russellville Photos
A Closer Look at Ot Megan Age: From TikTok Star to Media Sensation
Forums Social Media Girls Women Of Barstool
San Diego Box Score
New R-Link system and now issues creating R-Link store account.
Planet Zoo Obstructed
Www.playgd.mobi Wallet
Herbalism Guide Tbc
Theatervoorstellingen in Roosendaal, het complete aanbod.
Justina Morley Now
Nz Herald Obituary Notices
Trap Candy Strain Leafly
Mychart Mountainstarhealth
Nc Maxpreps
Webworx Call Management
How To Buy Taylor Swift Tickets By Navigating Ticketek's Stress-Inducing System
Aso Tools Vancouver
Wv Anon Vault
Horoskopi Koha
General Format - Purdue OWL® - Purdue University
Omaha World-Herald from Omaha, Nebraska
Craigslist Org Las Vegas Cars
Latest Posts
Article information

Author: Moshe Kshlerin

Last Updated:

Views: 5676

Rating: 4.7 / 5 (57 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Moshe Kshlerin

Birthday: 1994-01-25

Address: Suite 609 315 Lupita Unions, Ronnieburgh, MI 62697

Phone: +2424755286529

Job: District Education Designer

Hobby: Yoga, Gunsmithing, Singing, 3D printing, Nordic skating, Soapmaking, Juggling

Introduction: My name is Moshe Kshlerin, I am a gleaming, attractive, outstanding, pleasant, delightful, outstanding, famous person who loves writing and wants to share my knowledge and understanding with you.