Beyond Bar and Box Plots

Hands–on code to create and style chart alternatives with ggplot2

Cédric Scherer https://cedricscherer.com
2021-11-04

The slides are available here.
Alternatively, you can watch the recording on YouTube.

Packages

library(tidyverse)     ## data wrangling + ggplot2
library(colorspace)    ## adjust colors
library(rcartocolor)   ## Carto palettes
library(ggforce)       ## sina plots
library(ggdist)        ## halfeye plots
library(ggridges)      ## ridgeline plots
library(ggbeeswarm)    ## beeswarm plots
library(gghalves)      ## off-set jitter
library(systemfonts)   ## custom fonts

Import Data

We are using a synthetic data set consisting of four groups with differing sample sizes and diustributions of some values.

url <- "https://raw.githubusercontent.com/z3tt/DataViz-Teaching/master/data/weissgerber-data.csv"
data <- read_csv(url)

The data was generated with the code provided in the flipbook by Nico Riedel, Robert Schulz, and Tracey Weissgerber.

ggplot2 Setup

To use a custom font, one needs to install the .ttf or .otf font files on the local machine. Here I am using the well-known Roboto typefaces that are available via GoogleFonts:

(In case you can’t install the fonts, you might see some warnings that you can ignore. In case it throws an error, remove all arguments related to fonts, i.e. family and base_family arguments.)

## general theme
theme_set(theme_void(base_family = "Roboto"))

theme_update(
  axis.text.x = element_text(color = "black", face = "bold", size = 26, 
                             margin = margin(t = 6)),
  axis.text.y = element_text(color = "black", size = 22, hjust = 1, 
                             margin = margin(r = 6), family = "Roboto Mono"),
  axis.line.x = element_line(color = "black", size = 1),
  panel.grid.major.y = element_line(color = "grey90", size = .6),
  plot.background = element_rect(fill = "white", color = "white"),
  plot.margin = margin(rep(20, 4))
)


## theme for horizontal charts
theme_flip <-
  theme(
    axis.text.x = element_text(face = "plain", family = "Roboto Mono", size = 22),
    axis.text.y = element_text(face = "bold", family = "Roboto", size = 26),
    panel.grid.major.x = element_line(color = "grey90", size = .6),
    panel.grid.major.y = element_blank(),
    legend.position = "top", 
    legend.text = element_text(family = "Roboto Mono", size = 18),
    legend.title = element_text(face = "bold", size = 18, margin = margin(b = 25))
  )

## custom colors
my_pal <- rcartocolor::carto_pal(n = 8, name = "Bold")[c(1, 3, 7, 2)]

Summary Plots

Barplot

ggplot(data, aes(x = group, y = value, fill = group)) +
  geom_bar(stat = "summary", width = .8) +
  scale_y_continuous(expand = c(0, 0), breaks = 0:4) + 
  scale_fill_manual(values = my_pal, guide = "none")

Most data are not as clean as bar plots make them seem, and since bar plots reveal very little about the distribution of the data, this kind of visualization can be misleading. A further issue is that of the bar itself, which implies that the base of the y-axis is meaningful, which is not necessarily the case. ~ #barbarplot intiative

ggplot(data, aes(x = group, fill = group)) +
  geom_bar(width = .8) +
  scale_y_continuous(expand = c(0, 0)) +
  scale_fill_manual(values = my_pal, guide = "none")

Dynamite Plot

Dynamite plots are used to compare measurements from two or more groups: cases and controls, for example. In a two group comparison, the plots are graphical representations of a grand total of 4 numbers, regardless of the sample size. The four numbers are the average and the standard error (or the standard deviation, it’s not always clear) for each group. ~ Rafael Irizarry, “Dynamite Plots Must Die”

ggplot(data, aes(x = group, y = value, color = group, fill = group)) +
  stat_summary(
    geom = "errorbar",
    fun.max = function(x) mean(x) + sd(x),
    fun.min = function(x) mean(x) - sd(x),
    width = .3, size = 1.2
  ) +
  geom_bar(stat = "summary", width = .8, size = .8) +
  scale_y_continuous(expand = c(0, 0), breaks = 1:9) +
  scale_fill_manual(values = my_pal, guide = "none") +
  scale_color_manual(values = my_pal, guide = "none")

