In a <a href=https://vuorre.netlify.com/post/2017/better-brms-forest-plots/>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.
Joy plots are (I assume) so called because of Joy Division’s well-known album cover art, shown above (image source https://pbs.twimg.com/media/C1E4xfBWIAAUceW.jpg). 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).
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:
library(ggjoy) 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) p
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 plots work well with other ggplot2 extensions, such as the viridis color palette (Garnier 2017).
library(viridis) 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) + darktheme
Well, there you go. Have fun!
Buerkner, Paul-Christian. 2016. Brms: Bayesian Regression Models Using Stan. http://CRAN.R-project.org/package=brms.
Garnier, Simon. 2017. Viridis: Default Color Maps from ’Matplotlib’. https://CRAN.R-project.org/package=viridis.
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. https://github.com/clauswilke/ggjoy.