I'd like to use stat_density2D function with categorical variables but restraining my plot to high density areas, in order to reduce overlapping and increase legibility.
Let's take an example with the following data:
plot_data <-
data.frame(X = c(rnorm(300, 3, 2.5), rnorm(150, 7, 2)),
Y = c(rnorm(300, 6, 2.5), rnorm(150, 2, 2)),
Label = c(rep('A', 300), rep('B', 150)))
ggplot(plot_data, aes(X, Y, colour = Label)) + geom_point()
With a 2D-density plot we obtain overlapping densities
ggplot(plot_data, aes(X, Y)) +
stat_density_2d(geom = "polygon", aes(alpha = ..level.., fill = Label))
Would it be possible to plot only high density areas (for instance level>0.03
) ? The only solution I found is to "cheat" and manually modify the ..levels..
variable, either with step function or any power transformation, like in this simple example.
ggplot(plot_data, aes(X, Y)) +
stat_density_2d(geom = "polygon", aes(alpha = (..level..) ^ 2, fill = Label)) +
scale_alpha_continuous(range = c(0, 1))
Instead of modifying ..levels..
variable, is it possible to ask ggplot2/stat_density2D function to focus only on a certain range of density levels? I've tried to play with range
or limits
arguments of scale_alpha_continuous
function without any relevant result...
Thanks!
limits
inscale_alpha_continuous
, and set a lower and upper bound. Everything outside will simply be ignored (by default). – Axemanggplot(plot_data, aes(X,Y))+stat_density_2d(geom="polygon", aes(alpha=..level.., fill=Label)) + scale_alpha_continuous(limits=c(0.1,0.04))
. By the way you are right, this kind of modification must be explicitly stated and explained – Jonas