Heatmaps

Learn how to build and customize heatmaps in ggplot2.

Introduction to heatmaps

Heatmaps are graphical representations of data where the values are represented as colors. They are a valuable tool for visualizing and understanding large datasets, as they can help identify patterns and trends in the data. In ggplot2, heatmaps can be created using the geom_tile() function.

Let’s first create a numerical matrix in R using the rnorm function, as shown in the code below:

Press + to interact
set.seed(123)
m <- matrix(rnorm(50), nrow = 5, ncol = 5)
colnames(m) <- c("A", "B", "C", "D", "E")
rownames(m) <- c("P", "Q", "R", "S", "T")
m
  • Line 1: We set the seed parameter as 123 to reproduce the same set of random numbers every time we run the code.
  • Line 2: We create a matrix named m using the matrix() function with 55 rows and 55 columns specified by the arguments nrow=5 and ncol=5, respectively. We generate 50
...