Search code examples
rggplot2probability-densitydensity-plot

R ggplot: overlay two conditional density plots (same binary outcome variable) - possible?


I know how to plot several density curves/polygrams on one plot, but not conditional density plots. Reproducible example:

require(ggplot2)

# generate data
a <- runif(200, min=0, max = 1000)
b <- runif(200, min=0, max = 1000)
c <- sample(c("A", "B"), 200, replace =T)
df <- data.frame(a,b,c)

# plot 1
ggplot(df, aes(a, fill = c)) + 
  geom_density(position='fill', alpha = 0.5) 


# plot 2
ggplot(df, aes(b, fill = c)) + 
  geom_density(position='fill', alpha = 0.5)

In my real data I have a bunch of these paired conditional density plots and I would need to overlay one over the other to see (and show) how different (or similar) they are. Does anyone know how to do this?


Solution

  • One way would be to plot the two versions as layers. The overlapping areas will be slightly different, depending on the layer order, based on how alpha works in ggplot2. This may or may not be what you want. You might fiddle with the two alphas, or vary the border colors, to distinguish them more.

    ggplot(df, aes(fill = c)) + 
      geom_density(aes(a), position='fill', alpha = 0.5) +   
      geom_density(aes(b), position='fill', alpha = 0.5)
    

    enter image description here

    For example, you might make it so the fill only applies to one layer, but the other layer distinguishes groups using the group aesthetic, and perhaps a different linetype. This one seems more readable to me, especially if there is a natural ordering to the two variables that justifies putting one in the "foreground" and one in the "background."

    ggplot(df) + 
      geom_density(aes(a, group = c), position='fill', alpha = 0.2, linetype = "dashed") + 
      geom_density(aes(b, fill = c), position='fill', alpha = 0.5) 
    

    enter image description here