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))

g + 
  ggdist::stat_dots(side = "both")

g + 
  ggdist::stat_dots(layout = "weave", position = position_nudge(x = -.25))

Beeswarm Plot

g + 
  ggdist::stat_dots(layout = "swarm", side = "both")

g + 
  ggbeeswarm::geom_beeswarm(size = 8, cex = 3)

g + 
  ggbeeswarm::geom_quasirandom(size = 8, width = .33, alpha = .7) + 
  ggbeeswarm::geom_quasirandom(size = 8, width = .33, shape = 1, color = "black", stroke = .8)

Hybrid Charts

Beeswarm with Median Indicator

g + 
  ggbeeswarm::geom_quasirandom(
    size = 8, width = .33, alpha = .3
  ) +
  stat_summary(
    fun = median, geom = "point", 
    shape = 95, size = 50
  ) + 
  ggbeeswarm::geom_quasirandom(
    size = 8, width = .33, shape = 1, color = "black", stroke = .8
  )

Box Plot x Jitter Strips

g + 
  geom_boxplot(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
    size = 1.5, outlier.shape = NA
  ) +
  geom_jitter(width = .1, size = 7, alpha = .5)

g + 
  geom_boxplot(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
    size = 1.5, outlier.shape = NA
  ) +
  geom_point(
    position = position_jitter(width = .1, seed = 0),
    size = 7, alpha = .5
  ) +
  geom_point(
    position = position_jitter(width = .1, seed = 0),
    size = 7, stroke = .9, shape = 1, color = "white"
  )

Box Plot x Violin Plot

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

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .2
  ) + 
  geom_boxplot(
    fill = "white",  size = 1.2, width = .2, 
    outlier.shape = NA, coef = 0
  )

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .2, color = NA
  ) +
  geom_boxplot(
    width = .1, size = 1.2, outlier.shape = NA
  ) +
  stat_summary(
    geom = "point",
    fun = median,
    color = "white",
    size = 5
  )

Box Plot x Violin Plot x Jitter Strips

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .5))),
    size = 1.2, bw = .2
  ) + 
  geom_boxplot(
    fill = "white",  size = 1.2, width = .2, 
    outlier.shape = NA, coef = 0
  ) +
  geom_point(
    position = position_jitter(width = .03, seed = 0),
    size = 5, alpha = .5
  ) +
  geom_point(
    position = position_jitter(width = .03, seed = 0),
    size = 5, stroke = .7, shape = 1, color = "black"
  )

Box Plot x Violin Plot x Beeswarm Plot

Violin Plot x Sina Plots

g + 
  geom_violin(
    aes(fill = group, fill = after_scale(colorspace::lighten(fill, .7))),
    size = 1.2, bw = .2, width = .6, scale = "count"
  ) +
  stat_summary(
    geom = "point", 
    fun = median,
    shape = 23, size = 6, color = "black", stroke = 1.5
  ) + 
  ggforce::geom_sina(
    maxwidth = .5, scale = "count", 
    size = 3, alpha = .5, seed = 0
  ) + 
  ggforce::geom_sina(
    maxwidth = .5, scale = "count", 
    size = 3, alpha = .5, seed = 0,
    shape = 1, color = "black", stroke = .8
  )

Raincloud Plots

A data visualization approach which [provides] maximal statistical information while preserving the desired ‘inference at a glance’ nature of barplots and other similar visualization devices. These “raincloud plots” can visualize raw data, probability density, and key summary statistics such as median, mean, and relevant confidence intervals in an appealing and flexible format with minimal redundancy. ~ Allen et al. (2021) Wellcome Open Res 4:63

g + 
  geom_boxplot(
    width = .2, fill = "white",
    size = 1.5, outlier.shape = NA
  ) +
  ggdist::stat_halfeye(
    adjust = .33, ## bandwidth
    width = .67, 
    color = NA, ## remove slab interval
    position = position_nudge(x = .15)
  ) +
  gghalves::geom_half_point(
    side = "l", 
    range_scale = .3, 
    alpha = .5, size = 3
  )

