Learn R Programming

ggpmisc (version 0.3.9)

stat_dens1d_labels: Replace labels in data based on 1D density

Description

stat_dens1d_labels() Sets values mapped to the label aesthetic to "" or a user provided character string based on the local density in regions of a plot panel. Its main use is together with repulsive geoms from package ggrepel. If there is no mapping to label in data, the mapping is set to rownames(data), with a message.

Usage

stat_dens1d_labels(
  mapping = NULL,
  data = NULL,
  geom = "text",
  position = "identity",
  ...,
  keep.fraction = 0.1,
  keep.number = Inf,
  keep.sparse = TRUE,
  invert.selection = FALSE,
  bw = "SJ",
  kernel = "gaussian",
  adjust = 1,
  n = 512,
  orientation = "x",
  label.fill = "",
  na.rm = TRUE,
  show.legend = FALSE,
  inherit.aes = TRUE
)

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

...

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

keep.fraction

numeric [0..1]. The fraction of the observations (or rows) in data to be retained.

keep.number

integer Set the maximum number of observations to retain, effective only if obeying keep.fraction would result in a larger number.

keep.sparse

logical If TRUE, the default, observations from the more sparse regions are retained, if FALSE those from the densest regions.

invert.selection

logical If TRUE, the complement of the selected rows are returned.

bw

numeric or character The smoothing bandwidth to be used. If numeric, the standard deviation of the smoothing kernel. If character, a rule to choose the bandwidth, as listed in bw.nrd.

kernel

character See density for details.

adjust

numeric A multiplicative bandwidth adjustment. This makes it possible to adjust the bandwidth while still using the a bandwidth estimator through an argument passed to bw. The larger the value passed to adjust the stronger the smoothing, hence decreasing sensitivity to local changes in density.

n

numeric Number of equally spaced points at which the density is to be estimated for applying the cut point. See density for details.

orientation

character The aesthetic along which density is computed. Given explicitly by setting orientation to either "x" or "y".

label.fill

character vector of length 1 or a function.

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.

Value

A copy of data with a subset of the rows retained based on the filtering criterion.

Details

stat_dens1d_labels() is designed to work together with statistics from package 'ggrepel'. To avoid text labels being plotted over unlabelled points the corresponding rows in data need to be retained but labels replaced with the empty character string, "". This makes stat_dens1d_filter unsuitable for the task. Non-the-less stat_dens1d_labels() could be useful in some other cases, as the substitution character string can be set by the user.

See Also

density used internally.

Other statistics returning a subset of data: stat_dens1d_filter(), stat_dens2d_filter(), 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() })
)

# using defaults
ggplot(data = d, aes(x, y, label = lab)) +
  geom_point() +
  stat_dens1d_labels()

# using defaults
ggplot(data = d, aes(x, y, label = lab)) +
  geom_point() +
  stat_dens1d_labels(geom = "text_repel")

# if no mapping to label is found, it is set row names
ggplot(data = d, aes(x, y)) +
  geom_point() +
  stat_dens1d_labels(geom = "text_repel")

# using defaults, along y-axis
ggplot(data = d, aes(x, y, label = lab)) +
  geom_point() +
  stat_dens1d_labels(orientation = "y", geom = "text_repel")

# example labelling with coordiantes
ggplot(data = d, aes(x, y, label = sprintf("x = %.2f\ny = %.2f", x, y))) +
  geom_point() +
  stat_dens1d_filter(colour = "red") +
  stat_dens1d_labels(geom = "text_repel", colour = "red", size = 3)

# Using geom_debug() we can see that all 100 rows in \code{d} are
# returned. But only those labelled in the previous example still contain
# the original labels.
ggplot(data = d, aes(x, y, label = lab)) +
  geom_point() +
  stat_dens1d_labels(geom = "debug")

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

ggplot(data = d, aes(x, y, label = lab, colour = group)) +
  geom_point() +
  stat_dens1d_labels(geom = "text_repel", label.fill = NA)

# we keep labels starting with "a" across the whole plot, but all in sparse
# regions. To achieve this we pass as argument to label.fill a fucntion
# instead of a character string.
label.fun <- function(x) {ifelse(grepl("^a", x), x, "")}
ggplot(data = d, aes(x, y, label = lab, colour = group)) +
  geom_point() +
  stat_dens1d_labels(geom = "text_repel", label.fill = label.fun)

# }

Run the code above in your browser using DataLab