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

Time:06-15

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?

CodePudding user response:

I'm not so sure if "on top of one another" is a great idea. Jon's ideas are probably the way to go. But what about just plotting side-by side - our brains can cope with that and we can compare this pretty well.

Make it long, then use facet.

Another option might be an animated graph (see 2nd code chunk below).

require(ggplot2)
#> Loading required package: ggplot2

library(tidyverse)
a <- runif(200, min=0, max = 1000)
b <- runif(200, min=0, max = 1000)
#### BAAAAAD idea to call anything "c" in R!!! Don't do this. ever! 
d <- sample(c("A", "B"), 200, replace =T)
df <- data.frame(a,b,d)

df %>% pivot_longer(cols = c(a,b)) %>%
ggplot(aes(value, fill = d))   
  geom_density(position='fill', alpha = 0.5)  
  facet_grid(~name)

library(gganimate)
p <- df %>% pivot_longer(cols = c(a,b)) %>%
  ggplot(aes(value, fill = d))   
  geom_density(position='fill', alpha = 0.5)  
  labs(title = "{closest_state}")

p_anim <- p   transition_states(name)

animate(p_anim, duration = 2, fps = 5) 

Created on 2022-06-14 by the 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

  • Related