Learn R Programming

harbinger (version 1.2.747)

har_plot: Plot event detection on a time series

Description

Convenience plotting helper for Harbinger detections. It accepts a detector, the input series, an optional detection data.frame, and optional ground-truth events to color-code true positives (TP), false positives (FP), and false negatives (FN). It can also mark detected change points and draw reference horizontal lines.

Usage

har_plot(
  obj,
  serie,
  detection = NULL,
  event = NULL,
  mark.cp = TRUE,
  ylim = NULL,
  idx = NULL,
  pointsize = 0.5,
  colors = c("green", "blue", "red", "purple"),
  yline = NULL
)

Value

A ggplot object showing the time series with detected events highlighted.

Arguments

obj

A harbinger detector used to produce detection.

serie

Numeric vector with the time series to plot.

detection

Optional detection data.frame as returned by detect().

event

Optional logical vector with ground-truth events (same length as serie).

mark.cp

Logical; if TRUE, marks detected change points with dashed vertical lines.

ylim

Optional numeric vector of length 2 for y-axis limits.

idx

Optional x-axis labels or indices (defaults to seq_along(serie)).

pointsize

Base point size for observations.

colors

Character vector of length 4 with colors for TP, FN, FP, and motif segments.

yline

Optional numeric vector with y values to draw dotted horizontal lines.

References

  • Ogasawara, E., Salles, R., Porto, F., Pacitti, E. Event Detection in Time Series. 1st ed. Cham: Springer Nature Switzerland, 2025. doi:10.1007/978-3-031-75941-3

Examples

Run this code
library(daltoolbox)

# Load an example anomaly dataset
data(examples_anomalies)

# Use the simple time series
dataset <- examples_anomalies$simple
head(dataset)

# Set up an ARIMA-based anomaly detector
model <- hanr_arima()

# Fit the detector
model <- fit(model, dataset$serie)

# Run detection
detection <- detect(model, dataset$serie)

# Inspect detected events
print(detection[(detection$event),])

# Evaluate detections (soft evaluation)
evaluation <- evaluate(har_eval_soft(), detection$event, dataset$event)
print(evaluation$confMatrix)

# Plot the results
grf <- har_plot(model, dataset$serie, detection, dataset$event)
plot(grf)

Run the code above in your browser using DataLab