DiagrammeR is an R package that facilitates the creation of flowcharts, diagrams, and grViz/graphviz visualizations straight from R scripts. It taps into the Graphviz software suite, allowing users to describe graphs in a textual manner and visualize them in R.
If you're new to the Graphviz syntax, start simple. Here's a basic decision flow on whether to make coffee:
# Install and load the DiagrammeR package
if(!require(DiagrammeR)) {
install.packages("DiagrammeR")
}
library(DiagrammeR)
# Create a flowchart using Graphviz DOT language
graph <- "
digraph flowchart {
# Node definitions with their shape and label
A [shape=box, label=\"Start\", color=\"blue\"]
B [shape=ellipse, label=\"Decision 1: Do I need coffee?\", color=\"orange\"]
C [shape=box, label=\"Yes, make coffee!\", color=\"green\"]
D [shape=box, label=\"No, continue working.\", color=\"red\"]
E [shape=ellipse, label=\"Decision 2: Is it break time?\", color=\"orange\"]
F [shape=box, label=\"Yes, take a break!\", color=\"green\"]
G [shape=box, label=\"No, keep working.\", color=\"red\"]
H [shape=box, label=\"End\", color=\"blue\"]
# Edges to indicate the flow
A -> B
B -> C [label=\"Yes\"]
B -> D [label=\"No\"]
D -> E
E -> F [label=\"Yes\"]
E -> G [label=\"No\"]
F -> H
G -> H
}
"
# Render the graph
grViz(graph)
Here’s what you get:
Once you're comfortable with the basics, let's delve into a more complex example that describes a project management process. This example includes a feedback loop from the review phase back to planning and introduces the handling of issues during the design, development, and testing phases.
if(!require(DiagrammeR)) {
install.packages("DiagrammeR")
}
library(DiagrammeR)
graph <- "
digraph flowchart {
# Node definitions
Start [shape=box, label=\"Start Project\", color=\"blue\"]
Plan [shape=ellipse, label=\"Planning\", color=\"orange\"]
Design [shape=ellipse, label=\"Design\", color=\"orange\"]
Develop [shape=ellipse, label=\"Development\", color=\"orange\"]
Test [shape=ellipse, label=\"Testing\", color=\"orange\"]
Deploy [shape=ellipse, label=\"Deployment\", color=\"orange\"]
Review [shape=ellipse, label=\"Review\", color=\"orange\"]
Finish [shape=box, label=\"End Project\", color=\"blue\"]
Issue [shape=diamond, label=\"Issue Encountered?\", color=\"yellow\"]
Resolve [shape=ellipse, label=\"Issue Resolution\", color=\"red\"]
# Edges to indicate the flow
Start -> Plan
Plan -> Design
Design -> Develop
Develop -> Test
Test -> Deploy
Deploy -> Review
Review -> Finish
Review -> Plan [label=\"Feedback Loop\", color=\"purple\"]
# Issue handling
Design -> Issue [label=\"\"]
Develop -> Issue [label=\"\"]
Test -> Issue [label=\"\"]
Issue -> Resolve [label=\"Yes\", color=\"red\"]
Resolve -> Test
Issue -> Deploy [label=\"No\"]
}
"
grViz(graph)
Here’s what you get:
While there are many tools out there for creating flowcharts and diagrams, DiagrammeR offers a unique proposition:
• Integration with R: If your data processing and analysis workflow is in R, DiagrammeR seamlessly integrates into that workflow.
• Scriptable & Reproducible: Since diagrams are generated from textual descriptions, they can be version-controlled, easily modified, and are inherently reproducible.
• Customizable: With a bit of Graphviz syntax, you can customize node shapes, colors, edge types, and more.
Whether you're mapping out a new project workflow, visualizing a complex system, or just pondering over your coffee routine, DiagrammeR provides a robust toolset for your diagramming needs in R.
BridgeText can help you with all of your statistics needs.