Skip to contents

Runs changepoint detection inside the ggplot pipeline. Useful for quick exploration: ggplot(df, aes(t, y)) + geom_line() + stat_changepoint(method = "pelt"). Draws vertical lines at detected changepoint locations.

Usage

stat_changepoint(
  mapping = NULL,
  data = NULL,
  geom = "vline",
  position = "identity",
  ...,
  method = "pelt",
  change_in = "mean",
  na.rm = FALSE,
  show.legend = NA
)

Arguments

mapping

Aesthetic mappings.

data

A data frame.

geom

The geometric object to use (default: "vline"). The stat computes a single xintercept per changepoint, so only geoms that consume that aesthetic fit — "vline" and "rug". A geom needing x/y, such as "point", errors because the stat drops those aesthetics.

position

Position adjustment.

...

Other arguments passed to the geom.

method

Detection method (passed to cpt_detect).

change_in

What to detect change in (passed to cpt_detect).

na.rm

If FALSE, missing values are removed.

show.legend

Whether to show legend.

Value

A ggplot layer.

Examples

library(ggplot2)
set.seed(2026)
d <- data.frame(t = 1:100, y = c(rnorm(50), rnorm(50, 4)))
ggplot(d, aes(t, y)) + geom_line() +
  stat_changepoint(method = "pelt", colour = "blue")