annotate_segments: Annotate segments with alternating shading

View source: R/autoplot.R

annotate_segmentsR Documentation

Annotate segments with alternating shading

Description

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.

See Also

Other result class: as_cpt_series(), as_ggcpt(), as_json(), cpt_annotations(), cpt_export(), cpt_verify(), ggchangepoint-conditions, is_ggcpt(), new_ggcpt(), print.ggcpt()

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()

ggchangepoint documentation built on Nov. 5, 2026, 9:06 a.m.