stat_peaks finds at which x positions the global y maximun or local
y maxima are located. stat_valleys finds at which x positions the
global y minimum or local y minima located. They both support filtering
of relevant peaks. Axis flipping is supported.
stat_peaks(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
span = 5,
global.threshold = 0,
local.threshold = 0,
local.reference = "median",
strict = FALSE,
label.fmt = NULL,
x.label.fmt = NULL,
y.label.fmt = NULL,
extract.peaks = NULL,
orientation = "x",
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE
)
stat_valleys(
mapping = NULL,
data = NULL,
geom = "point",
position = "identity",
...,
span = 5,
global.threshold = 0.01,
local.threshold = NULL,
local.reference = "median",
strict = FALSE,
label.fmt = NULL,
x.label.fmt = NULL,
y.label.fmt = NULL,
extract.valleys = NULL,
orientation = "x",
na.rm = FALSE,
show.legend = FALSE,
inherit.aes = TRUE
)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.
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
other arguments passed on to layer. This can
include aesthetics whose values you want to set, not map. See
layer for more details.
odd positive integer A peak is defined as an element in a
sequence which is greater than all other elements within a moving window of
width span centred at that element. The default value is 5, meaning
that a peak is taller than its four nearest neighbours. span = NULL
extends the span to the whole length of x.
numeric A value belonging to class
"AsIs" is interpreted as an absolute minimum height or depth
expressed in data units. A bare numeric value (normally between 0.0
and 1.0), is interpreted as relative to the range of the data. In both
cases it sets a global height (depth) threshold below which peaks
(valleys) are ignored. A bare negative numeric value indicates the
global height (depth) threshold below which peaks (valleys) are be
ignored. If global.threshold = NULL, no threshold is applied and all
peaks are returned.
numeric A value belonging to class "AsIs" is
interpreted as an absolute minimum height (depth) expressed in data units
relative to the within-window computed minimum (maximum) value. A bare
numeric value (normally between 0.0 and 1.0), is interpreted as
expressed in units relative to the range of the data. In both cases
local.threshold sets a local height (depth) threshold below
which peaks (valleys) are ignored. If local.threshold = NULL or if
span spans the whole of x, no threshold is applied.
character One of "minimum"/maximum or
"median". The reference used to assess the height of the peak,
either the minimum value within the window or the median of all values in
the window.
logical flag: if TRUE, an element must be strictly
greater than all other values in its window to be considered a peak.
character strings giving a format
definition for construction of character strings labels with function
sprintf from x and/or y values.
If TRUE only the rows containing
peaks or valleys are returned. If FALSE the whole of data is
returned but with labels set to NA in rows not containing peaks or
valleys. If NULL, the default, TRUE, is used unless the geom
name passed as argument is "text_repel" or "label_repel".
character The orientation of the layer can be set to
either "x", the default, or "y".
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.
A data frame with one row for each peak (or valley) found in the
data extracted from the input data or all rows in data. Added
columns contain the labels.
These stats use geom_point by default as it is the geom most
likely to work well in almost any situation without need of tweaking. The
default aesthetics set by these stats allow their direct use with
geom_text, geom_label, geom_line, geom_rug,
geom_hline and geom_vline. The formatting of the labels
returned can be controlled by the user.
Two tests make it possible to ignore irrelevant peaks or valleys. One test
controlled by (global.threshold) is based on the absolute
height/depth of peaks/valleys and can be used in all cases to ignore
globally low peaks and shallow valleys. A second test controlled by
(local.threshold) is available when the window defined by `span`
does not include all observations and can be used to ignore peaks/valleys
that are not locally prominent. In this second approach the height/depth of
each peak/valley is compared to a summary computed from other values within
the window where it was found. In this second case, the reference value
used is the summary indicated by local.reference. The values
global.threshold and local.threshold if bare numeric are
relative to the range of y. Thresholds for ignoring too small peaks
are applied after peaks are searched for, and threshold values can in some
cases result in no peaks being displayed.
Date time scales are recognized and labels formatted accordingly.
These stats work nicely together with geoms geom_text_repel and
geom_label_repel from package ggrepel to
solve the problem of overlapping labels
by displacing them. To discard overlapping labels use check_overlap =
TRUE as argument to geom_text.
By default the labels are character values ready to be added as is, but
with a suitable label.fmt labels suitable for parsing by the geoms
(e.g. into expressions containing Greek letters or super or subscripts) can
be also easily obtained.
x-value at the peak (or valley) as numeric
y-value at the peak (or valley) as numeric
x-value at the peak (or valley) formatted as character
y-value at the peak (or valley) formatted as character
Set by the statistic and available to geoms.
stat(x.label)
stat(x)
stat(y)
Required by the statistic and need to be set with aes().
numeric, wavelength in nanometres
numeric, a spectral quantity
find_peaks, which is used internally.
find_peaks, for the functions used to located the
peaks and valleys.
# lynx and Nile are time.series objects recognized by
# ggpp::ggplot.ts() and converted on-the-fly with a default mapping
# numeric, date times and dates are supported with data frames
# using defaults
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red") +
stat_valleys(colour = "blue")
# using wider window
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red", span = 11) +
stat_valleys(colour = "blue", span = 11)
# global threshold for peak height
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
global.threshold = 0.5) # half of data range
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
global.threshold = I(1100)) + # data unit
expand_limits(y = c(0, 1500))
# local (within window) threshold for peak height
# narrow peaks at the tip and locally tall
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
span = 9,
local.threshold = 0.3,
local.reference = "farthest")
# with narrower window
ggplot(Nile) +
geom_line() +
stat_peaks(colour = "red",
span = 5,
local.threshold = 0.25,
local.reference = "farthest")
ggplot(lynx) +
geom_line() +
stat_peaks(colour = "red",
local.threshold = 1/5,
local.reference = "median")
ggplot(Nile) +
geom_line() +
stat_valleys(colour = "blue",
global.threshold = I(700))
# orientation is supported
ggplot(lynx, aes(lynx, time)) +
geom_line(orientation = "y") +
stat_peaks(colour = "red", orientation = "y") +
stat_valleys(colour = "blue", orientation = "y")
# default aesthetic mapping supports additional geoms
ggplot(lynx) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red", geom = "rug")
ggplot(lynx) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red", geom = "text", hjust = -0.1, angle = 33)
ggplot(lynx, aes(lynx, time)) +
geom_line(orientation = "y") +
stat_peaks(colour = "red", orientation = "y") +
stat_peaks(colour = "red", orientation = "y",
geom = "text", hjust = -0.1)
# Force conversion of time series time into POSIXct date time
ggplot(lynx, as.numeric = FALSE) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red",
geom = "text",
hjust = -0.1,
x.label.fmt = "%Y",
angle = 33)
ggplot(Nile, as.numeric = FALSE) +
geom_line() +
stat_peaks(colour = "red") +
stat_peaks(colour = "red",
geom = "text_s",
position = position_nudge_keep(x = 0, y = 60),
hjust = -0.1,
x.label.fmt = "%Y",
angle = 90) +
expand_limits(y = 2000)
ggplot(lynx, as.numeric = FALSE) +
geom_line() +
stat_peaks(colour = "red",
geom = "text_s",
position = position_nudge_to(y = 7600),
arrow = arrow(length = grid::unit(1.5, "mm")),
point.padding = 0.7,
x.label.fmt = "%Y",
angle = 90) +
expand_limits(y = 9000)