4

How can I anticipate the proportion of points within the contours generated by stat_density_2d?

For example creating 50 random points

set.seed(2)

points <- tibble(x = rnorm(50), y = rnorm(50))

And then ploting density contours

ggplot(points, aes(x = x, y = y)) + 
  xlim(c(-3, 3)) +
  ylim(c(-3, 3)) +
  geom_point() +
  stat_density_2d(bins = 5)

enter image description here

Within the first contour there are 11 points (about 22%). The next contours adds about 16 (so now 54% total). And so on...

Is there a way to anticipate the proportion within a contour? I would be interested in finding the contour which contains x%. Obviously I can draw many contours and check each of them, but I was wondering if there is another solution.

ColinTea
  • 998
  • 1
  • 9
  • 15

0 Answers0