![]() |
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 RThe 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:
Creating a Simple Heatmap in RFirst, 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
Output: ![]() Default Heatmap Heatmap with Row Clusters in RTo 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
Output: ![]() Heatmap with Row clusters Heatmap with Column Clusters in RTo 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
Output: ![]() Heatmap with Column Clusters Heatmap cut into pieces in RTo 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
Output: ![]() Heatmap divided into pieces Customizing Heatmap in RThe 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:
Finally, we used the dev.copy() and dev.off() functions to save the heatmap as a PNG file. R
Output: ![]() Customized Heatmap |
Reffered: https://www.geeksforgeeks.org
AI ML DS |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |