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")
Run the code above in your browser using DataLab