Skip to main content

log_scale/
log_scale.rs

1use ggplot_rs::prelude::*;
2use polars::prelude::*;
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    // Exponential growth data (e.g., population, bacteria count)
6    let year: Vec<f64> = (0..30).map(|i| 1990.0 + i as f64).collect();
7    let count: Vec<f64> = (0..30)
8        .map(|i| {
9            let base = 100.0 * (1.15_f64).powi(i); // 15% growth per year
10            let noise = ((i * 37 + 7) % 13) as f64 / 13.0 * 0.2 + 0.9;
11            base * noise
12        })
13        .collect();
14
15    let df = df! {
16        "year" => &year,
17        "count" => &count,
18    }?;
19
20    // Linear scale — exponential curve
21    GGPlot::new(df.clone())
22        .aes(Aes::new().x("year").y("count"))
23        .geom_point()
24        .geom_line()
25        .title("Exponential Growth (Linear Scale)")
26        .xlab("Year")
27        .ylab("Count")
28        .save("log_scale_linear.svg")?;
29
30    println!("Saved log_scale_linear.svg");
31
32    // Log10 y-axis — should appear roughly linear
33    GGPlot::new(df)
34        .aes(Aes::new().x("year").y("count"))
35        .geom_point()
36        .geom_line()
37        .scale_y_log10()
38        .title("Exponential Growth (Log10 Y Scale)")
39        .xlab("Year")
40        .ylab("Count (log10)")
41        .save("log_scale_log10.svg")?;
42
43    println!("Saved log_scale_log10.svg");
44    Ok(())
45}