# Plot Dendrogram with R and ggraph

• Last Updated : 22 Feb, 2022

In this article, we are going to see how to customize dendrogram. It is also known as a tree diagram and it is a visual representation of the hierarchical relationship between items. A dendrogram’s main purpose is to figure out the best approach to assign objects to clusters.

## Creating Basic Dendrogram

To create a dendrogram we will igraph package which is used for creating and manipulating graphs and analyzing networks graph. So for creating a dendrogram, we will create a dataframe into the hierarchical data structure and then transform it into an edge list. To plot a Dendrogram we will use the following syntax:

ggraph(dataset, layout)

Parameters:

• Dataframe: Hierarchical dataset/vector
• layout: Dimension of layout
• Circular: True/False

Syntax to install and import igraph package in the R working console:

install.package(‘igraph’)

library(igraph)

There are some optional functions to create edge into diagonal we will use geom_edge_diagonal() methods and to add nodes into a graph we will use geom_node_point() methods.

## R

 `# libraries` `library``(ggraph)` `library``(igraph)` `library``(tidyverse)` ` `  `# create a data frame ` `data <- ``data.frame``(` `  ``level1=``"CEO"``,` `  ``level2=``c``( ``rep``(``"boss1"``,4), ``rep``(``"boss2"``,4)),` `  ``level3=``paste0``(``"mister_"``, ``letters``[1:8])` `)` ` `  `# transform it to a edge list!` `edges_level1_2 <- data %>% ``select``(` `  ``level1, level2) %>% unique %>% ``rename``(` `  ``from=level1, to=level2)`   `edges_level2_3 <- data %>% ``select``(` `  ``level2, level3) %>% unique %>% ``rename``(` `  ``from=level2, to=level3)` `edge_list=``rbind``(edges_level1_2, edges_level2_3)` ` `  `# Now we can plot that` `graph_Data <- ``graph_from_data_frame``( edge_list )` `ggraph``(graph_Data, layout = ``'dendrogram'``, circular = ``FALSE``) + ` `  ``geom_edge_diagonal``() +` `  ``geom_node_point``() +` `  ``theme_void``()`

Output:

## Circular Layout Dendrogram

To create a circular layout we will use circular = True, which will plot it into the circular layout.

## R

 `# libraries` `library``(ggraph)` `library``(igraph)` `library``(tidyverse)` ` `  `# create a data frame ` `data <- ``data.frame``(` `  ``level1=``"CEO"``,` `  ``level2=``c``( ``rep``(``"boss1"``,4), ``rep``(``"boss2"``,4)),` `  ``level3=``paste0``(``"mister_"``, ``letters``[1:8])` `)` ` `  `# transform it to a edge list!` `edges_level1_2 <- data %>% ``select``(` `  ``level1, level2) %>% unique %>% ``rename``(` `  ``from=level1, to=level2)` `edges_level2_3 <- data %>% ``select``(` `  ``level2, level3) %>% unique %>% ``rename``(` `  ``from=level2, to=level3)` `edge_list=``rbind``(edges_level1_2, edges_level2_3)` ` `  `# plot` `graph_Data <- ``graph_from_data_frame``( edge_list )` `ggraph``(graph_Data, layout = ``'dendrogram'``, circular = ``TRUE``) + ` `  ``geom_edge_diagonal``()+` `  ``geom_edge_diagonal``() +` `  ``geom_node_point``() +` `  ``theme_void``()`

Output:

## Edge style Dendrogram

To make the straight edge dendrogram we will use geom_edge_diagonal() methods.

## R

 `# libraries` `library``(ggraph)` `library``(igraph)` `library``(tidyverse)` ` `  `# create a data frame ` `data <- ``data.frame``(` `  ``level1=``"CEO"``,` `  ``level2=``c``( ``rep``(``"boss1"``,4), ``rep``(``"boss2"``,4)),` `  ``level3=``paste0``(``"mister_"``, ``letters``[1:8])` `)` ` `  `# transform it to a edge list!` `edges_level1_2 <- data %>% ``select``(` `  ``level1, level2) %>% unique %>% ``rename``(` `  ``from=level1, to=level2)`   `edges_level2_3 <- data %>% ``select``(` `  ``level2, level3) %>% unique %>% ``rename``(` `  ``from=level2, to=level3)` `edge_list=``rbind``(edges_level1_2, edges_level2_3)` ` `  `# Now we can plot that` `graph_Data <- ``graph_from_data_frame``( edge_list )` `ggraph``(graph_Data, layout = ``'dendrogram'``) + ` `  ``geom_edge_link``()+` `  ``geom_node_point``() +` `  ``theme_void``()`

