How To Make Heatmaps in R with ComplexHeatmap? - GeeksforGeeks

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.
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)
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)  # code to create a simple heatmapset.seed(2020)  # generate random numbersd1 <- runif(500, 0, 1)m<-matrix(d1,nrow = 20,ncol=10)  # randomize the data pointsm<-m[,sample(ncol(m))]  Heatmap(m)
Output:
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)  # code to create a simple heatmapset.seed(2020)  # generate random numbersd1 <- runif(500, 0, 1)m<-matrix(d1,nrow = 20,ncol=10)  # randomize the data pointsm<-m[,sample(ncol(m))]  # heatmap with custom colors Heatmap(m,col = c('#b3ffff','#006600','#ffffb3'))
Output:
We assign cluster_rows as FALSE to remove row clusters.
Example: Removing row clusters
R
library(ComplexHeatmap)  # code to create a simple heatmapset.seed(2020)  # generate random numbersd1 <- runif(500, 0, 1)m<-matrix(d1,nrow = 20,ncol=10)  # randomize the data pointsm<-m[,sample(ncol(m))]  # heatmap with col clustersHeatmap(m,cluster_rows = FALSE,col = c('#1a53ff','#ffcccc'))
Output:
We assign cluster_columns as FALSE to remove column clusters.
Example: Heatmap with row clusters
R
library(ComplexHeatmap)  # code to create a simple heatmapset.seed(2020)  # generate random numbersd1 <- runif(500, 0, 1)m<-matrix(d1,nrow = 20,ncol=10)  # randomize the data pointsm<-m[,sample(ncol(m))]  # heatmap with row clustersHeatmap(m,cluster_columns = FALSE,        rect_gp = gpar(col = "white", lwd = 2),        col = rev(rainbow(10)))
Output:
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)  # code to create a simple heatmapset.seed(2020)  # generate random numbersd1 <- runif(500, 0, 1)m<-matrix(d1,nrow = 20,ncol=10)  # randomize the data pointsm<-m[,sample(ncol(m))]  # heatmap with no clustersHeatmap(m,cluster_rows = FALSE,cluster_columns = FALSE)
Output:
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)  # code to create a simple heatmapset.seed(2020)  # generate random numbersd1 <- runif(500, 0, 1)m<-matrix(d1,nrow = 20,ncol=10)  # randomize the data pointsm<-m[,sample(ncol(m))]  # heatmap 1h1<-Heatmap(m,col = c('#b3ffff','#006600','#ffffb3'))  # heatmap 2h2<-Heatmap(m,cluster_rows = FALSE,col = c('#1a53ff','#ffcccc'))  # add to combine 2 heatmapsh1+h2
Output:
Picked
R-Charts
R-Graphs
R-Packages
R-plots
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to import an Excel File into R ?
R - if statement
Time Series Analysis in R
Plot mean and standard deviation using ggplot2 in R