ggplot(data, aes(x = forcats::fct_rev(group), y = value, 
                 color = group, fill = group)) +
  geom_boxplot(
    width = .2, fill = "white",
    size = 1.5, outlier.shape = NA
  ) +
  ggdist::stat_halfeye(
    adjust = .33,
    width = .67, 
    color = NA,
    position = position_nudge(x = .15)
  ) +
  gghalves::geom_half_point(
    side = "l", 
    range_scale = .3, 
    alpha = .5, size = 3
  ) +
  coord_flip() +
  scale_x_discrete(expand = c(.07, .07)) +
  scale_y_continuous(breaks = 1:9) +
  scale_color_manual(values = my_pal, guide = "none") +
  scale_fill_manual(values = my_pal, guide = "none") +
  theme_flip

g + 
  geom_boxplot(
    width = .2, fill = "white",
    size = 1.5, outlier.shape = NA
  ) +
  ggdist::stat_halfeye(
    adjust = .33, 
    width = .55, 
    color = NA, 
    position = position_nudge(x = .14)
  ) +
  geom_point(
    position = position_nudge(x = -.22),
    shape = 95, size = 24, alpha = .25
  )


Session Info
[1] "2021-11-04 09:39:43 CET"
R version 4.1.0 (2021-05-18)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19043)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252   
[3] LC_MONETARY=German_Germany.1252 LC_NUMERIC=C                   
[5] LC_TIME=C                      
system code page: 65001

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] systemfonts_1.0.3 gghalves_0.1.1    ggbeeswarm_0.6.0 
 [4] ggridges_0.5.3    ggdist_3.0.0      ggforce_0.3.3    
 [7] rcartocolor_2.0.0 colorspace_2.0-2  forcats_0.5.1    
[10] stringr_1.4.0     dplyr_1.0.7       purrr_0.3.4      
[13] readr_2.0.2       tidyr_1.1.4       tibble_3.1.5     
[16] ggplot2_3.3.5     tidyverse_1.3.1  

loaded via a namespace (and not attached):
 [1] fs_1.5.0             lubridate_1.8.0      bit64_4.0.5         
 [4] httr_1.4.2           tools_4.1.0          backports_1.2.1     
 [7] bslib_0.3.1          utf8_1.2.2           R6_2.5.1            
[10] vipor_0.4.5          DBI_1.1.1            withr_2.4.2         
[13] tidyselect_1.1.1     downlit_0.2.1        curl_4.3.2          
[16] bit_4.0.4            compiler_4.1.0       textshaping_0.3.6   
[19] cli_3.0.0            rvest_1.0.2          xml2_1.3.2          
[22] labeling_0.4.2       sass_0.4.0           scales_1.1.1        
[25] digest_0.6.28        rmarkdown_2.11       pkgconfig_2.0.3     
[28] htmltools_0.5.2      highr_0.9            dbplyr_2.1.1        
[31] fastmap_1.1.0        rlang_0.4.12         readxl_1.3.1        
[34] rstudioapi_0.13      jquerylib_0.1.4      farver_2.1.0        
[37] generics_0.1.0       jsonlite_1.7.2       vroom_1.5.5         
[40] distill_1.3          distributional_0.2.2 magrittr_2.0.1      
[43] Rcpp_1.0.7           munsell_0.5.0        fansi_0.5.0         
[46] lifecycle_1.0.1      stringi_1.7.5        yaml_2.2.1          
[49] MASS_7.3-54          plyr_1.8.6           grid_4.1.0          
[52] parallel_4.1.0       crayon_1.4.1         haven_2.4.3         
[55] hms_1.1.1            knitr_1.36           pillar_1.6.4        
[58] reprex_2.0.1         glue_1.4.2           evaluate_0.14       
[61] modelr_0.1.8         vctrs_0.3.8          tzdb_0.1.2          
[64] tweenr_1.0.2         cellranger_1.1.0     gtable_0.3.0        
[67] polyclip_1.10-0      assertthat_0.2.1     xfun_0.27           
[70] broom_0.7.9          viridisLite_0.4.0    ragg_1.1.3          
[73] beeswarm_0.4.0       ellipsis_0.3.2