Output:

## Labels in Dendrogram

To add the labels into the dendrogram we will use geom_node_text() which will add the labels into the graph into a hierarchical structure

## R

 `# libraries` `library``(ggraph)` `library``(igraph)` `library``(tidyverse)` ` `  `# create a data frame ` `data <- ``data.frame``(` `  ``level1=``"CEO"``,` `  ``level2=``c``( ``rep``(``"boss1"``,4), ``rep``(``"boss2"``,4)),` `  ``level3=``paste0``(``"mister_"``, ``letters``[1:8])` `)` ` `  `# transform it to a edge list!` `edges_level1_2 <- data %>% ``select``(` `  ``level1, level2) %>% unique %>% ``rename``(` `  ``from=level1, to=level2)`   `edges_level2_3 <- data %>% ``select``(` `  ``level2, level3) %>% unique %>% ``rename``(` `  ``from=level2, to=level3)` `edge_list=``rbind``(edges_level1_2, edges_level2_3)` ` `  `# Now we can plot that` `graph_Data <- ``graph_from_data_frame``( edge_list )` `ggraph``(graph_Data, layout = ``'dendrogram'``) + ` `  ``geom_edge_diagonal``() +` `  ``geom_node_text``(``aes``( label=name)) +` `  ``theme_void``()`

Output:

## Customize Labels in Dendrogram

To Customize Labels in Dendrogram we use geom_node_text() which will add the labels along with color into the graph into the hierarchical structure

## R

 `# libraries` `library``(ggraph)` `library``(igraph)` `library``(tidyverse)` ` `  `# create a data frame ` `data <- ``data.frame``(` `  ``level1=``"CEO"``,` `  ``level2=``c``( ``rep``(``"boss1"``,4), ``rep``(``"boss2"``,4)),` `  ``level3=``paste0``(``"mister_"``, ``letters``[1:8])` `)` ` `  `# transform it to a edge list!` `edges_level1_2 <- data %>% ``select``(` `  ``level1, level2) %>% unique %>% ``rename``(` `  ``from=level1, to=level2)`   `edges_level2_3 <- data %>% ``select``(` `  ``level2, level3) %>% unique %>% ``rename``(` `  ``from=level2, to=level3)` `edge_list=``rbind``(edges_level1_2, edges_level2_3)` ` `  `# Now we can plot that` `graph_Data <- ``graph_from_data_frame``( edge_list )` `ggraph``(graph_Data, layout = ``'dendrogram'``) + ` `  ``geom_edge_diagonal``() +` `  ``geom_node_text``(``aes``( label=name, color=``"red"``)) +` `  ``theme_void``()`

Output:

## Customize Nodes in Dendrogram

To Customize Labels in Dendrogram we use geom_node_point() which will add the nodes along with color into the graph into a hierarchical structure

## R

 `# libraries` `library``(ggraph)` `library``(igraph)` `library``(tidyverse)` ` `  `# create a data frame ` `data <- ``data.frame``(` `  ``level1=``"CEO"``,` `  ``level2=``c``( ``rep``(``"boss1"``,4), ``rep``(``"boss2"``,4)),` `  ``level3=``paste0``(``"mister_"``, ``letters``[1:8])` `)` ` `  `# transform it to a edge list!` `edges_level1_2 <- data %>% ``select``(` `  ``level1, level2) %>% unique %>% ``rename``(` `  ``from=level1, to=level2)`   `edges_level2_3 <- data %>% ``select``(` `  ``level2, level3) %>% unique %>% ``rename``(` `  ``from=level2, to=level3)`   `edge_list=``rbind``(edges_level1_2, edges_level2_3)` ` `  `# Now we can plot that` `graph_Data <- ``graph_from_data_frame``( edge_list )` `ggraph``(graph_Data, layout = ``'dendrogram'``) + ` `  ``geom_edge_diagonal``() +` `  ``geom_node_text``(``aes``( label=name, color=``"red"``)) +` `  ``geom_node_point``(``aes``(color =``"Blue"``))+` `theme_void``()`

Output:

My Personal Notes arrow_drop_up
Related Articles