How to make a basic flowchart in R
This is a quick guide put together by a non-expert. It is not comprehensive in any way. You can copy and paste the code to make your own flowcharts quickly.
1 Resources and references
The examples in this short guide are taken or derived from the following two resources:
- https://rich-iannone.github.io/DiagrammeR/graphviz_and_mermaid.html
- https://datascienceplus.com/how-to-build-a-simple-flowchart-with-r-diagrammer-package/
- Link to this document: https://rpubs.com/anshulkumar/FlowchartsInR
Documentation for DiagrammeR package:
2 Simple flowcharts
2.1 Most basic and primitive version
This is the quickest way to make a flowchart (of which I’m aware), without modifying any default options:
library(DiagrammeR)
DiagrammeR::grViz("digraph {
Something -> SomethingElse -> 'Text With Spaces'
'Text with \n line break' -> SomethingElse
}")Tips:
- Copy and paste the code above for your own use.
- Change the names of
Something,SomethingElse, etc for your own needs.
2.2 Slightly formatted version
graph and node configurations are added below. This makes the chart horizontal, makes the nodes rectangluar, and gives them a color. I have also included a separate process that is unconnected to the initial one.
2.3 Another simple example
This is a flowchart that I was able to make in 30 seconds and send to someone by copying and pasting the code above and quickly changing the text in the nodes:
library(DiagrammeR)
DiagrammeR::grViz("digraph {
graph [layout = dot, rankdir = LR]
node [shape = rectangle, style = filled, fillcolor = Linen]
'Collect\ndata' -> 'Prepare data spreadsheet\n(takes a long time)' -> 'Analyze\ndata'
}")All you need to do is copy and paste the code above and modify this part:
'Collect\ndata' -> 'Prepare data spreadsheet\n(takes a long time)' -> 'Analyze\ndata'
3 Slightly complex flowcharts
Below is a slightly more complicated flowchart.
Here are some notes about it:
- Some nodes have their own non-default characteristics.
rankdir = LR– organize the flowchart from left to right;LRcan be changed toTBto go from top to bottom.label = 'Proposed Procedure'– add a label to the flowchartlabelloc = t– determine location of label;tcan be changed toborcas well.
library(DiagrammeR)
DiagrammeR::grViz("digraph {
# initiate graph
graph [layout = dot, rankdir = LR, label = 'Proposed Procedure\n\n',labelloc = t]
# global node settings
node [shape = rectangle, style = filled, fillcolor = Linen]
# label nodes
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', shape = diamond]
# edge definitions with the node IDs
{data1 data2} -> process -> statistical
statistical -> results
results -> {good bad}
bad -> {process 'Abandon project' 'Collect new data'}
'Collect new data' -> statistical
good -> 'Report Results'
}")