pheatmap function in R

Package

pheatmap

Author

Raivo Kolde

The pheatmap function

The pheatmap function is similar to the default base R heatmap, but provides more control over the resulting plot. You can pass a numeric matrix containing the values to be plotted.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m)

pheatmap function in R

Normalization

If the values of the matrix are not normalized you can normalize them by rows ("row") or by columns ("column") making use of the scale argument.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, scale = "column")

Heat map normalization

Values

If you set display_numbers = TRUE the values for each cell will be shown. You can also modify the color and the size of the text.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m,
         display_numbers = TRUE,
         number_color = "black", 
         fontsize_number = 8)

Heat map in R with numbers for each cell

Number of clusters

The number of clusters can be changed with kmeans_k. If the number of clusters is small you can increase the size of the cells with cellheight or cellwidth.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, kmeans_k = 3, cellheight = 50)

pheatmap clustering kmeans

Clustering

Remove rows dendrogram pheatmap

Remove rows dendrogram

You can pass a hclust object to the cluster_rows argument or set it to FALSE to remove the rows dendrogram.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, cluster_rows = FALSE)

Remove columns dendrogram pheatmap

Remove columns dendrogram

Equivalently to the previous argument, cluster_cols controls how the columns dendrogram should be plotted or if not plot them at all.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, cluster_cols = FALSE)

Remove all dendrograms pheatmap

Remove dendrograms

It is possible to remove both dendrograms setting cluster_cols and cluster_rows to FALSE.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m,
         cluster_cols = FALSE,
         cluster_rows = FALSE)

Color customization

Border color

The border_color controls the border of the cells. The default value is "grey60".

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, border_color = "black")

pheatmap function border color of the cells

Color palette

The default color palette can be changed passing a vector of colors to the color argument, as in the example below.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, color = hcl.colors(50, "BluYl"))

pheatmap color palette

Legend customization

Legend breaks heat map in R

Legend breaks

The breaks of the legend can be customized with legend_breaks, passing the desired values as a vector.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, legend_breaks = c(-2, 0, 2))

Legend labels R heat map

Legend labels

If you set the breaks you can also pass a vector of the same length to modify the labels to the legend_labels argument.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m,
         legend_breaks = c(-2, 0, 2),
         legend_labels = c("Low", "Medium", "High"))

Remove the legend in pheatmap function

Remove the legend

Finally, if you want to get rid of the legend set legend = FALSE.

# install.packages("pheatmap")
library(pheatmap)

# Data 
set.seed(8)
m <- matrix(rnorm(200), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Heat map
pheatmap(m, legend = FALSE)
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
Storytelling with Data

A Data Visualization Guide for Business Professionals

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also