+ - 0:00:00
Notes for current slide

Dealing with issues of overplotting

Claus O. Wilke

last updated: 2021-09-12

Be aware of points plotted exactly on top of one another

Be aware of points plotted exactly on top of one another

Technical term for this problem: overplotting

Partial transparency helps highlight overlapping points

A little jitter shows overlaps even more clearly

But don't jitter too much

2D density plots: Contour lines and contour bands

Contour lines are the 2D version of density plots

We can vary shading for added effect

What do we do when there are multiple groups?

Colored contour lines can work for 2 to 3 groups

What if there are multiple groups intermingled?

Don't make plots that look like spaghetti

Contour lines work well with small multiples (facets)

2D histograms: Rectangular and hex bins

2D histograms: rectangular bins

2D histograms: rectangular bins

We need to choose a bin size as in regular histograms

2D histograms: rectangular bins

We need to choose a bin size as in regular histograms

2D histograms: hex bins

2D histograms: hex bins

Choosing the right color scale

palette: SunsetDark

Choosing the right color scale

palette: Batlow

Choosing the right color scale

palette: YlOrRd

Choosing the right color scale

palette: BluYl

Choosing the right color scale

palette: Heat

Choosing the right color scale

palette: ag_GrnYl

Choosing the right color scale

palette: SunsetDark

Creating 2D density plots and histograms in R

Contour lines

blue_jays %>%
ggplot(aes(body_mass_g, head_length_mm)) +
geom_point() +
theme_bw(14)

Contour lines

blue_jays %>%
ggplot(aes(body_mass_g, head_length_mm)) +
geom_density_2d() +
geom_point() +
theme_bw(14)

Contour lines

blue_jays %>%
ggplot(aes(body_mass_g, head_length_mm)) +
geom_density_2d(bins = 5) +
geom_point() +
theme_bw(14)

Contour bands

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_density_2d_filled(bins = 5) +
#
geom_point() +
theme_bw(14)

Contour bands

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_density_2d_filled(bins = 5, alpha = 0.5) +
#
geom_point() +
theme_bw(14)

Contour bands

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_density_2d_filled(bins = 5, alpha = 0.5) +
geom_density_2d(bins = 5, color = "black", size = 0.2) +
geom_point() +
theme_bw(14)

2D histograms

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_bin2d() +
theme_bw(14)

2D histograms

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_bin2d(binwidth = c(3, 3)) +
theme_bw(14)

2D histograms

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_bin2d(binwidth = c(1, 5)) +
theme_bw(14)

2D histograms

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_bin2d(binwidth = c(5, 1)) +
theme_bw(14)

Hex bins

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_hex() +
theme_bw(14)

Hex bins

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_hex(bins = 15) +
theme_bw(14)

Hex bins

ggplot(blue_jays, aes(body_mass_g, head_length_mm)) +
geom_hex(bins = 10) +
theme_bw(14)

Further reading

Be aware of points plotted exactly on top of one another

Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow