Visualizing varying effects' posteriors with joyplots

Joy Plots

In a <a href=>previous blog post, I introduced a helper function for drawing “better” forest plots from Bayesian meta-analyses estimated with the brms R package (Buerkner 2016). Those plots looked like traditional forest plots, but with density curves to visualize the posterior densities (recall that frequentist Confidence Intervals, unlike their Bayesian counterparts, are “empty” (Morey et al. 2015)). Here, I’d like to suggest that we could use “joy plots(Wilke 2017) to visualize any varying effects’ posteriors instead.

Cover art of Joy Division’s Unknown Pleasures.

Cover art of Joy Division’s Unknown Pleasures.

Joy plots are (I assume) so called because of Joy Division’s well-known album cover art, shown above (image source In statistics and data science, we could clearly use the mountain shapes to visualize densities, histograms, etc. But how?


It turns out that there’s an R package for that. ggjoy (Wilke 2017) extends ggplot2’s (Wickham 2016) functionality for easily drawing figures such as the one above, but from data. So, let’s assume that we have some posterior samples, preferrably of some varying effects (any parameters will do but these will look best).

Table 1: First six rows of the posterior samples (in long format).
parameter value
u_1 -12.511793
u_1 -11.049137
u_1 -6.446825
u_1 -7.999259
u_1 -6.563697
u_1 -13.801990

Drawing these joyplots is easy with the package. You’ll need to specify a height aesthetic, which is a transformation of what’s mapped on the Y-axis. There are two geom_joy()s in the package, the first one draws the plot without trailing lines, and the second one, geom_joy2() includes the trailing lines. I’ll use the second one here:

p <- ggplot(d, aes(x=value, y=parameter, height=..density..)) +
    geom_vline(xintercept = 0, col = "grey70") +
    geom_joy2(col = "grey70", fill = "black", scale = 2.4)
Joy plot.

Figure 1: Joy plot.

However, to make the figure more Unknown Pleasures-y, you’ll need to modify the theme a little bit:

darktheme <-  theme_minimal() +
    theme(panel.grid = element_blank(), 
          panel.background = element_rect(fill = "black"),
          plot.background = element_rect(fill = "black"),
          plot.margin = unit(c(1,3,1,3), "cm"))
p + darktheme
Joy plot with modified ggplot2 theme.

Figure 2: Joy plot with modified ggplot2 theme.

Joy plots work well with other ggplot2 extensions, such as the viridis color palette (Garnier 2017).

ggplot(d, aes(x=value, y=parameter, height=..density.., fill = parameter)) +
    scale_fill_viridis(discrete = TRUE) +
    geom_vline(xintercept = 0, col = "grey70") +
    geom_joy(col = "grey70", scale = 2.4, show.legend = F) +
Joy plot with a custom color palette.

Figure 3: Joy plot with a custom color palette.

Well, there you go. Have fun!


Buerkner, Paul-Christian. 2016. Brms: Bayesian Regression Models Using Stan.

Garnier, Simon. 2017. Viridis: Default Color Maps from ’Matplotlib’.

Morey, Richard D., Rink Hoekstra, Jeffrey N. Rouder, Michael D. Lee, and Eric-Jan Wagenmakers. 2015. “The Fallacy of Placing Confidence in Confidence Intervals.” Psychonomic Bulletin & Review.

Wickham, Hadley. 2016. Ggplot2: Elegant Graphics for Data Analysis. 2nd ed. Springer Science & Business Media.

Wilke, Claus O. 2017. Ggjoy: Joyplots in Ggplot2.