A running cumulated density function (CDF) is computed as
cumsum(density) assuming that x steps are uniform in size.
Values are subsequently compared to the target quantiles, to identify the
regions they delimit. No interpolation is done making it crucial that
density is a long vector, as controlled by parameter n in
stat_density().
Unique and sorted values from the argument passed to quantiles are
used. Values 0 and 1 are added if not present, thus, the number of regions
returned is always one less than the length of these "normalized"
quantiles.
In 'ggplot2' (>= 4.0.0), geom_area()
when the fill aesthetic is mapped to a numeric variable or to a
factor, the fill is rendered as a gradient, making it possible to highlight
multiple quantiles within a single plot layer. Gradient
fills are supported in R (>= 4.1.0) and only by some graphic devices. This
function is of use only if gradient fills are supported! The capabilities
of the currently active device can be tested with a call to
dev.capabilities() checking the field
"patterns".
Function find_quantiles() is used in
stat_distrmix_line() and stat_distrmix_area()
to tag the regions limited by quantiles. If used on its own to
create a mapping in a call to aes(), data groupings
present in the ggplot must be described by the argument passed to
group.
The approach used is approximate and relies on assumptions that are
known to be fulfiled by the density estimates returned by specific
'ggplot2' stats such as stat_density().