title: How to correctly interpret ggplot's stat_density2d
body: <p>My initial goal was to plot a population of individual points and then draw a convex hull enclosing 80% of that population centered on the mass of the population.  </p>
  
  <p>After trying a number of ideas, the best solution I came up with was to use <code>ggplot</code>'s <code>stat_density2d</code>.  While this works great for a qualitative analysis, I still need to indicate an 80% boundary.  I started out looking for a way to outline the 80th percentile <strong>population</strong> boundary, but I can work with an 80% <strong>probability</strong> density boundary instead.  </p>
  
  <p>Here's where I'm looking for help.  The <code>bin</code> parameter for <code>kde2d</code> (used by <code>stat_density2d</code>) is not clearly documented.  If I set <code>bin</code> = 4 in the example below, am I correct in interpreting the central (green) region as containing a 25% probability mass and the combined yellow, red, and green areas as representing a 75% probability mass?  If so, by changing the bin to = 5, would the area inscribed then equal an 80% probability mass?</p>
  
  <pre><code>set.seed(1)
  n=100
  
  df &lt;- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))
  
  TestData &lt;- ggplot (data = df) +
    stat_density2d(aes(x = x, y = y, fill = as.factor(..level..)), 
    bins=4, geom = "polygon", ) +
    geom_point(aes(x = x, y = y)) +
    scale_fill_manual(values = c("yellow","red","green","royalblue", "black"))
  
  TestData
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/XvqMK.png" alt="enter image description here"></p>
  
  <p>I repeated a number of test cases and manually counted the excluded points [would love to find a way to count them based on what ..level.. they were contained within] but given the random nature of the data (both my real data and the test data) the number of points outside of the <code>stat_density2d</code> area varied enough to warrant asking for help.  </p>
  
  <p>Summarizing, is there a practical means of drawing a polygon around the central 80% of the population of points in the data frame?  Or, baring that, am I safe to use <code>stat_density2d</code> and set bin equal to 5 to produce an 80% probability mass? </p>
  
  <hr>
  
  <p>Excellent answer from Bryan Hanson dispelling the fuzzy notion that I could pass an undocumented <code>bin</code> parameter in <code>stat_density2d</code>.  The results looked close at values for <code>bin</code> around 4 to 6, but as he stated, the actual function is unknown and therefore not usable. </p>
  
  <p>I used the HDRegionplot as provided in the accepted answer by DWin to solve my problem.  To that, I added a center of gravity (<code>COGravity</code>) and point in polygon (<code>pnt.in.poly</code>) from the <code>SDMTools</code> package to complete the analysis.</p>
  
  <pre><code>library(MASS)
  library(coda)
  library(SDMTools)
  library(emdbook)
  library(ggplot2)
  
  
  theme_set(theme_bw(16))
  set.seed(1)
  n=100
  
  df &lt;- data.frame(x=rnorm(n, 0, 1), y=rnorm(n, 0, 1))
  
  HPDregionplot(mcmc(data.matrix(df)), prob=0.8)
  with(df, points(x,y))
  ContourLines &lt;- as.data.frame(HPDregionplot(mcmc(data.matrix(df)), prob=0.8))
  df$inpoly &lt;- pnt.in.poly(df, ContourLines[, c("x", "y")])$pip
  
  dp &lt;- df[df$inpoly == 1,]
  COG100 &lt;- as.data.frame(t(COGravity(df$x, df$y)))
  COG80 &lt;- as.data.frame(t(COGravity(dp$x, dp$y)))
  
  TestData &lt;- ggplot (data = df) +
    stat_density2d(aes(x = x, y = y, fill = as.factor(..level..)), 
    bins=5, geom = "polygon", ) +
    geom_point(aes(x = x, y = y, colour = as.factor(inpoly)), alpha = 1) +
    geom_point(data=COG100, aes(COGx, COGy),colour="white",size=2, shape = 4) +
    geom_point(data=COG80, aes(COGx, COGy),colour="green",size=4, shape = 3) +
    geom_polygon(data = ContourLines, aes(x = x, y = y), color = "blue", fill = NA) +
    scale_fill_manual(values = c("yellow","red","green","royalblue", "brown", "black", "white", "black", "white","black")) +
    scale_colour_manual(values = c("red", "black"))
  TestData 
  nrow(dp)/nrow(df) # actual number of population members inscribed within the 80% probability polgyon
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/djAz2.png" alt="enter image description here"></p>