stat_rle {ggh4x} | R Documentation |
Run length encoding
Description
Run length encoding takes a vector of values and calculates the lengths of consecutive repeated values.
Usage
stat_rle(
mapping = NULL,
data = NULL,
geom = "rect",
position = "identity",
...,
align = "none",
na.rm = FALSE,
orientation = "x",
show.legend = NA,
inherit.aes = TRUE
)
Arguments
mapping |
Set of aesthetic mappings created by |
data |
The data to be displayed in this layer. There are three options: If A A |
geom |
Use to override the default connection between
|
position |
Position adjustment, either as a string naming the adjustment
(e.g. |
... |
Other arguments passed on to |
align |
A
|
na.rm |
If |
orientation |
The orientation of the layer. The default ( |
show.legend |
logical. Should this layer be included in the legends?
|
inherit.aes |
If |
Details
The data is first ordered on the x
aesthetic before run lengths are
calculated for the label
aesthetic. In contrast to base::rle()
, NA
s
are considered equivalent values, not different values.
Value
A ggplot2
layer
Aesthetics
stat_rle()
understands the following
aesthetics (required aesthetics are in bold)
x
label
group
Computed variables
- start
The
x
values at the start of every run.- end
The
x
values at the end of every run.- start_id
The index where a run starts.
- end_id
The index where a run ends.
- run_id
The index of a run.
- runlength
The length of a run.
- runvalue
The value associated with a run.
Examples
df <- data.frame(
x = seq(0, 10, length.out = 100),
y = sin(seq(0, 10, length.out = 100)*2)
)
# Label every run of increasing values
ggplot(df) +
stat_rle(aes(x, label = diff(c(0, y)) > 0),
align = "end") +
geom_point(aes(x, y))
# Label every run above some threshold
ggplot(df) +
stat_rle(aes(x, label = y > 0),
align = "center") +
geom_point(aes(x, y))
# Categorising runs, more complicated usage
ggplot(df) +
stat_rle(aes(stage(x, after_stat = run_id),
after_stat(runlength),
label = cut(y, c(-1, -0.6, 0.6, 1)),
fill = after_stat(runvalue)),
geom = "col")