4

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

enter image description here

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.

Marcus Campbell
  • 2,746
  • 4
  • 22
  • 36
Paul Rougieux
  • 10,289
  • 4
  • 68
  • 110
  • Hey Paul, Is that the transparancy of overlaping sections you want to achieve ? If so just include `alpha = value` into the `geom_density` function. Or is that also the vertical overlapping between each sets ? – Paul Endymion Jul 25 '17 at 10:17
  • Well, it looks like a tough plot to do in ggplot, but you may find this post useful in plotly. https://www.r-bloggers.com/3d-density-plot-in-r-with-plotly/ – zielinskipp Jul 25 '17 at 10:29
  • @PaulEndymion I would like to reproduce the vertical overlapping. – Paul Rougieux Jul 25 '17 at 13:05
  • 1
    Update October 2017, the package is now called [ggrides](https://cran.r-project.org/web/packages/ggridges/vignettes/introduction.html), and the geom is called `geom_density_ridges`. After `library(ggridges)`, the plotting instruction becomes: `ggplot(dtf,aes(x = variable, y = as.factor(year), fill = group)) + geom_density_ridges(scale = 2,alpha = .5,rel_min_height = 0.01) + theme_joy()` – Paul Rougieux Oct 09 '17 at 10:08
  • It's probably not allowed to say so, but that's beautiful! – RTS Apr 26 '20 at 01:11

1 Answers1

11

Update: the package used to created this plot is now named "ggridges".

You'll need the ggjoy package for this. Here's a rough version that needs some tidying up:


devtools::install_github("clauswilke/ggjoy")
library(ggjoy)
library(ggplot2)

#Create data frame
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))

# Use ggplot2 and ggjoy packages  
ggplot(dtf,aes(x = variable, y = as.factor(year), fill = group)) +
  geom_joy(scale = 2,alpha = .5,rel_min_height = 0.01) + theme_joy() 
#> Picking joint bandwidth of 0.347

Ian
  • 441
  • 3
  • 5
  • Nice! just one suggestion, your alpha parameter should be outside `aes`. – Paul Endymion Jul 25 '17 at 13:50
  • Thanks, that's close to what I was looking for. How can I remove the horizontal black lines? I tried `theme(axis.line=element_blank())` but it doesn't remove those lines. – Paul Rougieux Jul 25 '17 at 13:53
  • Hi Paul, I've added the rel_min_height argument which removes the horizontal black lines. I have also moved alpha parameter outside aes, as @PaulEndymion has suggested. – Ian Jul 26 '17 at 08:31
  • @Ian In the original plot, there are no lines and the only indication of the position of the zero comes from the fill aesthetic of the distribution. Based on [this answer](https://stackoverflow.com/questions/2678141/how-can-i-suppress-the-vertical-gridlines-in-a-ggplot2-plot), I added `theme(panel.grid.major.y = element_blank())` to remove the horizontal lines completely. Thanks again, your answer made me happy. – Paul Rougieux Jul 26 '17 at 14:59
  • In fact there are horizontal grid lines in the original plot, so they should not be all removed, but just reduced to a reasonable number with `scale_y_discrete(breaks=c(2001,2005,2010))`. – Paul Rougieux Jul 28 '17 at 08:01