stacked density plot

Hi Joe,
It makes things easier if there's a reproducible example (reprex) to work with including code and data, but here's a toy example of the same data plotted with position = "stacked" and position = "identity". In your figure, I would read that the values start from the one plotted below it, and you have approximately 3x's as many males in your dataset as females, both with a modal age around 25.

library("tibble")
library("ggplot2")
library("patchwork")

set.seed(456)
DF <- tibble(Sex = c(rep("female", 50), rep("male", 150)),
  Age = rpois(200, 30))

# Position is stacked on each other
p_stacked <- ggplot(DF) +
  stat_density(aes(x = Age, y = ..count..,
    fill = Sex), position = "stack", alpha = 0.5) +
  labs(title = "Position = stacked")

# Position starts from the axes
p_identity <- ggplot(DF) +
  stat_density(aes(x = Age, y = ..count..,
    fill = Sex), position = "identity", alpha = 0.5) +
  labs(title = "Position = identity")


p_stacked + p_identity

Created on 2020-08-30 by the reprex package (v0.3.0)

1 Like