• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How To Make Heatmaps in R with ComplexHeatmap?

datavizpyr · October 8, 2020 ·

Heatmaps are a great way visualize a numerical dataset in a matrix form. Basically, a heatmap shows the actual data values as colors. When there is a broad trend in data, like change in data over rows or columns of data, a heat map makes it easy to see the broader trend.

In this tutorial, we will see how to make simple heatmaps using ComplexHeatmap, a R package that lets users to make complex heatmaps easily, combine multiple heatmaps and annotate them to make them easy to interpret.

Let us first install ComplexHeatmap package.

install.packages("ComplexHeatmap")

Here we load the package after making sure it is installed.

library(ComplexHeatmap)

In order to make simple heatmaps, we will simulate data matrix by using random numbers.

set.seed(2020)
n_row <- 30
n_col <- 10
data_matrix1 <- matrix(rnorm(n_row*n_col),ncol=n_col)

Heatmap() function in ComplexHeatmap Package

The data matrix we created does not really have any pattern, but simply random numbers from normal distribution. Let us use ComplexHeatmap package to visualize the data matrix. Heatmap() function with capital “H” is the main function for making heatmaps in ComplexHeatmap package.

Heatmap(data_matrix1)

By default, Heatmap() function clusters columns and rows and makes a heatmap. Also chooses a color palette automatically to show the data as heatmap.

Simple Heatmap with ComplexHeatmap Package
Simple Heatmap with ComplexHeatmap Package

Let us add some structure to our data matrix. To add some patterns to the data matrix, we update certain column values so that they are similar to each other. In the example below, we make half the columns to be sampled from normal distribution with mean 50 and the other half to have the mean 70. We also shuffle the columns, so the pattern in the data is not ordered.

set.seed(2020)
data_matrix2 <- matrix(rnorm(n_row*n_col),ncol=n_col)
# add structure to data
data_matrix2[,1:(n_col/2)] <- matrix(rnorm(n_row*n_col/2,mean=50,sd=5),
                            ncol=n_col/2)
data_matrix2[,((n_col/2)+1):n_col] <- matrix(rnorm(n_row*n_col/2,mean=70,sd=5),
                                    n_col/2)
# shuffle the columns
data_matrix2 <- data_matrix2[,sample(ncol(data_matrix2))]
# name columns and rows
colnames(data_matrix2) <- paste0("S",seq(1,n_col))
rownames(data_matrix2) <- paste0("f",seq(1,n_row))

How to Make Heatmaps without clustering columns and rows?

Now that we have a data matrix with some pattern in it, let us use Heatmap() and make a heatmap. This time we want to show the data matrix as it is, without clustering columns or rows.

Heatmap(data_matrix2,
         cluster_rows = FALSE, 
         cluster_columns = FALSE)
Heatmap with ComplexHeatmap without Clustering
Heatmap with ComplexHeatmap without Clustering

Heatmaps without clustering rows

Our simulated data has patterns along the columns, bit not along rows. So, let us make a heatmap without cluster rows, but clustering columns.

Heatmap(data_matrix2,
        name = "value",
        cluster_rows = FALSE)
Heatmap with ComplexHeatmap with Column Clustering
Heatmap with ComplexHeatmap with Column Clustering

How To Combine Two Heatmaps with ComplexHeatmap?

ComplexHeatmap also enables to combine multiple heatmaps together in a single plot. In this example below, we will combine two heatmaps, one before clustering columns and the one after clustering columns. We also specify title for each heatmap with “column_title” argument.

# first heatmap 
h1 <- Heatmap(data_matrix2, 
              cluster_rows =FALSE,
              cluster_columns=FALSE,
              name="before",
              show_heatmap_legend=FALSE,
              column_title = "Before clustering")
# second heatmap
h2 <-   Heatmap(data_matrix2, 
                cluster_rows = FALSE,
                name="values",
                column_title = "After clustering")

Note that, we would like to use one common legend for our two heatmaps in the combined plot. We did that using the argument “show_heatmap_legend” while making the first heatmap. Let us combine the two heatmaps. We can place them side by side using + operator.

# Combine two heatmaps
h1+h2

Now we have a single plot with two heatmaps side-by-side.

Combine Two Heatmaps with Common legend
Combine Two Heatmaps with Common legend in R

Related posts:

How to Make Heatmap with ggplot2?How To Make Simple Heatmaps with ggplot2 in R? Heatmap from Matrix with ggplot2Heatmap from Matrix using ggplot2 in R Customizing annotation to heatmap with ggplot2How to add text annotation to heatmap with ggplot2 Stacked Barplots Side By Side with ggplot2 in RHow to Make Horizontal Stacked Barplots with ggplot2 in R?

Filed Under: Heatmaps in R, Heatmaps with ComplexHeatmap, R Tagged With: Heatmap ComplexHeatmap, Heatmaps in R

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version