How To Make Heatmaps in R with ComplexHeatmap?
Last Updated :
17 Oct, 2021
Heatmap is a Visualization technique in the form of a matrix. It is mainly used for analyzing the numeric feature in a dataset and to visualize the trend over the data. This technique can highlight the range of values using color intensity i.e it indicates higher values with brighter colors and the color gradually fades out for smaller values.
In this article, we will see how to plot heatmaps using the Heatmap( ) function of ComplexHeatmap Package in R programming language.
Installation
To create heatmaps using ComplexHeatmap it has to be first installed and loaded into the working space.
Syntax:
# installing the package
install.packages("ComplexHeatmap")
library(ComplexHeatmap)
Heatmap
To create a heatmap using this module a simple heatmap() function is used with appropriate parameters.
Syntax:
Heatmap(m, col = c(), cluster_columns = FALSE, cluster_rows = FALSE, rect_gp = gpar() … )
Parameters:
- m : matrix or vector
- cluster_columns : specify clusters for columns
- cluster_rows : specify clusters for rows
- rect_gp : to add gap spaces to heatmap
Example: Simple Heatmap
R
library (ComplexHeatmap)
set.seed (2020)
d1 <- runif (500, 0, 1)
m<- matrix (d1,nrow = 20,ncol=10)
m<-m[, sample ( ncol (m))]
Heatmap (m)
|
Output:
Customizing colors of a Heatmap
To change the color of the heatmap col parameter is used with the values to change the color with.
Example: Changing color of a heatmap
R
library (ComplexHeatmap)
set.seed (2020)
d1 <- runif (500, 0, 1)
m<- matrix (d1,nrow = 20,ncol=10)
m<-m[, sample ( ncol (m))]
Heatmap (m,col = c ( '#b3ffff' , '#006600' , '#ffffb3' ))
|
Output:
Heatmap with Column Clusters
We assign cluster_rows as FALSE to remove row clusters.
Example: Removing row clusters
R
library (ComplexHeatmap)
set.seed (2020)
d1 <- runif (500, 0, 1)
m<- matrix (d1,nrow = 20,ncol=10)
m<-m[, sample ( ncol (m))]
Heatmap (m,cluster_rows = FALSE ,col = c ( '#1a53ff' , '#ffcccc' ))
|
Output:
Heatmap with Row Clusters
We assign cluster_columns as FALSE to remove column clusters.
Example: Heatmap with row clusters
R
library (ComplexHeatmap)
set.seed (2020)
d1 <- runif (500, 0, 1)
m<- matrix (d1,nrow = 20,ncol=10)
m<-m[, sample ( ncol (m))]
Heatmap (m,cluster_columns = FALSE ,
rect_gp = gpar (col = "white" , lwd = 2),
col = rev ( rainbow (10)))
|
Output:
Heatmap with no clusters
Let’s visualize a heatmap with no clusters. For this cluster_rows and cluster_columns are set to FALSE.
Example: Heatmap with no clusters
R
library (ComplexHeatmap)
set.seed (2020)
d1 <- runif (500, 0, 1)
m<- matrix (d1,nrow = 20,ncol=10)
m<-m[, sample ( ncol (m))]
Heatmap (m,cluster_rows = FALSE ,cluster_columns = FALSE )
|
Output:
Combine two Heatmap together
Let’s add 2 heatmaps and visualize them together. For this plots are created independently and then joined together.
Example: Combining two heatmap together
R
library (ComplexHeatmap)
set.seed (2020)
d1 <- runif (500, 0, 1)
m<- matrix (d1,nrow = 20,ncol=10)
m<-m[, sample ( ncol (m))]
h1<- Heatmap (m,col = c ( '#b3ffff' , '#006600' , '#ffffb3' ))
h2<- Heatmap (m,cluster_rows = FALSE ,col = c ( '#1a53ff' , '#ffcccc' ))
h1+h2
|
Output:
Share your thoughts in the comments
Please Login to comment...