VOOZH about

URL: https://www.geeksforgeeks.org/r-language/plot-dendrogram-with-r-and-ggraph/

⇱ Plot Dendrogram with R and ggraph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

Output:


 

👁 Image

Circular Layout Dendrogram


 

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


 

Output:


 

👁 Image

Edge style Dendrogram


 

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


 

Output:


 

👁 Image

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


 

Output:


 

👁 Image

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


 

Output:


 

👁 Image

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


 

Output:


 

👁 Image


 

Comment

Explore