When plotting with stat_density in ggplot:
# Create the KDE surface
kde <- ggplot(data = db) +
stat_density2d_filled(
data = as.data.frame(st_coordinates(db)),
aes(x = X, y = Y, alpha = ..level..),
n = 16
) +
scale_color_viridis_c() +
theme_bw()
Is there a possibility to use my own density computed elsewhere (outside the stat_density2d function)?