Show the code
# View the structure of the volcano data
dim(volcano)[1] 87 61
A heatmap is a data visualization technique that displays values in a matrix format using color to represent magnitude. Heatmaps are particularly useful when you want to:
In ggplot2, we create heatmaps using the geom_tile() function, which draws rectangular tiles and fills them with color based on a third variable.
The geom_tile() function is the core of heatmap creation in ggplot2. It requires three aesthetics:
| Aesthetic | Description |
|---|---|
x |
The variable for the horizontal axis |
y |
The variable for the vertical axis |
fill |
The variable that determines the tile color |
Optional arguments include:
color - the border color of each tilelinewidth - the thickness of the tile bordersThis function applies the viridis color palette to continuous data. Viridis is popular because it is:
Key arguments:
| Argument | Description |
|---|---|
option |
The color palette variant: “magma”, “inferno”, “plasma”, “viridis”, “cividis”, “rocket”, “mako”, or “turbo” |
name |
The title for the legend |
direction |
Set to -1 to reverse the color scale |
This function fixes the aspect ratio of the plot, ensuring that one unit on the x-axis is the same length as one unit on the y-axis. This is important for spatial data where the x and y axes represent real-world coordinates.
We will use the volcano dataset, which is built into R. This dataset contains elevation data for Maunga Whau, a volcanic cone in Auckland, New Zealand. The data is stored as an 87 × 61 matrix, where each cell represents the elevation (in meters) at a specific grid location.
# View the structure of the volcano data
dim(volcano)[1] 87 61
The volcano data is a matrix, but ggplot2 requires data in a “long” format (a data frame with one row per observation). We need to convert it.
library(tidyverse)
volcano_df <- as.data.frame(volcano) %>%
mutate(X = row_number()) %>%
pivot_longer(
cols = starts_with("V"),
names_to = "Y",
names_prefix = "V",
values_to = "Elevation"
) %>%
mutate(
X = as.numeric(X),
Y = as.numeric(Y)
)volcano matrix into a standard data frame so it can be used with dplyr and tidyr functions.
X based on the row index to preserve the horizontal coordinate.
X and Y coordinate columns from integers or strings into numeric doubles for plotting.
ggplot(volcano_df, aes(x = Y, y = X, fill = Elevation)) +
geom_tile()ggplot() and define the aesthetics using aes(). The x aesthetic maps to our Y column (east-west position), the y aesthetic maps to our X column (north-south position), and the fill aesthetic maps to Elevation. The fill aesthetic tells ggplot2 which variable should control the color of each tile.
geom_tile() function draws the heatmap. Each tile represents one cell from our original matrix. The position of the tile is determined by the x and y aesthetics, and the color is determined by the fill aesthetic.
ggplot(volcano_df, aes(x = Y, y = X, fill = Elevation)) +
geom_tile() +
scale_fill_viridis_c(option = "magma")scale_fill_viridis_c() function replaces the default blue color gradient with the viridis color palette. The “c” in the function name stands for “continuous”, meaning it works with numeric data. We use option = "magma" to select a warm color palette that transitions from dark purple (low values) through pink and orange to bright yellow (high values). This makes the elevation differences more visually striking.
ggplot(volcano_df, aes(x = Y, y = X, fill = Elevation)) +
geom_tile() +
scale_fill_viridis_c(
option = "magma",
name = "Elevation\n(meters)"
) +
labs(
title = "Maunga Whau Volcano Topography",
x = "East-West Grid Line",
y = "North-South Grid Line"
)name argument inside scale_fill_viridis_c() sets the legend title. The \n creates a line break, so “Elevation” appears on one line and “(meters)” appears below it. This keeps the legend compact while providing complete information about what the colors represent.
labs() function adds labels to the plot. We provide a descriptive title and clearer axis labels that explain what the grid lines represent.
ggplot(volcano_df, aes(x = Y, y = X, fill = Elevation)) +
geom_tile() +
scale_fill_viridis_c(
option = "magma",
name = "Elevation\n(meters)"
) +
labs(
title = "Maunga Whau Volcano Topography",
x = "East-West Grid Line",
y = "North-South Grid Line"
) +
theme_minimal() +
theme(panel.grid = element_blank()) +
coord_fixed()\n to create a line break for better formatting.