ggpmisc (version 0.3.2)

stat_dens2d_filter: Filter observations by local density

Description

stat_dens2d_filter Filters-out/filters-in observations in regions of a plot panel with high density of observations. 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.

Usage

stat_dens2d_filter(mapping = NULL, data = NULL, geom = "point",
  position = "identity", keep.fraction = 0.1, keep.number = Inf,
  keep.sparse = TRUE, 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, na.rm = TRUE, show.legend = FALSE, inherit.aes = TRUE, h = NULL, n = NULL, ...)

Arguments

mapping

The aesthetic mapping, usually constructed with aes or aes_. Only needs to be set at the layer level if you are overriding the plot defaults.

data

A layer specific dataset - only needed if you want to override the plot defaults.

geom

The geometric object to use display the data.

position

The position adjustment to use for overlapping points on this layer

keep.fraction

numeric [0..1].

keep.number

integer number of labels to keep.

keep.sparse

logical If false the observations from the densest regions are kept.

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

show.legend

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.

inherit.aes

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.

h

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.

n

Number of grid points in each direction. Can be scalar or a length-2 integer vector

...

other arguments passed on to layer. This can include aesthetics whose values you want to set, not map. See layer for more details.

Computed variables

labels

x at centre of range

See Also

kde2d used internally.

Other statistics for selection of observations based on local density: stat_dens2d_labels

Examples

Run this code
# NOT RUN {
library(ggrepel)
library(gginnards)

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() })
)

ggplot(data = d, aes(x, y)) +
  geom_point() +
  stat_dens2d_filter(color = "red")

# Using geom_debug() we can see that only 10 out off 100 rows in \code{d} are
# returned. Those highlighted in red in the previous example.
ggplot(data = d, aes(x, y)) +
  geom_point() +
  stat_dens2d_filter(geom = "debug")

ggplot(data = d, aes(x, y)) +
  geom_point() +
  stat_dens2d_filter(color = "red", keep.fraction = 0.5)

ggplot(data = d, aes(x, y)) +
  geom_point() +
  stat_dens2d_filter(color = "red",
                     keep.fraction = 0.5,
                     keep.number = 12)

ggplot(data = d, aes(x, y, color = group)) +
  geom_point() +
  stat_dens2d_filter(shape = 1, size = 3, keep.fraction = 1/4)

ggplot(data = d, aes(x, y, color = group)) +
  geom_point() +
  stat_dens2d_filter_g(shape = 1, size = 3, keep.fraction = 1/4)

ggplot(data = d, aes(x, y, label = lab, color = group)) +
  geom_point() +
  stat_dens2d_filter(geom = "text")

ggplot(data = d, aes(x, y, label = lab, color = group)) +
  geom_point() +
  stat_dens2d_filter(geom = "text_repel")

# }

Run the code above in your browser using DataCamp Workspace