0

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!

egor
  • 37
  • 7
  • 1
    Ths question would benefit from a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). And exactly do you want to "indicate how consistent the densities are at each point"? – MrFlick Aug 31 '14 at 22:54
  • 2
    i think you'll be better off pre-computing the densities yourself outside ggplot2, and then plotting those + smooth layer. smooth and density are two different stat functions, so you can't use one as the input of the other, afaik – baptiste Aug 31 '14 at 23:29
  • I'll try doing that. Thanks for the suggestion. – egor Aug 31 '14 at 23:45

0 Answers0