My goal is to use geometry geom_density2d()to draw outlines on a scatterplot in user-defined places. Consider the following code:
library(ggplot2)
n = 100
df = data.frame(x = c(rnorm(n, 0, .5), rnorm(n, 3, .5)),
y = c(rnorm(n, 1, .5), rnorm(n, 0, .5)))
ggplot(df, aes(x = x, y = y)) +
geom_density2d() +
geom_point()

This creates a standard outline graph, but there seems to be no way to manually control which outlines are drawn. The optional parameter bits and h inside can to some extent control the contour lines (passed to kde2d from MASS, which I assume), but the resulting lines do not seem interpreted.
Ideally, I could reproduce the functionality of plot.kde from the ks library, where they can be controlled with this cont argument.
library(ks)
est = kde(df)
plot(est, cont = c(50, 95))

source
share