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.
Usage
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
)
Arguments
- mapping
The aesthetic mapping, usually constructed with
aes
oraes_
. 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. Seelayer
for more details.- span
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 ofx
.- global.threshold
numeric A value belonging to class
"AsIs"
is interpreted as an absolute minimum height or depth expressed in data units. A barenumeric
value (normally between 0.0 and 1.0), is interpreted as relative tothreshold.range
. In both cases it sets a global height (depth) threshold below which peaks (valleys) are ignored. A bare negativenumeric
value indicates the global height (depth) threshold below which peaks (valleys) are be ignored. Ifglobal.threshold = NULL
, no threshold is applied and all peaks returned.- local.threshold
numeric A value belonging to class
"AsIs"
is interpreted as an absolute minimum height (depth) expressed in data units relative to a within-window computed reference value. A barenumeric
value (normally between 0.0 and 1.0), is interpreted as expressed in units relative tothreshold.range
. In both caseslocal.threshold
sets a local height (depth) threshold below which peaks (valleys) are ignored. Iflocal.threshold = NULL
or ifspan
spans the whole ofx
, no threshold is applied.- local.reference
character One of
"median"
,"median.log"
,"median.sqrt"
,"farthest"
,"farthest.log"
or"farthest.sqrt"
. The reference used to assess the height of the peak, is either the minimum/maximum value within the window or the median of all values in the window.- strict
logical flag: if
TRUE
, an element must be strictly greater than all other values in its window to be considered a peak. Default:FALSE
(since version 0.13.1).- label.fmt, x.label.fmt, y.label.fmt
character strings giving a format definition for construction of character strings labels with function
sprintf
fromx
and/ory
values.- extract.peaks, extract.valleys
If
TRUE
only the rows containing peaks or valleys are returned. IfFALSE
the whole ofdata
is returned but with labels set toNA
in rows not containing peaks or valleys. IfNULL
, the default,TRUE
, is used unless the geom name passed as argument is"text_repel"
or"label_repel"
.- orientation
character The orientation of the layer can be set to either
"x"
, the default, or"y"
.- 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, andTRUE
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 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.
Details
As find_valleys
, stat_peaks
and stat_valleys
call find_peaks
to search for peaks or valleys, this description
applies to all four functions.
Function find_peaks
is a wrapper built onto function
peaks
from splus2R, adds support for peak
height thresholds and handles span = NULL
and non-finite (including
NA) values differently than splus2R::peaks
. Instead of giving an
error when na.rm = FALSE
and x
contains NA
values,
NA
values are replaced with the smallest finite value in x
.
span = NULL
is treated as a special case and selects max(x)
.
Passing `strict = TRUE` ensures that multiple global and within window
maxima are ignored, and can result in no peaks being returned.#'
Two tests make it possible to ignore irrelevant peaks. One test
(global.threshold
) is based on the absolute height of the peaks and
can be used in all cases to ignore globally low peaks. A second test
(local.threshold
) is available when the window defined by `span`
does not include all observations and can be used to ignore peaks that are
not locally prominent. In this second approach the height of each peak is
compared to a summary computed from other values within the window of width
equal to span
where it was found. In this second case, the reference
value used within each window containing a peak is given by
local.reference
. Parameter threshold.range
determines how the
bare numeric
values passed as argument to global.threshold
and local.threshold
are scaled. The default, NULL
uses the
range of x
. 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 found. If either threshold is not available (NA
)
the returned value is a NA
vector of the same length as x
.
The local.threshold
argument is used as is when
local.reference
is "median"
or "farthest"
, i.e., the
same distance between peak and reference is used as cut-off irrespective of
the value of the reference. In cases when the prominence of peaks is
positively correlated with the baseline, a local.threshold
that
increases together with increasing computed within window median or
farthest value applies apply a less stringent height requirement in regions
with overall low height. In this case, natural logarithm or square root
weighting can be requested with `local.reference` arguments `"median.log"`,
`"farthest.log"`, `"median.sqrt"`, and `"farthest.sqrt"` as arguments for
local.reference
.
Note
stat_peaks
and stat_valleys
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.
Computed and copied variables in the returned data frame
- x
x-value at the peak (or valley) as numeric
- y
y-value at the peak (or valley) as numeric
- x.label
x-value at the peak (or valley) formatted as character
- y.label
y-value at the peak (or valley) formatted as character
Default aesthetics
These stats use geom_point
by default as
it is the geom most likely to work well in almost any situation without.
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.
Default aesthetics mapped by the statistic and available to geoms, in addtion to the automatically set required aesthetics.
- label
stat(x.label)
- xintercept
stat(x)
- yintercept
stat(y)
Required aesthetics
Required by the statistic and need to be set
with aes()
. Date time scales are recognized and labels
formatted accordingly.
- x
numeric or date time, independent variable
- y
numeric, response variable where peaks or valleys are searched
See also
find_peaks
, which is used internally.
find_peaks
, for the functions used to located the
peaks and valleys.
Examples
# 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)