Hexbin chart in ggplot2

The geom_hex function

Given a numerical data frame you can create a hexbin chart in ggplot2 making use of geom_hex.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex()

geom_hex in ggplot2

Few bins

You can control de number of bins in both vertical and horizontal directions. Default value is 30.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(bins = 15)

Hexbin graph in ggplot2

Too many bins

Note that if you set too many bins the hexbin chart will look like a classical scatter plot.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(bins = 60)

geom_hex bin selection

Color customization

Border color of the hexagons

Border color

The border for all hexagons can be customized with the color argument of the geom_hex function.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(color = "white")

Fill color and transparency of the hexagons

Fill and transparency

You can also set a fill color for all the hexagons with fill and control the transparency of the colors with alpha.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex(color = 1, fill = 4, alpha = 0.4) 

Hexbin chart in ggplot2

Color palette

If you want to keep the gradient color palette representing values you can change the default colors with scale_fill_viridis_c, scale_fill_gradient or a similar function.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  scale_fill_viridis_c()

Legend customization

Width and height

When creating a hexbin chart a colour bar will be added to the chart by default. You can control the width and height of this bar with barwidth and barheight, as shown below.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  guides(fill = guide_colourbar(barwidth = 0.7,
                                barheight = 15))

Hexbin chart colour bar legend customization

Change the title

The default title of the legend is “count”. You can change it with the title argument of the guide_colourbar function as follows.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  guides(fill = guide_colourbar(title = "Count"))

Change the title of the hexbin graph in ggplot2

Remove the labels and the ticks

Note that you can remove the ticks and/or the labels of the bar setting the label and ticks arguments to FALSE.

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  guides(fill = guide_colourbar(label = FALSE,
                                ticks = FALSE))

Ticks and labels colour bar in ggplot2

Remove the legend

Finally, if you want to remove the legend you can set its position to "none".

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

# Data
set.seed(1)
df <- data.frame(x = rnorm(2000), y = rnorm(2000))

ggplot(df, aes(x = x, y = y)) +
  geom_hex() +
  theme(legend.position = "none")

Remove the legend of a hexbin chart in ggplot2

Better Data Visualizations

A Guide for Scholars, Researchers, and Wonks

Buy on Amazon
ggplot2

Elegant Graphics for Data Analysis

Buy on Amazon
Data Sketches

A journey of imagination, exploration, and beautiful data visualizations

Buy on Amazon

See also