| Authors | Rasouli, H., Moein, F |
| Date | December 20, 2025 |
| Affiliation | ABSA, Tarbiat Modares University, Tehran, Iran |
| Workshop | R club for researchers |
Master clustered heatmap visualization in under 60 minutes! This tutorial provides complete, ready-to-use code examples that you can immediately apply to your own datasets. Whether you’re analyzing biological data, financial metrics, survey responses, or any multivariate dataset, these techniques will accelerate your data exploration workflow.
# Installation
install.packages("heatmaply")
# Open its library
library(heatmaply)
data <- mtcars
head(data, 4)
print(data)
head () function outputOutput:
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
print() function output mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
# Basic heatmap
heatmaply(data)
Output: An interactive heatmap with default colors showing values of each variable.
heatmaply_cor()# Plot correlation relationship between the input dataset
heatmaply_cor(
cor(mtcars),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2
)
cor(): Input correlation matrixxlab = "features": Title of X axis
(string format)ylab = "type": Title of Y axis (string
format)k_col = 2 and k_row = 2:
Number of clusters in each column and rowOutput: A correlation heatmap with specific colors
| Number of Variables | Clustering Type | Recommended k |
|---|---|---|
| 10-12 variables | Simple clustering | k = 2 or 3 |
| 12-20 variables | Balanced detail/simplicity | k = 3 or 4 |
| > 20 variables | More detailed grouping | k = 4 to 6 |
Before dealing with this topic, you should know some information about colors
You can use this online color picker to get a wide range of beautiful
colors.
Color vision deficiency, commonly called color blindness, is the decreased ability to see color differences. Most types are inherited, affect more men than women, and involve difficulties distinguishing between specific colors, most commonly reds and greens.
Distinguishing greens from reds and some shades of gray.
Distinguishing reds from greens and also from blues.
Distinguishing blues from yellows and violets from reds.
Seeing any color at all; vision is in shades of black, white, and
gray.
# Three-color gradient
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = c("blue", "purple", "red") # blue → purple → red
)
Output: A heatmap with blue-purple-red gradient
# Alternative color combination
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = c("green", "pink", "red")
)
Output: A heatmap with green-pink-red gradient
# Example using RdYlBu
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colorscale = "RdYlBu" # Red-Yellow-Blue
)
Output: A heatmap with RdYlBu palette
# Install and load RColorBrewer
install.packages("RColorBrewer")
library(RColorBrewer)
# Example 1: Spectral palette
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = brewer.pal(11, "Spectral")
)
Output: A heatmap with Spectral palette
# Example 2: YlOrRd palette
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = brewer.pal(9, "YlOrRd")
)
Output: A heatmap with YlOrRd palette:
# Example 3: YlGn palette
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = brewer.pal(5, "YlGn")
)
Output: A heatmap with YlGn palette
| Palette | Max Colors | Category | Colorblind Safe |
|---|---|---|---|
| BrBG | 11 | div | TRUE |
| PiYG | 11 | div | TRUE |
| PRGn | 11 | div | TRUE |
| PuOr | 11 | div | TRUE |
| RdBu | 11 | div | TRUE |
| RdGy | 11 | div | FALSE |
| RdYlBu | 11 | div | TRUE |
| RdYlGn | 11 | div | FALSE |
| Spectral | 11 | div | FALSE |
| Blues | 9 | seq | TRUE |
| BuGn | 9 | seq | TRUE |
| YlGn | 9 | seq | TRUE |
| YlOrBr | 9 | seq | TRUE |
| YlOrRd | 9 | seq | TRUE |
# Install and load viridis
install.packages("viridis")
library(viridis)
# Example with plasma
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = plasma(100)
)
Output: A heatmap with plasma palette
# Example with turbo
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = turbo(100)
)
Output:
A heatmap with Turbo palette
# Example with inferno
heatmaply_cor(
cor(data),
xlab = "Features",
ylab = "Features",
k_col = 2,
k_row = 2,
colors = inferno(100)
)
Output: A heatmap with inferno palette
# Calculates Pearson correlation coefficients between all variables
# r is an 11×11 matrix of correlation values (-1 to 1)
r <- cor(mtcars)
print(round(r, 3))
Output:
mpg cyl disp hp drat wt qsec vs am gear carb
mpg 1.000 -0.852 -0.848 -0.776 0.681 -0.868 0.419 0.664 0.600 0.480 -0.551
cyl -0.852 1.000 0.902 0.832 -0.700 0.782 -0.591 -0.811 -0.523 -0.493 0.527
disp -0.848 0.902 1.000 0.791 -0.710 0.888 -0.434 -0.710 -0.591 -0.556 0.395
hp -0.776 0.832 0.791 1.000 -0.449 0.659 -0.708 -0.723 -0.243 -0.126 0.750
drat 0.681 -0.700 -0.710 -0.449 1.000 -0.712 0.091 0.440 0.713 0.700 -0.091
wt -0.868 0.782 0.888 0.659 -0.712 1.000 -0.175 -0.555 -0.692 -0.583 0.428
qsec 0.419 -0.591 -0.434 -0.708 0.091 -0.175 1.000 0.745 -0.230 -0.213 -0.656
vs 0.664 -0.811 -0.710 -0.723 0.440 -0.555 0.745 1.000 0.168 0.206 -0.570
am 0.600 -0.523 -0.591 -0.243 0.713 -0.692 -0.230 0.168 1.000 0.794 0.058
gear 0.480 -0.493 -0.556 -0.126 0.700 -0.583 -0.213 0.206 0.794 1.000 0.274
carb -0.551 0.527 0.395 0.750 -0.091 0.428 -0.656 -0.570 0.058 0.274 1.000
# Creates a matrix of p-values from correlation tests
# Uses cor.test() to test if each correlation is statistically significant
# outer() applies the test to all variable pairs
# Returns a matrix where each cell is the p-value for that correlation pair
cor.test.p <- function(x){
FUN <- function(x, y) cor.test(x, y)[["p.value"]]
z <- outer(
colnames(x),
colnames(x),
Vectorize(function(i,j) FUN(x[,i], x[,j]))
)
dimnames(z) <- list(colnames(x), colnames(x))
z
}
# Applies the function to mtcars
# p is an 11×11 matrix of p-values (0 to 1)
# Small p-value (< 0.05) means correlation is statistically significant
p <- cor.test.p(mtcars)
print(round(p, 4))
Sample Output:
mpg cyl disp hp drat wt qsec vs am gear carb
mpg 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0171 0.0000 0.0002 0.0054 0.0011
cyl 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0004 0.0000 0.0022 0.0042 0.0019
disp 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0131 0.0000 0.0004 0.0010 0.0250
hp 0.0000 0.0000 0.0000 0.0000 0.0090 0.0000 0.0000 0.0000 0.1800 0.4930 0.0000
drat 0.0000 0.0000 0.0000 0.0090 0.0000 0.0000 0.6190 0.0134 0.0000 0.0000 0.6220
wt 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.3380 0.0009 0.0000 0.0003 0.0134
qsec 0.0171 0.0004 0.0131 0.0000 0.6190 0.3380 0.0000 0.0000 0.2050 0.2420 0.0000
vs 0.0000 0.0000 0.0000 0.0000 0.0134 0.0009 0.0000 0.0000 0.3380 0.2570 0.0010
am 0.0002 0.0022 0.0004 0.1800 0.0000 0.0000 0.2050 0.3380 0.0000 0.0000 0.7540
gear 0.0054 0.0042 0.0010 0.4930 0.0000 0.0003 0.2420 0.2570 0.0000 0.0000 0.1290
carb 0.0011 0.0019 0.0250 0.0000 0.6220 0.0134 0.0000 0.0010 0.7540 0.1290 0.0000
heatmaply_cor(
r,
node_type = "scatter",
point_size_mat = -log10(p),
point_size_name = "-log10(p-value)",
label_names = c("x", "y", "Correlation"),
main = "Functional Correlation Plot with Significance"
)
Output:
A Scatterplot Heatmap
If we assume that all variables follow a normal distribution,
standardizing them—by subtracting the mean and dividing by the standard
deviation—would align them closely with the standard normal
distribution. In this standardized form, each data point expresses its
distance from the mean in terms of standard deviation units. The
scale parameter in heatmaply enables scaling
along columns, rows, or both, and can be implemented as described
below.
node_type = "scatter"point_size_mat = -log10(p)-log10(p-value)
-log10(0.05) = 1.30-log10(0.01) = 2.00-log10(0.001) = 3.00This tutorial covers:
heatmaply_cor() to display correlation
matricesxlab and
ylabk_col and k_row for groupingcolors = c("color1", "color2", "color3")brewer.pal()node_type = "scatter"The heatmaply package provides a powerful tool for
interactive visualization of relationships in data and can be used at
various stages of data analysis.
| Session Part | Filename | Direct Link |
|---|---|---|
| Part 1 | part-1-R452-2025.mp4 | 📥 Download |
| Part 2 | Part-2-2.mp4 | 📥 Download |
| Part 3 | Part-3.mp4 | 📥 Download |
| Part 4 | par-4.mp4 | 📥 Download |
File Format: All videos are provided in the MP4 format. This is a universal container format commonly used for streaming and storing digital video and audio, ensuring broad compatibility with media players.