How to Create a Heatmap in R Programming

Heatmaps are useful data visualizations in data science and business. Here are three ways to create heatmaps using R.

Pierre DeBois
6 min readMar 11


Image create in MidJourney by Pierre DeBois

Visualizations are helpful for identifying the degree of difference among a dataset. One of the most trusted visualizations is a heatmap.

A heatmap is essentially a table containing colors for each element. The colors represent a measuring scale applied to the data. Heatmaps are useful data visualizations when you need to provide your stakeholders a simple difference between a range of numbers. This is valuable for exploratory data analysis, reflecting potential correlations. It can help you locate high and low values among the table variables or rows. Correlation plots provide this visualization principle, with the data relationships displayed through a correlation scale.

There are a few ways to have a heatmap on data in R programming. A heatmap involves 2 variables represented as columns of data within an R object, like a data frame or matrix. So you want to pick the two variables you want to compare in your heatmap.

1. Heatmaps with ggplot2

The ggplot library is a popular data visualization choice in R programming, so naturally interest in creating a heatmap while using the ggplot syntax is also high. The ggplot function accesses the data and specifies the graph dimensions in a usual fashion.

To create a heatmap, the ggplot function encourages users to place data in the long format. A long format is simply a data table that appears as a portrait rectangular shape. Categories that appear as variables are moved into one column with the corresponding data in each row.

You start your heatmap creation by calling a standard ggplot() function syntax with the data for the graph and the graph aesthetics. You then add the geom parameter geom_tile() to set up the heatmap tiles. The geom_tile() parameter is one of two parameters that create tiles using x, y, width, and height. It positions tiles according to the center point (x,y) and its size (fill).



Pierre DeBois

#analytics |#datascience |#JS |#rstats |#marketing services for #smallbiz | #retail | #nonprofits Contrib @CMSWire @smallbiztrends #blackbusiness #BLM