Boxplot

g <- ggplot(data, aes(x = group, y = value, color = group, fill = group)) +
  scale_y_continuous(breaks = 1:9) +
  scale_color_manual(values = my_pal, guide = "none") +
  scale_fill_manual(values = my_pal, guide = "none")
g + 
  geom_boxplot(alpha = .5, size = 1.5, outlier.size = 5)

g + 
  geom_boxplot(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.5, outlier.size = 5
  )

While box plots are highly effective and widely used in data analytics, they are limited in the fact that they only show specific statistical points, such as the median average or outliers, rather than the distribution of a data set as a whole. ~ LondonSoda.com

Violin Plot

Violin plots focus on illustrating the distribution of the entire data set and can generate different insights, that are hidden in the structure of box plots. ~ LondonSoda.com

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2
  )

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .2
  )

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .8
  )

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .05
  )

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .2, scale = "count"
  )

g + 
  ggdist::stat_halfeye(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7)))
  )

g + 
  ggdist::stat_halfeye(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
    adjust = .2, position = position_nudge(x = -.3)
  )

g + 
  ggdist::stat_halfeye(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
    .width = 1, point_size = 5, adjust = .2, position = position_nudge(x = -.3)
  )

Ridgeline Plot

Ridgeline plots are partially overlapping line plots that create the impression of a mountain range. They can be quite useful for visualizing changes in distributions over time or space. ~ {ggridges} reference

g_ridges <- 
  ggplot(data, aes(value, fct_rev(group), color = group, fill = group)) + 
  coord_cartesian(clip = "off") +
  scale_y_discrete(expand = c(.07, .07)) +
  scale_color_manual(values = my_pal, guide = "none") +
  scale_fill_manual(values = my_pal, guide = "none") +
  theme_flip

g_ridges +
  ggridges::geom_density_ridges(
    alpha = .7, size = 1.5
  )

g_ridges +
  ggridges::geom_density_ridges(
    alpha = .8, size = 1.5, 
    rel_min_height = 0.01
  )

g_ridges + 
  ggridges::stat_density_ridges(
    quantile_lines = TRUE, quantiles = 2, 
    color = "black", alpha = .8, size = 1.5
  ) 

q_pal <- colorspace::lighten(rcartocolor::carto_pal(n = 4, name = "Geyser")[c(2, 1, 4, 3)], .6)
#q_pal <- colorspace::adjust_transparency(rcartocolor::carto_pal(n = 4, name = "Tropic")[c(2, 1, 4, 3)], alpha = .7)

g_ridges + 
  ggridges::stat_density_ridges(
    aes(fill = factor(stat(quantile))),
    geom = "density_ridges_gradient", calc_ecdf = TRUE, quantiles = 4, 
    color = "black", size = 1
  ) +
  scale_fill_manual(values = q_pal, name = "Quartile:") +
  guides(fill = guide_legend(override.aes = list(color = "transparent")))

g_ridges + 
  ggridges::stat_density_ridges(
    aes(fill = factor(stat(quantile))),
    geom = "density_ridges_gradient", calc_ecdf = TRUE,
    quantiles = 4, 
    color = "black", size = 1,
    bandwidth = .1
  ) +
  scale_fill_manual(values = q_pal, name = "Quartile:") +
  guides(fill = guide_legend(override.aes = list(color = "transparent")))

(Note that the quartile lines can become potentially misleading due to low sample sizes. Both quartiles and densities are mathematically correct given the input data, and both fail at providing a useful representation of the data. See Claus Wilke’s tweet for more.)

g_ridges + 
  ggridges::stat_density_ridges(
    aes(fill = factor(stat(quantile))),
    geom = "density_ridges_gradient", calc_ecdf = TRUE,
    quantiles = c(0.025, 0.975),
    color = "black", size = 1.5
  ) +
  scale_fill_manual(
    name = "Probability:", values = c("#994c00", "grey70", "#003366"),
    labels = c("(0, 0.025]", "(0.025, 0.975]", "(0.975, 1]")
  ) +
  guides(fill = guide_legend(override.aes = list(color = "transparent")))

