R Graphics · Heatmap

Revealing Matrix Patterns with Heatmaps in R

Learn how color scales, annotations, clustering, labels, and normalization turn matrices into interpretable heatmaps.

6 examplesReproducible R code
Before you begin

Choose the variation that serves the analysis

Heatmaps encode numeric matrices with color. Careful ordering and scaling can expose blocks, gradients, correlations, and clusters that are difficult to detect numerically.

Examples
06
Levels
Beginner + Intermediate
01
Example 01Base RBeginner

Create a Basic Heatmap

A basic heatmap maps every matrix cell to a color.

Create a Basic Heatmap example generated in R
R-generated example output.

Use image() for a direct matrix display and choose a sequential palette for values that progress from low to high.

RRun this code
m <- as.matrix(mtcars[1:10, 1:6])
image(t(m[nrow(m):1, ]), axes = FALSE,
      col = colorRampPalette(c("#eff6ff", "#2563eb", "#172554"))(40))
02
Example 02Base RBeginner

Visualize a Correlation Matrix

A correlation heatmap displays the strength and direction of pairwise linear relationships.

Visualize a Correlation Matrix example generated in R
R-generated example output.

A diverging palette centered on zero distinguishes negative, neutral, and positive correlations.

RRun this code
m <- cor(mtcars)
heatmap(m, Rowv = NA, Colv = NA, scale = "none",
        col = colorRampPalette(c("#dc2626", "white", "#2563eb"))(50))
03
Example 03Base RBeginner

Add Row and Column Labels

Labels identify the observations and variables represented by each cell.

Add Row and Column Labels example generated in R
R-generated example output.

Keep labels short and rotate or reduce them when the matrix is large so the color pattern remains dominant.

RRun this code
m <- as.matrix(mtcars[1:8, 1:5])
heatmap(m, Rowv = NA, Colv = NA, scale = "column",
        labRow = rownames(m), labCol = colnames(m),
        col = hcl.colors(30, "Blues 3"))
04
Example 04Base RIntermediate

Cluster Similar Rows and Columns

A clustered heatmap reorders rows and columns using hierarchical clustering.

Cluster Similar Rows and Columns example generated in R
R-generated example output.

The dendrograms show which profiles are similar, while the reordered matrix reveals coherent blocks.

RRun this code
m <- as.matrix(scale(mtcars[, 1:7]))
heatmap(m, scale = "none",
        col = colorRampPalette(c("#dc2626", "white", "#2563eb"))(50))
05
Example 05Base RIntermediate

Standardize Variables Before Coloring

Standardization prevents variables with large numeric ranges from dominating the colors.

Standardize Variables Before Coloring example generated in R
R-generated example output.

scale() expresses every column in standard-deviation units, making relative high and low values comparable.

RRun this code
m <- scale(mtcars[1:12, 1:7])
heatmap(m, Rowv = NA, Colv = NA, scale = "none",
        col = hcl.colors(40, "Blue-Red 3"))
06
Example 06Base RIntermediate

Print Values Inside Cells

Cell annotations combine color-based pattern recognition with exact numerical values.

Print Values Inside Cells example generated in R
R-generated example output.

Use text() over an image plot for small matrices. For large matrices, labels quickly become cluttered.

RRun this code
m <- round(cor(iris[, 1:4]), 2)
image(1:4, 1:4, m, col = hcl.colors(30, "Blues 3"), axes = FALSE)
axis(1, 1:4, colnames(m)); axis(2, 1:4, rownames(m))
text(row(m), col(m), labels = m, font = 2)