layout order of polygons obstructs higher levels #725

milkypostman opened this Issue Nov 21, 2012 · 3 comments

3 participants


Using code from


data(geyser, "MASS")

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?


just tried it and it doesn't fix the issue.

wch commented Mar 1, 2013

Fixed by #771.

@wch wch closed this Mar 1, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment