Overlay ggplot2 stat_density2d graphics with alpha channels, constant by group

I would like to build several groups in the stat_density2 plot with alpha values ​​associated with the case counts in each group. However, the levels formed by stat_density2d seem to normalize to the number of observations in each group. For instance,

temp <- rbind(movies[1:2,],movies[movies$mpaa == "R" | movies$mpaa == "PG-13",])
ggplot(temp, aes(x=rating,y=length)) + 
stat_density2d(geom="tile", aes(fill = mpaa, alpha=..density..), contour=FALSE) + 
theme_minimal()

Creates the following schedule:

enter image description here

Because I included only 2 points without ratings, they lead to densities that look much stronger / stronger than the other two, and therefore wash off the other two densities. I tried looking at Overlaying two ggplot2 stat_density2d graphs with alpha channels and Specifying a scale for density in stat_density2d ggplot2 , but they really do not address this specific problem.

, , , "" 2d- , / . , , stat_density2d. , , !

!

+4
2

stat_hexbin, ..count.. ..density.., :

ggplot(temp, aes(x=rating,y=length)) + 
    stat_binhex(geom="hex", aes(fill = mpaa, alpha=..count..)) + 
    theme_minimal()

.

+2

r-, , , . , , , . for, stat_density2d , .

temp <- rbind(movies[1:2,],movies[movies$mpaa == "R" | movies$mpaa == "PG-13",])
mpaa = unique(temp$mpaa)
p <- ggplot() + theme_minimal()
for (ii in seq(1,3)) {
  ratio = length(which(temp$mpaa == mpaa[ii]))
  p <- p + stat_density2d(data=temp[temp$mpaa == mpaa[ii],], 
                          aes(x=rating,y=length,fill = mpaa, alpha=..level..),
                      geom="polygon", 
                      contour=TRUE, 
                      alpha = ratio/20, 
                      lineType = "none") 
}
print(p)
+2

All Articles