I have a dataset with lat/long coordinates and a binary class of interest.
# Simulating the dataset
df <- tibble(x = rnorm(100),
y = rnorm(100),
class = rbernoulli(100))
Now, my objective is to visualize how the rate of class == 1
varies in the 2D space defined by x
and y
. I'm aware of:
# Density of points
ggplot(df) +
geom_density_2d(aes(x = x, y = y))
But I would need to do the same plot with the percentage of class == 1
. instead of the density of points itself. It would be basically the ratio of density corresponding to class == 1
by the density corresponding to the whole dataset.
Is there a way to maybe tweak stat_density_2d()
in order to achieve that goal? Or alternatively, is there any method to obtain this plot?