Menu

Tutorial8 min read

How to Make Heatmaps in R with ggplot2 (geom_tile)

By Francesco Villasmunta
How to Make Heatmaps in R with ggplot2 (geom_tile)

Heatmaps are excellent for summarizing large correlation matrices, gene expression grids, or spatial intensities. In ggplot2, heatmaps are built using the geom_tile() layer, mapping two categorical columns to the x and y axes, and a continuous variable to the fill aesthetic.

In This Tutorial

0.Live Code: Heatmap with Annotations

1.Basic Grid (geom_tile)

2.Applying Diverging Color Scales

3.Adding Values as Text

4.Masking Symmetrical Triangles

0. Live Code: Heatmap with Annotations

Correlation matrices. Customize parameters using Python below, or upload your data to run R directly.

1. Basic Heatmap Grid with geom_tile

Specify categorical variables for coordinate grids, and map intensity to the `fill` aesthetic:

R / ggplot2

# Basic heat grid
ggplot(df, aes(x = Var1, y = Var2, fill = Correlation)) +
  geom_tile(color = "white", size = 0.1)

Try it

Try it now: create this heatmap from your data

Generate an annotated heatmap with a readable colormap and export-ready styling in one click.

Generate my heatmap

Newsletter

Get a weekly Python plotting tip

One concise tip each week for cleaner, faster scientific figures. Built for researchers who publish.

No spam. Unsubscribe anytime.

2. Applying Diverging Color Scales

For correlation matrices, use diverging scales like `scale_fill_gradient2()` centered at zero to clearly separate positive and negative correlations:

R / ggplot2

ggplot(df, aes(x = Var1, y = Var2, fill = Correlation)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "royalblue4", mid = "white", high = "red4", midpoint = 0, limit = c(-1, 1))

3. Adding Values as Text annotations

Overlay numeric values directly inside cells using geom_text():

R / ggplot2

ggplot(df, aes(x = Var1, y = Var2, fill = Correlation)) +
  geom_tile(color = "white") +
  scale_fill_gradient2(low = "blue", mid = "white", high = "red", midpoint = 0) +
  geom_text(aes(label = round(Correlation, 2)), color = "black", size = 3) +
  theme_minimal()

4. Masking Symmetrical Triangles

Filer the correlation data frame beforehand using `lower.tri()` or `upper.tri()` to keep only one half and avoid redundant duplicates.

Chart gallery

Explore related formats

Review visual formats.

Browse all chart types →
Scatter plot of height vs weight colored by gender with regression line
Statisticalmatplotlib, seaborn
From the chart galleryCorrelation analysis between metrics

Scatterplot

Displays values for two variables as points on a Cartesian coordinate system.

Sample code / prompt

import matplotlib.pyplot as plt
import numpy as np
from scipy import stats
import pandas as pd

# Generate sample data
np.random.seed(42)
n_samples = 200
height = np.random.normal(170, 8, n_samples)
weight = height * 0.6 + np.random.normal(0, 8, n_samples) - 50
Bar chart comparing average scores across 5 groups with error bars
Comparisonmatplotlib, seaborn
From the chart galleryComparing performance across categories

Bar Chart

Compares categorical data using rectangular bars with heights proportional to values.

Sample code / prompt

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from scipy import stats

# Generate performance scores for 5 treatment groups
np.random.seed(42)
groups = ['Control', 'Treatment A', 'Treatment B', 'Treatment C', 'Treatment D']
n_samples = 30

Build This Correlation Heatmap Online

Upload your data and describe the design. Plotivy writes the ggplot2 code and executes it instantly.

Use R Analyzer
Tags:#R#ggplot2#heatmap#geom_tile#correlation matrix

Related chart guides

Apply this tutorial directly in the chart gallery with ready-to-run prompts and examples.

Found this helpful? Share it with your network.

FV
Francesco Villasmunta

Experimental Physicist & Photonics Researcher

Hands-on experience in silicon photonics, semiconductor fabrication (DRIE/ICP-RIE), optical simulation, and data-driven analysis. Built Plotivy to help researchers focus on discoveries instead of data struggles.

More about the author

Visualize your own data

Apply the techniques from this article to your own datasets. Upload CSV, Excel, or paste data directly.

Start Analyzing - Free