In this tutorial, we will see how to create and rotate 4 types of heatmaps in R with the ggplot2 package. In heatmaps, the diagonal line is always 1, which is not informative. Moreover, the plot is symmetrical so the upper part gives the same information as the lower part. Therefore, we can plot 4 types of heatmaps, depending if we keep the diagonal line and the lower part or not.
Load the ggplot2 package
library(ggplot2)
Variables assignment
First, we will simulate a dataset with the rnorm function:
set.seed(50)
data = data.frame(col1 = rnorm(50), col2 = rnorm(50), col3 = rnorm(50),
col4 = rnorm(50), col5 = rnorm(50), col6 = rnorm(50),
col7 = rnorm(50), col8 = rnorm(50), col9 = rnorm(50))
columns = names(data)
n = length(columns)
How to make heatmaps
To create 4 types of heatmaps, we will compute the correlation between pairs of columns and use permutations and combinations. If you are not familiar with them, please read my post about Permutations and combinations in R using iteration.
1) Permutations with repetition
In this heatmap, we will keep the diagonal line and the lower part:
results = NULL
for (i in 1:n){
for (j in 1:n){
counts = cor(data[, columns[i]], data[, columns[j]])
results = rbind(results, data.frame(i = columns[i], j = columns[j], counts))
}
}
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
2) Permutations without repetition
In this heatmap, we will keep the lower part but remove the diagonal line:
results = NULL
for (i in 1:n){
for (j in setdiff(1:n, i)){
counts = cor(data[, columns[i]], data[, columns[j]])
results = rbind(results, data.frame(i = columns[i], j = columns[j], counts))
}
}
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
3) Combinations with repetition
In this heatmap, we will keep the diagonal line but remove the lower part:
results = NULL
for (i in 1:n){
for (j in i:n){
counts = cor(data[, columns[i]], data[, columns[j]])
results = rbind(results, data.frame(i = columns[i], j = columns[j], counts))
}
}
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
4) Combinations without repetition
In this heatmap, we will remove the diagonal line and the lower part:
results = NULL
for (i in 1:n){
for (j in setdiff(i:n, i)){
counts = cor(data[, columns[i]], data[, columns[j]])
results = rbind(results, data.frame(i = columns[i], j = columns[j], counts))
}
}
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
How to rotate heatmaps
As an example, we will use data from combinations with replacement:
results = NULL
for (i in 1:n){
for (j in i:n){
counts = cor(data[, columns[i]], data[, columns[j]])
results = rbind(results, data.frame(i = columns[i], j = columns[j], counts))
}
}
To rotate heatmaps in ggplot2, we will use factors and change the order of levels in these factors according to the desired orientation.
1) Top left orientation
In this heatmap, we will use an increasing order of the columns for both axes:
results[, "i"] = factor(results[, "i"], levels = columns)
results[, "j"] = factor(results[, "j"], levels = columns)
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
2) Top right orientation
In this heatmap, we will use a decreasing order of the columns for the x axis and an increasing order of the columns for the y axis:
results[, "i"] = factor(results[, "i"], levels = rev(columns))
results[, "j"] = factor(results[, "j"], levels = columns)
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
3) Bottom right orientation
In this heatmap, we will use a decreasing order of the columns for both axes:
results[, "i"] = factor(results[, "i"], levels = rev(columns))
results[, "j"] = factor(results[, "j"], levels = rev(columns))
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
4) Bottom left orientation
In this heatmap, we will use an increasing order of the columns for the x axis and a decreasing order of the columns for the y axis:
results[, "i"] = factor(results[, "i"], levels = columns)
results[, "j"] = factor(results[, "j"], levels = rev(columns))
ggplot(results, aes_string(x = "i", y = "j", fill = "counts"))+
geom_tile()+
scale_fill_gradient(limit = c(-0.3, 1))
Conclusion
In conclusion, we can easily create 4 types of heatmaps in R using ggplot2, based on permutations and combination, with and without replacement. We can also rotate them by reversing or not the column names in the factor function. Is this post helpful to you?