Question: How to make a moving distribution plot?

Stackoverflow question

G Elliot Moris showed political polarization through time using a moving distribution plot. Plot of polarization in US politics 1963-2013

From this question: How to use ‘facet’ to create multiple density plot in GGPLOT I managed to use facets to reproduce a similar plot using dummy data:

    library(ggplot2)
    set.seed(101)
    dtf <- data.frame(variable = c(rnorm(1000),
                                   rnorm(1000) + rep(1:10/2,each =100)),
                      group = rep(c("a","b"), each = 1000),
                      year = rep(2001:2010, each=100))
    ggplot(dtf) +
        geom_density(aes(x = variable, fill = group)) +
        facet_grid(year ~.)

But I would like the distributions to overlap as in the original plot. Is there a specific name for such plots and is it possible to reproduce them with R?

[EDIT] A dynamic version of a similar plot is available in this global temperature distribution plot.

Ian’s answer using the ggplot2 and ggjoy packages

Ian answered on StackOverflow

# install.packages("ggjoy")
library(ggjoy)
ggplot(dtf,aes(x = variable, y = as.factor(year), fill = group, alpha = .5)) +
        geom_joy(scale = 2) + theme_joy() + theme_minimal()
## Picking joint bandwidth of 0.347

Modifications based on Ian’s answer

# Transform year to a factor
dtf$year <- factor(dtf$year, levels = rev(unique(dtf$year)))
ggplot(dtf,aes(x = variable, y = year, fill = group)) +
        geom_joy(scale = 2, alpha = .5, rel_min_height = 0.01) + 
    theme_joy() + 
    scale_y_discrete(breaks=c(2001,2005,2010))
## Picking joint bandwidth of 0.347

    # Originaly removed the horizontal lines completely
    # theme(panel.grid.major.y = element_blank())