1

I am trying to ascertain the identity of the data points in the top two (i.e. innermost) contours, as given by stat_density2d(). How can I retrieve the data point ids? For example:

set.seed(1235)
dat <- as.data.frame(rbind(matrix(rnorm(200,0),nrow=100),matrix(rnorm(200,3),nrow=100)))
colnames(dat) <- c('X1','X2')
rownames(dat) <- paste0('id.',1:nrow(dat))

ggplot(data=dat,aes(X1,X2))   + 
  stat_density2d(data=dat,aes(fill=..level..,alpha=..level..),bins=4,geom='polygon',colour='black') + 
  scale_fill_continuous(low="sienna1",high="sienna4")+ geom_point()

Contour plot

thanks!

Axeman
  • 32,068
  • 8
  • 81
  • 94
brian
  • 85
  • 7
  • I think this is actually not that easy... you can get the grid being contoured by calling `MASS::kde2d()` directly, but then I think it's hard to intersect the original points with the contour polygons – Calum You Jun 10 '21 at 22:17

0 Answers0