Re: Drawing order on stat_density2d and stat_contour obscures some layers.

199 views
Skip to first unread message

Brandon Hurr

unread,
Nov 20, 2012, 8:39:59 PM11/20/12
to Donald Curtis, ggp...@googlegroups.com
I bet the levels are being reversed before plotting. I'd say its a bug. 


On Tuesday, November 20, 2012, Donald Curtis wrote:
I am having the problem that when adding stat_contour or stat_density2d the "upper" layer polygons are being obscured by lower ones. I made a video to demonstrate and it can also be seen by running the code on http://www.inside-r.org/packages/cran/ggplot2/docs/stat_density2d

ggplot2 draw order problem with stat_density2d

I'm not an R expert by any means so I can't be sure what version of ggplot2 I am using but i've had this happen on two machines and I am pretty sure I have the newest version.

Here is the "version" information from R

platform       x86_64-apple-darwin12.2.0    
arch           x86_64                       
os             darwin12.2.0                 
system         x86_64, darwin12.2.0         
status                                      
major          2                            
minor          15.2                         
year           2012                         
month          10                           
day            26                           
svn rev        61015                        
language       R                            
version.string R version 2.15.2 (2012-10-26)
nickname       Trick or Treat  

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Dennis Murphy

unread,
Nov 20, 2012, 9:42:14 PM11/20/12
to Donald Curtis, ggp...@googlegroups.com
> I am having the problem that when adding stat_contour or stat_density2d 
> the "upper" layer polygons are being obscured by lower ones.

Well, yes, later filled polygons will obscure previous ones. This is because ggplot2, like grid, operates on a "painter's model" of graphics (new overwrites old at any given location). The order in which layers are drawn can sometimes matter. Alpha transparency can be used to mitigate the problem. Try various fixed values of alpha (assuming the geom supports it) and use the one that looks the best to you.

Example:

data(geyser, "MASS")
(m <- ggplot(geyser, aes(x = duration, y = waiting)) +
   geom_point() + xlim(0.5, 6) + ylim(40, 110))
m + stat_density2d(aes(fill = ..level..), geom="polygon")
m + stat_density2d(aes(fill = ..level..), geom="polygon", alpha = 0.2)

The colorbar issue is a side point that can be handled separately using guides().

A more mundane example of the painter's model:

DF <- data.frame(x = seq(10), y = seq(10))
# line overwrites points
ggplot(DF, aes(x = x, y = y)) + geom_point(size = 3, color = "orange") +
     geom_line()
# points overwrite line
ggplot(DF, aes(x = x, y = y)) + geom_line() + 
     geom_point(size = 3, color = "orange")
# compromise
ggplot(DF, aes(x = x, y = y)) + geom_line() + 
     geom_point(size = 3, color = "orange", alpha = 0.6)

Dennis

On Tue, Nov 20, 2012 at 4:56 PM, Donald Curtis <dcu...@coe.edu> wrote:
I am having the problem that when adding stat_contour or stat_density2d the "upper" layer polygons are being obscured by lower ones. I made a video to demonstrate and it can also be seen by running the code on http://www.inside-r.org/packages/cran/ggplot2/docs/stat_density2d

Donald Curtis

unread,
Nov 20, 2012, 10:37:01 PM11/20/12
to Dennis Murphy, ggp...@googlegroups.com
But shouldn't ggplot know to order by level?
Reply all
Reply to author
Forward
0 new messages