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:
Plot Dendrogram with R and ggraph
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.