
Last chance! 50% off unlimited learning
Sale ends in
stat_dens2d_filter
Filters-out/filters-in observations in
regions of a plot panel with high density of observations, based on the
values mapped to both x
and y
aesthetics.
stat_dens2d_filter_g
does the filtering by group instead of by
panel. This second stat is useful for highlighting observations, while
the first one tends to be most useful when the aim is to prevent clashes
among text labels.
stat_dens2d_filter(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
keep.fraction = 0.1,
keep.number = Inf,
keep.sparse = TRUE,
invert.selection = FALSE,
na.rm = TRUE,
show.legend = FALSE,
inherit.aes = TRUE,
h = NULL,
n = NULL,
...
)stat_dens2d_filter_g(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
keep.fraction = 0.1,
keep.number = Inf,
keep.sparse = TRUE,
invert.selection = FALSE,
na.rm = TRUE,
show.legend = FALSE,
inherit.aes = TRUE,
h = NULL,
n = NULL,
...
)
A layer specific dataset - only needed if you want to override the plot defaults.
The geometric object to use display the data.
The position adjustment to use for overlapping points on this layer
numeric [0..1]. The fraction of the observations (or
rows) in data
to be retained.
integer Set the maximum number of observations to retain,
effective only if obeying keep.fraction
would result in a larger
number.
logical If TRUE
, the default, observations from the
more sparse regions are retained, if FALSE
those from the densest
regions.
logical If TRUE
, the complement of the
selected rows are returned.
a logical value indicating whether NA values should be stripped before the computation proceeds.
logical. Should this layer be included in the legends?
NA
, the default, includes if any aesthetics are mapped. FALSE
never includes, and TRUE
always includes.
If FALSE
, overrides the default aesthetics, rather
than combining with them. This is most useful for helper functions that
define both data and aesthetics and shouldn't inherit behaviour from the
default plot specification, e.g. borders
.
vector of bandwidths for x and y directions. Defaults to normal reference bandwidth (see bandwidth.nrd). A scalar value will be taken to apply to both directions.
Number of grid points in each direction. Can be scalar or a length-2 integer vector
A plot layer instance. Using as output data
a subset of the
rows in input data
retained based on a 2D-density-based filtering
criterion.
kde2d
used internally.
Other statistics returning a subset of data:
stat_dens1d_filter()
,
stat_dens1d_labels()
,
stat_dens2d_labels()
# NOT RUN {
library(ggrepel)
random_string <- function(len = 6) {
paste(sample(letters, len, replace = TRUE), collapse = "")
}
# Make random data.
set.seed(1001)
d <- tibble::tibble(
x = rnorm(100),
y = rnorm(100),
group = rep(c("A", "B"), c(50, 50)),
lab = replicate(100, { random_string() })
)
# filter (and here highlight) 1/10 observations in sparsest regions
ggplot(data = d, aes(x, y)) +
geom_point() +
stat_dens2d_filter(colour = "red")
# filter observations not in the sparsest regions
ggplot(data = d, aes(x, y)) +
geom_point() +
stat_dens2d_filter(colour = "blue", invert.selection = TRUE)
# filter observations in dense regions of the plot
ggplot(data = d, aes(x, y)) +
geom_point() +
stat_dens2d_filter(colour = "blue", keep.sparse = FALSE)
# filter 1/2 the observations
ggplot(data = d, aes(x, y)) +
geom_point() +
stat_dens2d_filter(colour = "red", keep.fraction = 0.5)
# filter 1/2 the observations but cap their number to maximum 12 observations
ggplot(data = d, aes(x, y)) +
geom_point() +
stat_dens2d_filter(colour = "red",
keep.fraction = 0.5,
keep.number = 12)
# density filtering done jointly across groups
ggplot(data = d, aes(x, y, colour = group)) +
geom_point() +
stat_dens2d_filter(shape = 1, size = 3, keep.fraction = 1/4)
# density filtering done independently for each group
ggplot(data = d, aes(x, y, colour = group)) +
geom_point() +
stat_dens2d_filter_g(shape = 1, size = 3, keep.fraction = 1/4)
# density filtering done jointly across groups by overriding grouping
ggplot(data = d, aes(x, y, colour = group)) +
geom_point() +
stat_dens2d_filter_g(colour = "black",
shape = 1, size = 3, keep.fraction = 1/4)
# label observations
ggplot(data = d, aes(x, y, label = lab, colour = group)) +
geom_point() +
stat_dens2d_filter(geom = "text")
# repulsive labels with ggrepel::geom_text_repel()
ggplot(data = d, aes(x, y, label = lab, colour = group)) +
geom_point() +
stat_dens2d_filter(geom = "text_repel")
# }
Run the code above in your browser using DataLab