Heat map in ggplot2

Sample data

Given a numerical matrix you will need to transform it into a data frame that ggplot2 can understand. For that purpose you can use the melt function from reshape package.

# install.packages("reshape")
library(reshape)

# Data 
set.seed(8)
m <- matrix(round(rnorm(200), 2), 10, 10)
colnames(m) <- paste("Col", 1:10)
rownames(m) <- paste("Row", 1:10)

# Transform the matrix in long format
df <- melt(m)
colnames(df) <- c("x", "y", "value")

Heat map with geom_tile

A heap map in ggplot2 can be created with geom_tile, passing the categorical variables to x and y arguments and the continuous variable to fill argument of aes.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile()

geom_tile in ggplot2

Square tiles

Note that depending on the plotting windows size the tiles might not be squared. If you want to keep them squared use cood_fixed.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile() +
  coord_fixed()

Basic heat map in ggplot2 with geom_tile

Border customization

You can customize the border color, line width and line style of the tiles with color, lwd and linetype, respectively.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "white",
            lwd = 1.5,
            linetype = 1) +
  coord_fixed()

geom_tile border line customization

Adding the values

In addition, you can add the values over the tiles with geom_text, passing the numerical variable to the label argument of the aes function.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  geom_text(aes(label = value), color = "white", size = 4) +
  coord_fixed()

Heat map with text values in ggplot2

Color palette

There three ways to change the default color palette used when creating the heat map: using scale_fill_gradient, scale_fill_gradient2 or scale_fill_gradientn.

Heat map with custom colors in ggplot2

scale_fill_gradient

This function allows changing the colors, setting a lower and a higher color to represent the values of the heat map.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradient(low = "white", high = "red") +
  coord_fixed()

ggplot2 heat map gradiend colors

scale_fill_gradient2

If you want to add a mid color you can use scale_fill_gradient2, which includes the mid argument.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradient2(low = "#075AFF",
                       mid = "#FFFFCC",
                       high = "#FF0000") +
  coord_fixed()

ggplot heat map color palette

scale_fill_gradientn

Finally, you can also use a custom color palette with scale_fill_gradientn, which allows passing n colors to the colors argument. In this example we are passing 20 colors of the "RdYlGn" palette.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  scale_fill_gradientn(colors = hcl.colors(20, "RdYlGn")) +
  coord_fixed()

Legend customization

Width and height

You can change the width and the height of the legend color bar with the following code:

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  guides(fill = guide_colourbar(barwidth = 0.5,
                                barheight = 20))

Heat map legend width and height in ggplot

Change the title

The default title of the legend is the name of the continuous variable of the data frame. If you want to change it pass a string to the title argument of the guide_colourbar function.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  guides(fill = guide_colourbar(title = "Title"))

Heat map ggplot2 legend title

Remove the labels and the ticks

You can also remove the labels and/or the ticks of the legend, setting the corresponding arguments to FALSE.

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  guides(fill = guide_colourbar(label = FALSE,
                                ticks = FALSE))

Remove the ticks and the labels of the color bar in ggplot2

Remove the legend

Finally, it you want to get rid of the legend you can set its position to "none".

# install.packages("ggplot2")
library(ggplot2)

ggplot(df, aes(x = x, y = y, fill = value)) +
  geom_tile(color = "black") +
  coord_fixed() +
  theme(legend.position = "none")

Remove the legend of a heat map in ggplot2

Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon
Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon
Fundamentals of Data Visualization

A Primer on Making Informative and Compelling Figures

Buy on Amazon

See also