Search⌘ K
AI Features

Heatmaps

Explore how to create heatmaps in ggplot2 by transforming data into long format and applying geom_tile for visualization. Understand customizing color gradients, tile borders, and adding numeric labels to effectively represent data patterns and trends.

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:

R
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
...