Using code from http://www.inside-r.org/packages/cran/ggplot2/docs/stat_density2d
m <- ggplot(geyser, aes(x = duration, y = waiting)) +
geom_point() + xlim(0.5, 6) + ylim(40, 110)
m + geom_density2d()
dens <- kde2d(geyser$duration, geyser$waiting, n = 50,
lims = c(0.5, 6, 40, 110))
densdf <- data.frame(expand.grid(duration = dens$x, waiting = dens$y),
z = as.vector(dens$z))
m + geom_contour(aes(z=z), data=densdf)
m + geom_density2d() + scale_y_log10()
m + geom_density2d() + coord_trans(y="log10")
m + stat_density2d(aes(fill = ..level..), geom="polygon")
There is a problem with the order in which the layers are drawn and so higher levels (which are smaller) get obstructed. Here is a video of what is happening.
It seems to me that when rendering stat_contour ggplot2 should know to render higher layers after lower layers.
Can you try adding order = ..level.. to your aes spec?
order = ..level..
just tried it and it doesn't fix the issue.
Fixed by #771.