In this post, we will learn how to make a heatmap in R. A heatmap is a graphical representation of data where different colors show the values. Heatmaps can help us visualize our data’s complex patterns, trends, and correlations. They are instrumental in psychological research, where we often deal with large and multidimensional datasets. For example, we can use heatmaps to explore the relationships between psychological variables, such as personality traits, emotions, attitudes, or behaviors. We can also use heatmaps to compare different groups of participants, such as clinical and non-clinical populations or experimental and control conditions.
Table of Contents
Preparing the Data
To create a heatmap in R, we need a matrix or a dataframe of numerical values. In this post, we will use a sample dataset from the psych package, which contains the scores of 300 participants on nine personality scales: Neuroticism, Extraversion, Openness, Agreeableness, Conscientiousness, Masculinity, Femininity, Lie, and Paranoia. To load the data and the package, we can run the following code:
# load the package
library(psych)
# load the data
data(bfi)
Code language: R (r)
The data frame bfi has 300 rows and 28 columns, but we only need the personality scores columns. Here, we subset the data:
# Select only the personality item columns
bfi_traits <- bfi %>%
select(A1:A5, C1:C5, E1:E5, N1:N5, O1:O5)
# Remove rows with any missing values in the selected columns
bfi_clean <- na.omit(bfi_traits)
Code language: CSS (css)
In the above code chunk, we selected columns by name in R using dplyr and removed any rows with missing values to ensure we could compute correlations without error. Now that we have a clean dataset containing only the personality trait items, we can calculate a correlation matrix and visualize it using a heatmap.
# Compute the correlation matrix
cor_matrix <- cor(bfi_clean)
Code language: R (r)
In the code example above, we computed the correlation matrix for the cleaned bfi
dataset using the default Pearson method. For more information about how to carry out correlation and how to create a correlation matrix in R:
- Correlation in R: Coefficients, Visualizations, & Matrix Analysis
- Create a Correlation Matrix in Python with NumPy and Pandas
- Report Correlation in APA Style using R: Text & Tables
Creating the Heatmap in R
To create a heatmap using ggplot2
, we need to convert the correlation matrix to a long format. We will also remove the upper triangle to reduce redundancy in the plot.
Step 1: Transforming data
# Convert the correlation matrix to long format
cor_long <- as.data.frame(as.table(cor_matrix)) %>%
filter(as.numeric(factor(Var1)) >= as.numeric(factor(Var2)))
Code language: R (r)
In the code example above, we converted the matrix to a long-format data frame and filtered it to include only the lower triangle of the correlation matrix.
Step 2: Creating the Heatmap
ggplot(cor_long, aes(Var1, Var2, fill = Freq)) +
geom_tile(color = NA) + # No border/grid lines between tiles
scale_fill_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0,
name = "Correlation") +
labs(title = "Correlation Heatmap of BFI Personality Traits",
x = "", y = "") +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1),
panel.grid = element_blank() # Removes gridlines
)
Code language: R (r)
In the code above, we used ggplot
to create a heatmap of the correlations between personality traits in the BFI dataset. We passed cor_long
(a long-format correlation matrix) to ggplot()
and mapped Var1
and Var2
to the x and y axes, with Freq
controlling the fill color. We applied geom_tile(color = NA)
to draw the heatmap tiles without any borders or grid lines between them. The scale_fill_gradient2()
function defined the color gradient from blue (negative correlation) through white (no correlation) to red (positive correlation), centered at zero. We added a descriptive title and removed axis labels using labs()
. Finally, we applied a minimal theme with theme_minimal()
and customized it by rotating the x-axis labels for readability and removing the grid lines entirely with panel.grid = element_blank()
.
This will produce a static heatmap that looks like this:

Summary
In this post, we learned how to make a heatmap in R using the ggplot2 package. We saw how to load and subset the data, how to reshape the data from a wide format to a long format, and how to create a basic heatmap. We also saw how to choose a color scheme and add labels to the heatmap. Heatmaps are a data visualization and analysis tool. I hope you found this post valuable and informative. If you did, I encourage you to share this post with your friends and colleagues and comment below if you have any questions or feedback.
Resources
Here are some more data visualization tutorials that you might find helpful:
- How to Create a Word Cloud in R
- Plot Prediction Interval in R using ggplot2
- How to Create a Violin plot in R with ggplot2 and Customize it
- How to Make a Scatter Plot in R with Ggplot2