stat_rle | R Documentation |
Run length encoding takes a vector of values and calculates the lengths of consecutive repeated values.
stat_rle(
mapping = NULL,
data = NULL,
geom = "rect",
position = "identity",
...,
align = "none",
na.rm = FALSE,
orientation = "x",
show.legend = NA,
inherit.aes = TRUE
)
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 |
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.
A ggplot2
layer
stat_rle()
understands the following
aesthetics (required aesthetics are in bold)
x
label
group
The x
values at the start of every run.
The x
values at the end of every run.
The index where a run starts.
The index where a run ends.
The index of a run.
The length of a run.
The value associated with a run.
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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.