R Visualizations: Flow Charts in R | by Paul Aleksis | Towards Data Science

In the field of data analytics, data visualization is one of the single most important tools when starting new projects. It aids discovery and allows data scientists to read the story of their data. I have recently noticed how much I love R over Python for my data visualization needs. Because of this, I have started a small blog series on R visualizations beginning with this blog on flow charts. You can find the code to this blog on my GitHub.
Flow charts are a great way to express ideas or logic with a top-down approach. Personally, I often use them in setting up for debates or trying to understand an idea. I present the idea in the uppermost node and each argument supporting the idea in lower level nodes along with counter-arguments in opposing nodes.
Flow charts are also super useful when you are learning a new skill. Whenever learning something new, I start at a super high contextual level to grasp some understanding, then I dive lower into the finer details of what I need to know at that particular time of learning. Having a visual aid such as a flow chart is very useful in this because it serves as a roadmap to my understanding.
Begin by installing the DiagrammeR package, so that we can take advantage of its node graphing methods. Once installed, import it into your R script using the library() function.
install.packages("DiagrammeR")library(DiagrammeR)
The next step is to run the grViz() function from the library and enter your syntax. Below is the syntax to a very basic top-down flow chart.
grViz(diagram = "digraph flowchart {  node [fontname = arial, shape = oval]  tab1 [label = '@@1']  tab2 [label = '@@2']  tab3 [label = '@@3']    tab1 -> tab2 -> tab3;}    [1]: 'Learning Data Science'  [2]: 'Industry vs Technical Knowledge'      [3]: 'Statistics vs Mathematics Knowledge'      ")
Within the node argument, you can set a font and shape for the text in the nodes and the node itself. I used oval in this example but you can use square, diamond, rectangle, circle, triangle, etc...
Note that you can add as many tabs by defining them after the node input and connect them by drawing their path.
the output looks like this:
It is also possible to add color to the outline of your nodes, or even fill them with a color. To do so, add additional arguments “color” and “style” to the node section of the syntax as shown below.
grViz(diagram = "digraph flowchart {  node [fontname = arial, shape = oval, color = grey, style = filled]  tab1 [label = '@@1']  tab2 [label = '@@2']  tab3 [label = '@@3']    tab1 -> tab2 -> tab3;}    [1]: 'Learning Data Science'  [2]: 'Industry vs Technical Knowledge'      [3]: 'Statistics vs Mathematics Knowledge'      ")
The output looks like this:
You can find a list of colors, edge attributes, node shapes, node attributes, and arrow shapes from the source documentation.
To split up your arrows and connect one node to multiple nodes, all you need to do is adjust the layout argument.
grViz(diagram = "digraph flowchart {      # define node aesthetics      node [fontname = Arial, shape = oval, color = Lavender, style = filled]              tab1 [label = '@@1']      tab2 [label = '@@2']      tab3 [label = '@@3']      tab4 [label = '@@4']# set up node layout      tab1 -> tab2;      tab2 -> tab3;      tab2 -> tab4      }[1]: 'Learning Data Science'      [2]: 'Industry vs Technical Knowledge'      [3]: 'Python/R'      [4]: 'Domain Experience'      ")
The output looks like this:
There are some additional arguments you can include in your function to customize things such as font size, font color, height, width, alpha, etc...
Just add the argument within the node [] section of your formula for example:
grViz(diagram = "digraph flowchart {      # define node aesthetics      node [fontname = Arial, shape = oval, color = DeepSkyBlue, style = filled, fontcolor = White]              tab1 [label = '@@1']      tab2 [label = '@@2']      tab3 [label = '@@3']      tab4 [label = '@@4']# set up node layout      tab1 -> tab2;      tab2 -> tab3;      tab2 -> tab4      }      [1]: 'Learning Data Science'      [2]: 'Industry vs Technical Knowledge'      [3]: 'Python/R'      [4]: 'Domain Experience'      ")
Please review the documentation for more customization options.
Flow charts are a great way to express ideas especially at the beginning stages of production. Consider using R visualizations in your future work and take advantage of its powerful graphing libraries.
Sources:
rich-iannone.github.io
Code for this blog can be found on my GitHub.