Thanks!
-s
> ## manually calculate the lines where you want them
>
> dens <- kde2d(df$X1, df$X2, n = 100)
>
sorry for my first reply which overlooked the tiny problem that you
can't have two stats for one geom. So David is correct, you have to
compute the density outside of ggplot. However, once you do that,
there is no need to do all the rest manually too. You can use
geom_contour directly. See the second example in stat_density2d or :
library("ggplot2")
library("MASS")
d = data.frame(x=rnorm(1000),y=rnorm(1000))
dens = MASS::kde2d(d$x, d$y, n = 100)
densf = data.frame(expand.grid(x=dens$x, y=dens$y), z=as.vector(dens$z))
ggplot(densf, aes(x=x,y=y,z=z)) + geom_contour(breaks=seq(min(densf
$z), max(densf$z), length.out=10))
# this puts 10 equally spaced breaks but you could put whatever you
want in the breaks parameter
I hope this is more helpful.
JiHO
---
http://jo.irisson.free.fr/
Clearly a far superior method. Thanks JiHO. :)
david.