ggplot2: identity of points in stat_density2d contours/bins

260 views Asked by At

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!

0

There are 0 answers