geom_hdr_points {ggdensity} | R Documentation |
Scatterplot colored by highest density regions of a 2D density estimate
Description
Perform 2D density estimation, compute the resulting highest density regions (HDRs), and plot the provided data as a scatterplot with points colored according to their corresponding HDR.
Usage
stat_hdr_points(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
method = "kde",
probs = c(0.99, 0.95, 0.8, 0.5),
n = 100,
xlim = NULL,
ylim = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
geom_hdr_points(
mapping = NULL,
data = NULL,
stat = "hdr_points",
position = "identity",
...,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
The geometric object to use to display the data, either as a
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
method |
Density estimator to use, accepts character vector:
|
probs |
Probabilities to compute highest density regions for. |
n |
Number of grid points in each direction. |
xlim , ylim |
Range to compute and draw regions. If |
na.rm |
If |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
stat |
The statistical transformation to use on the data for this
layer, either as a |
Aesthetics
geom_hdr_points understands the following aesthetics (required aesthetics are in bold):
-
x
-
y
alpha
color
fill
group
linetype
size
subgroup
Computed variables
- probs
The probability associated with the highest density region, specified by
probs
.
Examples
set.seed(1)
df <- data.frame(x = rnorm(500), y = rnorm(500))
p <- ggplot(df, aes(x, y)) +
coord_equal()
p + geom_hdr_points()
# Setting aes(fill = after_stat(probs)), color = "black", and
# shape = 21 helps alleviate overplotting:
p + geom_hdr_points(aes(fill = after_stat(probs)), color = "black", shape = 21, size = 2)
# Also works well with geom_hdr_lines()
p +
geom_hdr_lines(
aes(color = after_stat(probs)), alpha = 1,
xlim = c(-5, 5), ylim = c(-5, 5)
) +
geom_hdr_points(
aes(fill = after_stat(probs)), color = "black", shape = 21, size = 2,
xlim = c(-5, 5), ylim = c(-5, 5)
)