Adding a Dendrogram to a ggplot2 Heatmap in R
Last Updated :
28 Apr, 2025
A potent technique that improves the visualisation of hierarchical clustering patterns inside data is to add a dendrogram to a ggplot2 heatmap in R. Dendrograms shed light on the connections and resemblances among data points, assisting in the discovery of distinct clusters or groups. We will examine how to combine dendrograms with ggplot2 heatmaps using R in this article, enabling a more thorough comprehension of intricate data structures and patterns.
Heatmap
A heatmap is a graphical representation of data in which each value is represented by a different colour. It's very helpful for analysing complicated data sets and spotting trends or correlations between variables. Large datasets may be interpreted and analysed more easily because each cell in the heatmap represents a data point and is coloured according to its value.
Dendrogram
A dendrogram is a tree-like diagram used in hierarchical clustering to represent the arrangement of data points based on their similarity or dissimilarity. Dendrograms are often displayed alongside heatmaps to help visualize how data points cluster together.
Applications of Dendrograms:
Genetics: Dendrograms are widely used in genetics to analyze DNA sequences and determine evolutionary relationships between species or individuals.
Market Segmentation: In marketing, dendrograms can be used to group customers or products based on similarities in their purchasing behavior or characteristics.
Phylogenetics: Biologists use dendrograms to construct evolutionary trees and understand the evolutionary history of species.
The hierarchical clustering of the data may be learned a lot by adding a dendrogram to a R ggplot2 heatmap. The ggtree package, which enables the addition of dendrograms to ggplot2 displays, can be used to accomplish this. A step-by-step tutorial for adding a dendrogram to a ggplot2 heatmap is provided below:
Hierarchical Clustering: Hierarchical clustering is a method that organizes data points into a hierarchical tree-like structure called a dendrogram. It is commonly used to group similar observations together based on their similarity or dissimilarity.
ggplot2: ggplot2 is a popular R package for data visualization. It allows for the creation of a wide range of customizable graphics and plots, making it a versatile choice for adding dendrograms to heatmaps.
Using heatmap Function:
R
# Generate random data for the heatmap
data <- matrix(runif(25), nrow=5)
# Create a heatmap
heatmap(data, col=cm.colors(256), main="Random Data Heatmap",
xlab="X-axis", ylab="Y-axis")
Output:
Adding a Dendrogram to a ggplot2 Heatmap in RIn this example, we first generate random data using matrix and runif. Then, we create a heatmap using the heatmap function, specifying the color palette with col and adding labels and a title.
Using heatmap.2 Function from the gplots Package:
R
# install.packages("gplots")
library(gplots)
# Generate random data for the heatmap
data <- matrix(runif(25), nrow=5)
# Create a heatmap using heatmap.2
heatmap.2(data, col=cm.colors(256), main="Random Data Heatmap",
xlab="X-axis", ylab="Y-axis")
Output:
Adding a Dendrogram to a ggplot2 Heatmap in RIn this example, we load the gplots package, generate random data, and create a heatmap using the more flexible heatmap.2 function. Like the previous example, we specify the color palette with col and add labels and a title.
Using Dendextend Library
R
# Install and load required packages
install.packages("gplots")
library(gplots)
library(dendextend)
# Sample data
set.seed(123)
data_matrix <- matrix(rnorm(100), nrow = 10)
rownames(data_matrix) <- paste("Row", 1:10)
colnames(data_matrix) <- paste("Col", 1:10)
# Calculate hierarchical clustering
row_dendro <- as.dendrogram(hclust(dist(data_matrix)))
col_dendro <- as.dendrogram(hclust(dist(t(data_matrix))))
# Create a heatmap with dendrograms using heatmap.2
heatmap_plot <- heatmap.2(data_matrix,
dendrogram = "row",
Colv = col_dendro,
trace = "none",
col = colorRampPalette(c("blue", "white", "red"))(50),
scale = "none",
key = TRUE,
keysize = 1.0,
key.title = NA)
print(heatmap_plot)
Output:
Adding a Dendrogram to a ggplot2 Heatmap in R- We use the heatmap.2() function from the gplots package to create the heatmap.
- We specify dendrogram = "row" to add a row dendrogram.
- Colv = col_dendro specifies the column dendrogram.
- trace = "none" removes the trace lines.
- col specifies the color palette for the heatmap.
- scale = "none" prevents scaling of the data.
- key = TRUE adds a color key.
- keysize = 1.0 sets the size of the color key.
- key.title = NA removes the color key title.
Adding a Dendrogram to a Heatmap in R using heatmaply
R
# Load necessary libraries
library(heatmaply)
# Create a sample data matrix (replace with your actual data)
set.seed(123)
data_matrix <- matrix(rnorm(100), nrow = 10)
colnames(data_matrix) <- letters[1:10]
rownames(data_matrix) <- letters[11:20]
# Calculate hierarchical clustering for rows and columns
hc_rows <- hclust(dist(data_matrix))
hc_cols <- hclust(dist(t(data_matrix)))
# Create dendrograms
row_dendro <- as.dendrogram(hc_rows)
col_dendro <- as.dendrogram(hc_cols)
# Create the heatmap with dendrograms using heatmaply
heatmaply(data_matrix, Rowv = row_dendro, Colv = col_dendro)
Output:
Adding a Dendrogram to a ggplot2 Heatmap in R- We generate a sample data matrix (data_matrix) for demonstration purposes. You should replace this with your actual data.
- We perform hierarchical clustering on both the rows and columns of the data matrix using hclust and dist functions. This creates dendrogram objects (row_dendro and col_dendro) that represent the hierarchical structure of the data.
- The main part of the code is the creation of the heatmap with dendrograms using the heatmaply function.
- The heatmaply function generates an interactive heatmap plot. You can explore this plot by clicking and interacting with it. It provides features like zooming, hovering to see data values, and more.
- Remember to replace the sample data (data_matrix) with your actual dataset to visualize your data in the heatmap with dendrograms.
Conclusion
Finally, the addition of dendrograms to R's ggplot2 heatmaps provides a useful way to find hierarchical clustering patterns in large datasets. Insights into data linkages are provided by dendrograms, which facilitate the recognition of unique clusters or groups. This article has described several approaches for combining dendrograms with ggplot2 heatmaps, including the ggtree package and heatmaply function. Utilising these methods enables data researchers and analysts to efficiently analyse complex data structures and trends, making it a useful tool for a variety of applications, from market segmentation to genetics.
Similar Reads
Create Heatmap in R Using ggplot2
A heatmap depicts the relationship between two attributes of a data frame as a color-coded tile. A heatmap produces a grid with multiple attributes of the data frame, representing the relationship between the two attributes taken at a time. In both data analysis and visualization, heatmaps are a com
5 min read
Change Color of Range in ggplot2 Heatmap in R
A heatmap represents the connection between two properties of a dataframe as a color-coded tile. A heatmap generates a grid with several properties of the dataframe, showing the connection between the two properties at a time.Creating a Basic Heatmap in ggplot2Let us first create a regular heatmap w
3 min read
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. Creating Basic Dendrog
4 min read
How to Draw Heatmap with Colorful Dendrogram in R
Heatmaps are powerful visualizations for representing data density, correlations, or the relationship between variables. Combined with dendrograms, they allow for an insightful exploration of hierarchical clustering. In this article, weâll learn how to create a heatmap with a colorful dendrogram in
3 min read
How to add image to ggplot2 in R ?
In this article, we will discuss how to insert or add an image into a plot using ggplot2 in R Programming Language. The ggplot() method of this package is used to initialize a ggplot object. It can be used to declare the input data frame for a graphic and can also be used to specify the set of plot
4 min read
How to Add Caption to a ggplot in R?
In this article, we are going to see how we can add a caption to a plot in R Programming Language. The caption is much important in data visualization to display some details related to graphs. Preparing Data To plot the scatterplot we will use we will be using the geom_point() function. Following i
2 min read
How to annotate a plot in ggplot2 in R ?
In this article, we will discuss how to annotate functions in R Programming Language in ggplot2 and also read the use cases of annotate. What is annotate?An annotate function in R can help the readability of a plot. It allows adding text to a plot or highlighting a specific portion of the curve. Th
4 min read
Assigning Colors to Values in a Seaborn Heatmap
Heatmaps are a powerful visualization tool that can help you understand complex data sets by representing values as colors. The primary element of heatmeaps is the use of color. The right color palette can highlights subtle difference in the data, emphasize the relationships and make the heatmap mor
7 min read
Creating a Legend for Google Heatmap
Heatmaps are a powerful tool for visualizing data, especially when representing the intensity of data points on a map. Google Maps provides a Heatmap layer to visualize geographic data, but one common challenge users face is adding a legend to explain the color intensities. In this article, we'll co
5 min read
Smooth data for a geom_area graph Using ggplot2 in R
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in R. The package can be downloaded and installed into the working space using the following command : in
3 min read