Interval strips

g_interval <- 
  ggplot(data, aes(group, value)) +
  scale_color_viridis_d(
    option = "mako", name = "Level:", direction = -1, 
    begin = .15, end = .9
  ) +
  guides(
    color = guide_legend(reverse = TRUE, title.position = "top")
  ) +
  theme(
    legend.position = c(.75, .95), legend.direction = "horizontal",
    legend.text = element_text(family = "Roboto Mono", size = 18),
    legend.title = element_text(face = "bold", size = 22, hjust = .5)
  )

g_interval +
  ggdist::stat_interval(size = 12)

g_interval +
  ggdist::stat_interval(.width = c(.25, .5, .95, 1), size = 12)

g_interval +
  ggdist::stat_interval(.width = c(.25, .5, .95, 1), size = 12) +
  scale_color_viridis_d(
    option = "mako", name = "Level:", direction = -1, 
    begin = .15, end = .9,
    labels = function(x) paste0(as.numeric(x)*100, "%")
  )

g_interval +
  ggdist::stat_interval(.width = c(.25, .5, .95, 1), size = 12) +
  stat_summary(
    geom = "point", fun = median,
    color = "white", size = 6, shape = 1, stroke = 1.6
  ) +
  scale_color_viridis_d(
    option = "mako", name = "Level:", direction = -1, 
    begin = .15, end = .9,
    labels = function(x) paste0(as.numeric(x)*100, "%")
  ) 

g_interval +
  ggdist::stat_interval(
    .width = c(.25, .5, .95, 1), 
    size = 7
  ) +
  ggdist::stat_halfeye(
    adjust = .33, ## bandwidth
    width = .7, fill = "grey85",
    interval_colour = NA, point_colour = "black",
    shape = 23, stroke = 1.5, point_size = 5, point_fill = "white",
    position = position_nudge(x = .03),
    aes(thickness = stat(f*n))
  ) +
  scale_color_viridis_d(
    option = "mako", name = "Level:", direction = -1, 
    begin = .15, end = .9,
    labels = function(x) paste0(as.numeric(x)*100, "%")
  )

Gradient Interval

g +
  ggdist::stat_gradientinterval(
    width = .3, color = "black"
  )

Raw Data Charts

Scatter Plot (Strip Chart)

g + geom_point(size = 10, alpha = .33)

Barcode Plots (Strip Chart)

g + geom_point(shape = 95, size = 50, alpha = .33)

Jitter Strip Chart

g + geom_jitter(size = 7, alpha = .5)

g + 
  geom_point(
    position = position_jitter(width = .2, seed = 0),
    size = 7, alpha = .5
  )

g + 
  geom_point(
    position = position_jitter(width = .2, seed = 0),
    size = 7, alpha = .5
  ) +
  geom_point(
    position = position_jitter(width = .2, seed = 0),
    size = 7, stroke = .9, shape = 1, color = "black"
  )

Sina Plot

The sina plot is a data visualization chart suitable for plotting any single variable in a multiclass dataset. It is an enhanced jitter strip chart, where the width of the jitter is controlled by the density distribution of the data within each class. ~ {ggforce} reference

g + 
  ggforce::geom_sina(
    maxwidth = .6, scale = "count", seed = 1,
    size = 7, alpha = .5
  ) + 
  ggforce::geom_sina(
    maxwidth = .6, scale = "count", seed = 1, 
    size = 7, shape = 1, color = "black", stroke = .8
  )

Dot Plot

A dot plot or dot chart is a statistical chart consisting of data points plotted on a fairly simple scale, typically using filled in circles. There are two common versions of the dot chart. The first is described by Leland Wilkinson as a graph that has been used in hand-drawn (pre-computer era) graphs to depict distributions. The other version is described by William Cleveland as an alternative to the bar chart, in which dots are used to depict the quantitative values (e.g. counts) associated with categorical variables. ~ datavizproject.com

g + 
  ggdist::stat_dots(position = position_nudge(x = -.25))