How to plot graphs using the DiagrammeR package in R?

DiagrammeR is a package in R that is used to create graphs and flowcharts using graphviz and mermaid.
In this article, let's look at how to create graphs using the grViz() in the DiagrammeR package.
- What is DiagrammeR?
- What is grViz()?
- How to create nodes, edges, labels, and their substitution and connection using grViz()
- How to knit to HTML
What is DiagrammeR
DiagrammeR is a package within htmlwidgets for R. It is used to generate graphs using graphviz and mermaid library.
1. Create an RMarkdown document.
In RStudio -> Go to File → New File →R Markdown. then give the title and select the output format as HTML.

The R Markdown document will have the document title and other information at the top.

Below that we will have r chunks which have delimiters ```{r}
and ""`
When you render the R Markdown document, it will run each chunk and embed the results beneath the code chunk or you can run each code chunk and the result will be displayed for that code chunk.
We can knit the R Markdown document to HTML output. Let's see how to do that in this article.
2. Install the DiagrammeR package
Go to Packages → Install on the right side of the RStudio and enter the package name DiagrammeR and click Install.

2. What is grViz?
DiagrammeR package uses the grViz() function for Graphviz graphs. Let's see how to use grViz() function to create graphs.
3. How to create nodes, edges, labels, and their substitution and connections using grViz()?
While creating a graph, we have to mention the layout, node attributes, edge attributes, and connection.

Let's see how to create the above-displayed graph. In the above graph, green color circles are called nodes. A=15, B=10, etc-> are the labels of the nodes. The arrow mark is known as the edges.
1. Mention the layout
The default layout is the dot.
layout = dot
2. Mention the Node attributes:
We can mention node shape, color, style, width, etc.
node [shape = circle,color=green,style=filled,fixedsize=True,width=0.6]
3. Mention the Edge Attributes:
Edge attributes include color, arrowhead, arrow tail, pen width, direction, etc.
edge[color=grey,arrowhead=vee]
4. Mention the label and substitutions:
We have to mention the label and substitution for the node.
A[label = 'A= @@1']
B[label = 'B=@@2']
C[label = 'C=@@3']
D[label = 'D=@@4']
E[label = 'E=@@5']
Here A -> node name
A=@@1 → This is the label.
[1]:15
[2]:10
[3]:20
[4]:2
[5]:2
@@1 -> is the substitution. Here 1 is the footnote number. The value 15 will be substituted in place of 1 when the graph is rendered.
5. Mention the connection details
In my graph A is connected to B and C. B is connected to D and C is connected to E.
A->B
A->C
B->D
C->E
Full R code:
```{r , echo=FALSE}
library(DiagrammeR)
grViz("
digraph {
layout = dot
node [shape = circle,color=green,style=filled,fixedsize=True,width=0.6]
edge[color=grey,arrowhead=vee]
A[label = 'A= @@1']
B[label = 'B=@@2']
C[label = 'C=@@3']
D[label = 'D=@@4']
E[label = 'E=@@5']
A->B
A->C
B->D
C->E
}
[1]:15
[2]:10
[3]:20
[4]:2
[5]:2
")
Output Graph:

Let's create another graph where nodes A and C are in the same rank and B, and E are in the same rank.
If we mention rank=same {A->C}. A and C nodes will be placed in the same line.
rank=same {A->C}
rank=same {B->E}
Full code
```{r , echo=FALSE}
library(DiagrammeR)
grViz("
digraph {
layout = dot
node [shape = circle,color=green,style=filled,fixedsize=True]
edge[color=grey,arrowhead=vee,minlen = 1]
A[label = 'A=@@1']
B[label = 'B=@@2']
C[label = 'C=@@3']
D[label = 'D=@@4']
E[label = 'E=@@5']
A->B
B->D
edge [minlen = 2]
rank=same {A->C}
rank=same {B->E}
}
[1]:15
[2]:10
[3]:20
[4]:2
[5]:2
")
Output graph:

Let's create a node from the down arrow of another node.

Let's look at how to create the graph mentioned above.
First, we have to create a blank node.
A -> blank1;
blank1 -> B[minlen=10];
{{ rank = same; blank1 B }}
blank1 -> C
C -> blank2;
blank2 -> D[minlen=1];
{{ rank = same; blank2 E }}
blank2 -> E [minlen=10]
Output:

After that we can make the blank node label= " " and the width and height to be very small (0.01)
blank1[label = '', width = 0.01, height = 0.01]
blank2[label = '', width = 0.01, height = 0.01]
Full code
```{r , echo=FALSE}
library(DiagrammeR)
grViz("
digraph {
layout = dot
node [shape = circle,color=green,style=filled,fontsize=45,fixedsize=True,width=4.0]
edge[color=grey,arrowhead=vee,penwidth=5,arrowsize=5]
A[label = 'A= @@1']
B[label = 'B=@@2']
C[label = 'C=@@3']
D[label = 'D=@@4']
E[label = 'E=@@5']
blank1[label = '', width = 0.01, height = 0.01]
A -> blank1;
blank1 -> B[minlen=10];
{{ rank = same; blank1 B }}
blank1 -> C
blank2[label = '', width = 0.01, height = 0.01]
C -> blank2;
blank2 -> D[minlen=1];
{{ rank = same; blank2 E }}
blank2 -> E [minlen=10]
}
[1]:15
[2]:10
[3]:20
[4]:2
[5]:2
")
**Output:**

The next step is to remove the arrow mark. It can be removed by mentioning the edge attribute "dir=none"
```r
A -> blank1[dir=none];
C -> blank2[dir=none];
Full code:
```{r , echo=FALSE}
library(DiagrammeR)
grViz("
digraph {
layout = dot
node [shape = circle,color=green,style=filled,fontsize=45,fixedsize=True,width=4.0]
edge[color=grey,arrowhead=vee,penwidth=5,arrowsize=5]
A[label = 'A= @@1']
B[label = 'B=@@2']
C[label = 'C=@@3']
D[label = 'D=@@4']
E[label = 'E=@@5']
blank1[label = '', width = 0.01, height = 0.01]
A -> blank1[dir=none];
blank1 -> B[minlen=10];
{{ rank = same; blank1 B }}
blank1 -> C
blank2[label = '', width = 0.01, height = 0.01]
C -> blank2[dir=none];
blank2 -> D[minlen=1];
{{ rank = same; blank2 E }}
blank2 -> E [minlen=10]
}
[1]:15
[2]:10
[3]:20
[4]:2
[5]:2
")
### Output:

## 4. Knit to html
Select the option knit -> knit to html

The whole R-Markdown document will be rendered into an HTML document.
If you don't want the r-code to be displayed in the HTML output, mention echo =False in the r-chunk code.
```r
```{r , echo=FALSE}```
This HTML page can be downloaded from my GitHub link.
https://github.com/IndhumathyChelliah/R-Projects
Conclusion:
DiagrammeR is a package in R that allows the creation of graphs using Graphviz and mermaid styles. In this article, we have covered using GraphViz style. We have covered how to create nodes, labels, edges, connections, and layouts for the graphs using the grViz() function in the DiagrammeR package. We have also covered node attributes, edge attributes, labels, and their substitutions. We have covered only the dot layout, which is the default layout in this article.
Thanks for reading and hope you all like it!