Plotting heatmaps with ggplot2

Heatmaps are representations of matrix data where the individual values contained are represented as colors. Heatmaps can be realized in ggplot2 using the traditional functions available, but we have also included plots in this chapter that represent very specific type of graphs.

In order to realize a heatmap with ggplot2, you will simply need to use the geom_tile() function. These plots can be realized usually by representing two variables on both axes and the combinations between these variables are color-mapped using a third variable. Let's first create a simple dataset that we can use.

x1 <- seq(-10, 10, length.out = 10)
y1 <- seq(-10, 10, length.out = 10)
d1 <- expand.grid(x = x1, y = y1)

d1$z <- d1$x^2 - d1$y^2 ...

Get ggplot2 Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.