Skip to contents

Adds alternating shaded rectangles to highlight segments between changepoints.

Usage

annotate_segments(cp, n, fill = c("grey90", "white"), alpha = 0.5, ...)

Arguments

cp

Changepoint indices (including 0 and n).

n

Length of the series.

fill

Colors for alternating segments. Defaults to c("grey90", "white").

alpha

Alpha for fill. Defaults to 0.5.

...

Additional arguments passed to annotate.

Value

A list of ggplot annotations.

Examples

library(ggplot2)
set.seed(2026)
fit <- cpt_detect(c(rnorm(50), rnorm(50, 4)), method = "pelt")
ggplot(fit$data, aes(index, value)) +
  annotate_segments(fit$changepoints$cp, n = nrow(fit$data)) +
  geom_line()