title: R: adding alpha bags to a 2d or 3d scatterplot
body: <p>I know that in <code>ggplot2</code> one can add the convex hull to a scatterplot by group as in</p>
  
  <pre><code>library(ggplot2)
  library(plyr)
  data(iris)
  df&lt;-iris
  find_hull &lt;- function(df) df[chull(df$Sepal.Length, df$Sepal.Width), ]
  hulls &lt;- ddply(df, "Species", find_hull)
  plot &lt;- ggplot(data = df, aes(x = Sepal.Length, y = Sepal.Width, colour=Species, fill = Species)) +
      geom_point() + 
      geom_polygon(data = hulls, alpha = 0.5) +
      labs(x = "Sepal.Length", y = "Sepal.Width")
  plot
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/AEoiG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AEoiG.png" alt="enter image description here"></a></p>
  
  <p>I was wondering though how one could calculate and add alpha bags instead, i.e. the largest convex hull that contains at least a proportion 1-alpha of all the points? Either in 2d (to display with ggplot2)  or 3d (to display with rgl).</p>
  
  <p>EDIT: My initial idea was be to keep on "peeling" the convex hull for as along as the criterion of containing at least a given % of points would be satisfied, although in the paper <a href="http://venus.unive.it/romanaz/ada2/bagplot.pdf" rel="nofollow noreferrer">here</a> it seems they use a different algorithm (isodepth, which seems to be implemented in R package depth, in function <a href="http://finzi.psych.upenn.edu/R/library/depth/html/isodepth.html" rel="nofollow noreferrer">isodepth</a> and <a href="http://finzi.psych.upenn.edu/R/library/aplpack/html/plothulls.html" rel="nofollow noreferrer">aplpack::plothulls</a> seems also close to what I want (although it produces a full plot as opposed to just the contour), so I think with these I may be sorted. Though these function only works in 2D, and I would also be interested in a 3D extension (to be plotted in rgl). If anyone has any pointers let me know!</p>
  
  <p>EDIT2: with function <code>depth::isodepth</code> I found a 2d solution (see post below), although I am still looking for a 3D solution as well - if anyone would happen to know how to do that, please let me know!</p>