I am trying to improve a figure containing multiple density plots. I generate the figure like so:
library(ggplot2)
m <- matrix(data=cbind(rnorm(50, 0, 1), rnorm(50, 0, 1.2), rnorm(50, 0, 1.4), rnorm(50, 0, 1.6)), nrow=50, ncol=4)
ms <- stack(as.data.frame(m))
ggplot(ms, aes(x=values, color=ind)) + geom_density()
Instead of visualizing each density individually, I want to combine them together into one 'averaged' density and indicate how consistent the densities are at each point (just like the confidence bounds produced by the stat_smooth()
function).
I have actually tried using stat_smooth()
, but got this error:
Error: stat_smooth requires the following missing aesthetics: y
Is there a way to use stat_smooth()
in this situation?
Thanks!