Open In App

Draw Heatmap with Clusters Using pheatmap in R

Last Updated : 05 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In data visualization, heatmaps are frequently used to show numerical data in a matrix structure where each value is represented by a color. In this article, we will see how we can draw heatmaps with clusters using the Pheatmap package. In R programming, the heatmap visualizations can be produced using the Pheatmap package. Its excellent clustering feature can be utilized to find collections of related rows or columns. By putting comparable rows or columns together based on the values in the data matrix, clustering is done. You can also choose to cluster by rows, columns, or both.

Required Module

To install the Pheatmap package in the R script, write the following code:

install.packages("pheatmap")

Heatmap with Clusters Using Pheatmap in R

The heatmap customization tools provided by Pheatmap include clustering, scaling, and annotation. It has a number of features to personalize and enhance visualizations. Pheatmap’s clustering feature enables the hierarchical grouping of rows and/or columns using a variety of linkage techniques, including single, complete, average, and others.

Pheatmap is a flexible tool for building customized heatmaps that can aid in the exploration and visualization of complex datasets thanks to these capabilities. The Pheatmap package provided the following feature:

  • Scaling: To show the relative differences in the data, Pheatmap provides options to scale the rows and/or columns using z-score or other techniques.
  • Color gradations: Pheatmap offers a variety of color gradations to display the data and lets the user modify the color gradation. Additionally, it supports color schemes from other R tools like RColorBrewer.
  • Annotations: Pheatmap enables users to add notes to the rows and/or columns of the heatmap. The user can scale the heatmap, text size, and margins using Pheatmap in order to fit the visualization into the output format they want.
  • Exporting: Pheatmap gives users the option to export heatmaps in a number of file types, including PDF, PNG, JPEG, and SVG.
  • Customization: The heatmap can be tailored in a variety of ways with Pheatmap, including text size, row and column names, and more.

Creating a Simple Heatmap in R

First, we will load the pheatmap package. Then, using mtcars[,1:6], we import the mtcars dataset and extract the numerical data from the first 6 columns. After standardizing the variables, we scale the data using the scale(mtcars_data). The scaled data is then passed to pheatmap(), where the parameters scaled_data and main = “mtcars heatmap” are used to draw a heatmap of the data and set the heatmap’s title.

R




library(pheatmap)
  
# Load the mtcars dataset
data(mtcars)
  
# Extract the numerical data from the mtcars dataset
mtcars_data <- mtcars[,1:6]
  
# Scale the data
scaled_data <- scale(mtcars_data)
  
# Cluster the rows
clustered_data <- pheatmap(scaled_data, 
                           main = "mtcars heatmap")


Output:

Creating a Simple Heatmap in R

Default Heatmap

Heatmap with Row Clusters in R

To create a heatmap with row clusters in R, you can use the pheatmap() function with the cluster_cols argument set to F(false). This will cluster the rows of the heatmap in hierarchical clusters and turn off the clustering for columns.

R




library(pheatmap)
  
# Load the "mtcars" dataset
data(mtcars)
  
# Create a matrix of the data
m <- as.matrix(mtcars)
  
# Create a heatmap with row clusters
pheatmap(m, cluster_cols = F, main = "Row Cluster Heatmap")


Output:

Heatmap with Row clusters

Heatmap with Row clusters

Heatmap with Column Clusters in R

To create a heatmap with column clusters in R, you can use the pheatmap() function with the cluster_rows argument set to F(false). This will cluster the columns of the heatmap in hierarchical clusters and turn off the clustering for the rows.

R




library(pheatmap)
  
# Load the "mtcars" dataset
data(mtcars)
  
# Create a matrix of the data
m <- as.matrix(mtcars)
  
# Create a heatmap with column clusters
pheatmap(m, cluster_rows = F, main = "Column Cluster Heatmap")


Output:

Heatmap with Column Clusters

Heatmap with Column Clusters

Heatmap cut into pieces in R

To create a heatmap with column and row clusters in R, you can use the pheatmap() function with the cutree_cols and cutree_rows argument set to a specific value. This will cluster the columns and rows of the heatmap using hierarchical clustering and display the resulting dendrogram on the top of the heatmap.

R




library(pheatmap)
  
# Load the "mtcars" dataset
data(mtcars)
  
# Create a matrix of the data
m <- as.matrix(mtcars)
  
# Create a heatmap divided in pieces
pheatmap(m, cutree_rows = 5, cutree_cols = 4, 
         main = "Heatmap divided into pieces")


Output:

Heatmap cut into pieces in R

Heatmap divided into pieces

Customizing Heatmap in R

The heatmap package provides us with various tools and functions using which we can customize the heatmap to our own requirements. We can cluster rows and columns, and change the title, color, font size, cell size, borders, etc of our heatmap to make it more attractive.

In this example, we will be doing the following changes:

  • Cell color: using a custom color palette chosen using the colorRampPalette() function.
  • Font size: using the fontsize_row and fontsize_col, the row and column labels have been set.
  • Cell size: specifying the cellwidth and cellheight.
  • Border: the borders have been removed by setting the heatmap cells’ border color to NA.
  • K-means clustering: it has been set to use four clusters or kmeans_k.
  • Legend: using the legend_breaks, legend_labels, and fontsize_legend, we have added a legend to the heatmap, customized its labels and breaks, and enlarged its font.

Finally, we used the dev.copy() and dev.off() functions to save the heatmap as a PNG file.

R




library(pheatmap)
  
# Load the mtcars dataset
data(mtcars)
  
# Extract the numerical data from the mtcars dataset
mtcars_data <- mtcars[,1:6]
  
# Scale the data
scaled_data <- scale(mtcars_data)
  
# Cluster the rows
clustered_data <- pheatmap(scaled_data, 
                           cluster_rows = TRUE
                           show_colnames = FALSE
                           main = "Customized Heatmap",
                           color = colorRampPalette(c("#FFEDA0", "#F03B20"))(100),
                           fontsize_row = 8,
                           fontsize_col = 10,
                           cellwidth = 25,
                           cellheight = 15,
                           border_color = NA,
                           kmeans_k = 4,
                           legend = TRUE,
                           legend_breaks = seq(-2, 2, by = 0.5),
                           legend_labels = c("-2.0", "-1.5", "-1.0", "-0.5",
                                             "0.0", "0.5", "1.0", "1.5", "2.0"),
                           fontsize_legend = 12)
  
# Save the heatmap
dev.copy(png, "mtcars_heatmap.png", width = 1000, height = 600)
dev.off()


Output:

Customizing Heatmap in R

Customized Heatmap



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads