Is there a way to add contour labels to a 2D kernel density plot made using geom_density_2d in ggplot2?
library(ggplot2)
# load data
data("meuse")
# plot
ggplot(meuse, aes(x=x, y=y)) +
geom_density_2d()
I tried extracting out the plotted data from the built ggplot object so I could pass it to geom_text_contour from metR but the data it returns is a set of points that are used to plot the contour lines, not a grid of x/y coordinates geom_text_contour would need.
# extract denisty 2d
data <- ggplot_build(p)$data[[1]]
p + geom_path(data=data,
mapping=aes(color=nlevel, group=group),
color='red')
I want to make something like this (this example is from the geom_text_contour documentation).



