Skip to main content

annotations/
annotations.rs

1use ggplot_rs::prelude::*;
2use polars::prelude::*;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Sales data with a notable spike
6    let month: Vec<f64> = (1..=12).map(|i| i as f64).collect();
7    let sales = vec![
8        120.0, 135.0, 150.0, 180.0, 210.0, 310.0, 280.0, 250.0, 190.0, 170.0, 155.0, 140.0,
9    ];
10
11    let df = df! {
12        "month" => month,
13        "sales" => sales,
14    }?;
15
16    GGPlot::new(df)
17        .aes(Aes::new().x("month").y("sales"))
18        .geom_line()
19        .geom_point()
20        // Highlight the peak region
21        .annotate_rect(4.5, 7.5, 100.0, 320.0)
22        // Label the peak
23        .annotate_text("Summer Peak", 6.0, 330.0)
24        // Draw an arrow-like segment pointing to the max
25        .annotate_segment(7.5, 330.0, 6.2, 312.0)
26        .title("Monthly Sales with Annotations")
27        .xlab("Month")
28        .ylab("Sales ($)")
29        .save("annotations.svg")?;
30
31    println!("Saved annotations.svg");
32    Ok(())
33}