Skip to content

Instantly share code, notes, and snippets.

@juliasilge
Forked from noamross/density.R
Created July 13, 2016 13:38
Show Gist options
  • Save juliasilge/1808b7c15e3805bdd9c43367d6ff977c to your computer and use it in GitHub Desktop.
Save juliasilge/1808b7c15e3805bdd9c43367d6ff977c to your computer and use it in GitHub Desktop.
Overlapping Density Plots
library(dplyr)
library(tidyr)
library(ggplot2)
library(gridExtra)
df = data_frame(a = rnorm(100, mean=1), b=rnorm(100, mean=2), c=rnorm(100, mean=0)) %>%
gather("var", "value", a,b,c)
ggplot(df, aes(x=value)) +
geom_density(fill="black", col="white", lwd=1) +
facet_grid(var~1, switch="y") +
theme(strip.text.x=element_blank(), panel.background=element_blank(),
panel.margin.y=unit(-3, "cm"), panel.grid=element_blank(),
axis.ticks.y=element_blank(), axis.text.y=element_blank())
@timothyslau
Copy link

timothyslau commented Aug 30, 2017

This didn't work for me when I tried it. First it wanted panel.margin.y to be panel.spacing.y and then it didn't like the facet grid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment