Box plot with jittered data points in ggplot2

Box plot with jittered points with geom_jitter

Adding jittered points (a stripchart) to a box plot in ggplot is useful to see the underlying distribution of the data. You will need to use geom_jitter.

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

# Data
set.seed(8)
y <- rnorm(200)
df <- data.frame(y)

# Basic box plot
ggplot(df, aes(x = "", y = y)) + 
  geom_boxplot() +
  geom_jitter()

Box plot in ggplot2 with jitter

A good practice is removing the outliers of the box plot with outlier.shape = NA, as the jitter will add them again.

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

# Data
set.seed(8)
y <- rnorm(200)
df <- data.frame(y)

# Basic box plot
ggplot(df, aes(x = "", y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter()

ggplot2 box plot of one variable with jittered points

Recall that you can flip the axes with coord_flip or flipping the variables inside aes.

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

# Data
set.seed(8)
y <- rnorm(200)
df <- data.frame(y)

# Basic box plot
ggplot(df, aes(x = "", y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter() +
  coord_flip()

Horizontal box plot with jitter in ggplot

Customization of the jittered points

The jittered points can be customized in several ways. You can change the amount of jitter with width, the color of the points, its shape or its size, as shown in the examples below.

Boxplot jitter width ggplot

Width

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

# Data
set.seed(8)
y <- rnorm(200)
df <- data.frame(y)

# Basic box plot
ggplot(df, aes(x = "", y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(width = 0.2)

Jitter points color box plot ggplot

Colour

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

# Data
set.seed(8)
y <- rnorm(200)
df <- data.frame(y)

# Basic box plot
ggplot(df, aes(x = "", y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(colour = 2)

Shape symbol and size of the jitter points in ggplot2

Shape and size

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

# Data
set.seed(8)
y <- rnorm(200)
df <- data.frame(y)

# Basic box plot
ggplot(df, aes(x = "", y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(shape = 17, size = 2)

Box plot by group with jitter

If you have a categorical variable representing groups you can create a box plot by group and add the points to each group and customize its color, size and shape.

Jitter by group

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

# Data
set.seed(8)
y <- rnorm(200)
group <- sample(LETTERS[1:3], size = 200,
                replace = TRUE)
df <- data.frame(y, group)

# Box plot by group with jitter
ggplot(df, aes(x = group, y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter()

Boxplot by group with jitter points in ggplot

Jitter by group with custom color

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

# Data
set.seed(8)
y <- rnorm(200)
group <- sample(LETTERS[1:3], size = 200,
                replace = TRUE)
df <- data.frame(y, group)

# Box plot by group with jitter
ggplot(df, aes(x = group, y = y)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter(colour = 2)

Grouped box plot with jitter in ggplot

Jitter by group with color by group

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

# Data
set.seed(8)
y <- rnorm(200)
group <- sample(LETTERS[1:3], size = 200,
                replace = TRUE)
df <- data.frame(y, group)

# Box plot by group with jitter
ggplot(df, aes(x = group, y = y, colour = group)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter()

ggplot box plot with jitter points colored by group

Jitter by group with custom shape

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

# Data
set.seed(8)
y <- rnorm(200)
group <- sample(LETTERS[1:3],
                size = 200,
                replace = TRUE)
df <- data.frame(y, group)

# Box plot by group with jitter
ggplot(df, aes(x = group, y = y,
               colour = group,
               shape = group)) + 
  geom_boxplot(outlier.shape = NA) +
  geom_jitter()

ggplot2 box plot with jitter points with shape by group

Storytelling with Data

A Data Visualization Guide for Business Professionals

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

See also