set.seed(133)
x=rnorm(200)
y=rnorm(200)
d = hexDensity(x=x,y=y,bandwidth=0.4)
cutoff=quantile(d@count,0.9)
lines = hexContour(d,cutoff)
library(ggplot2)
library(hexbin)
#plot against density
ggplot()+
geom_point(
aes(x=hcell2xy(d)$x,
y=hcell2xy(d)$y,
col=d@count)
) +
scale_color_viridis_c()+
geom_path(
aes(
x = lines[[1]]$x, y = lines[[1]]$y, group = lines[[1]]$id
)
)
#plot against data points
ggplot() +
geom_point(
aes(x=x,y=y)) +
geom_path(
aes(
x = lines[[1]]$x, y = lines[[1]]$y, group = lines[[1]]$id
)
)
Run the code above in your browser using DataLab