ggplot-rs 0.28.0

A Rust implementation of ggplot2's Grammar of Graphics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Bivariate fill scale: two binned variables → an `n × m` palette
//! (biscale-style; Stevens 1994's bivariate-choropleth method), backing a
//! `::BIVARIATE` choropleth. Ported from the design in
//! anofox-visualization's `docs/plans/plan-maps.md` (§15, D10) — DataZooDE/
//! anofox-visualization#318, #182.
//!
//! The *binning itself* (computing each variable's tertile/class index) is
//! the caller's job, same as [`super::binned::ScaleFillBinned`]'s break
//! maths — this scale only colours an already-binned composite class.
//! Follow the `biscale` R package's own convention: a composite class is a
//! 1-indexed `"{x_class}-{y_class}"` string (e.g. `"3-2"` = x-tertile 3,
//! y-tertile 2), the value a `bi_class()`-style data-prep step would put in
//! a `fill` column.

use crate::aes::Aesthetic;
use crate::data::Value;

use super::Scale;

/// The colour for class `(i, j)` (0-indexed) of an `nx × ny` grid: each
/// channel of `base` is pulled toward the matching channel of `color_x` by
/// `tx = i/(nx-1)` and toward `color_y` by `ty = j/(ny-1)`, subtracted
/// (not averaged) so the two axes compound rather than cancel —
/// `base − tx·(base − color_x) − ty·(base − color_y)`, clamped to
/// `0..=255`. At `(0, 0)` this is exactly `base`; at `(nx-1, 0)` exactly
/// `color_x`; at `(0, ny-1)` exactly `color_y`; at the high-high corner the
/// two subtractions compound (usually clamping low), the classic
/// bivariate-choropleth "dark/saturated corner" look. `base` is the
/// low-low cell's colour — white for a light theme; pass a theme's card
/// colour for a "lifted over the card colour" dark variant (plan-maps.md
/// §15's D10) instead of guessing a theme from inside this crate.
pub fn bivariate_cell_color(
    base: (u8, u8, u8),
    color_x: (u8, u8, u8),
    color_y: (u8, u8, u8),
    nx: usize,
    ny: usize,
    i: usize,
    j: usize,
) -> (u8, u8, u8) {
    let tx = if nx <= 1 {
        1.0
    } else {
        i as f64 / (nx - 1) as f64
    };
    let ty = if ny <= 1 {
        1.0
    } else {
        j as f64 / (ny - 1) as f64
    };
    let chan = |b: u8, x: u8, y: u8| -> u8 {
        let v = b as f64 - tx * (b as f64 - x as f64) - ty * (b as f64 - y as f64);
        v.clamp(0.0, 255.0).round() as u8
    };
    (
        chan(base.0, color_x.0, color_y.0),
        chan(base.1, color_x.1, color_y.1),
        chan(base.2, color_x.2, color_y.2),
    )
}

/// Parse a biscale-style `"{x}-{y}"` composite class (1-indexed) into a
/// 0-indexed `(i, j)`, clamped into `0..nx`/`0..ny`.
fn parse_class(s: &str, nx: usize, ny: usize) -> Option<(usize, usize)> {
    let (xs, ys) = s.split_once('-')?;
    let x: i64 = xs.trim().parse().ok()?;
    let y: i64 = ys.trim().parse().ok()?;
    let i = (x - 1).clamp(0, nx as i64 - 1) as usize;
    let j = (y - 1).clamp(0, ny as i64 - 1) as usize;
    Some((i, j))
}

/// Bivariate fill scale: `nx × ny` classes, each class `"{i}-{j}"`
/// (1-indexed) coloured by [`bivariate_cell_color`]. See the
/// [module docs](self).
#[derive(Clone, Debug)]
pub struct ScaleFillBivariate {
    aesthetic: Aesthetic,
    nx: usize,
    ny: usize,
    base: (u8, u8, u8),
    color_x: (u8, u8, u8),
    color_y: (u8, u8, u8),
    x_name: String,
    y_name: String,
    name: String,
}

impl ScaleFillBivariate {
    /// `nx × ny` classes (biscale's usual is 3×3), tinted from `base`
    /// toward `color_x`/`color_y`. `x_name`/`y_name` are the two legend
    /// axis captions (the mapped variables' names).
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        aesthetic: Aesthetic,
        nx: usize,
        ny: usize,
        base: (u8, u8, u8),
        color_x: (u8, u8, u8),
        color_y: (u8, u8, u8),
        x_name: impl Into<String>,
        y_name: impl Into<String>,
    ) -> Self {
        ScaleFillBivariate {
            aesthetic,
            nx: nx.max(1),
            ny: ny.max(1),
            base,
            color_x,
            color_y,
            x_name: x_name.into(),
            y_name: y_name.into(),
            name: String::new(),
        }
    }

    /// The classic biscale 3×3, white base.
    pub fn tertile(
        aesthetic: Aesthetic,
        color_x: (u8, u8, u8),
        color_y: (u8, u8, u8),
        x_name: impl Into<String>,
        y_name: impl Into<String>,
    ) -> Self {
        Self::new(
            aesthetic,
            3,
            3,
            (255, 255, 255),
            color_x,
            color_y,
            x_name,
            y_name,
        )
    }

    pub fn nx(&self) -> usize {
        self.nx
    }
    pub fn ny(&self) -> usize {
        self.ny
    }
    pub fn base(&self) -> (u8, u8, u8) {
        self.base
    }
    pub fn color_x(&self) -> (u8, u8, u8) {
        self.color_x
    }
    pub fn color_y(&self) -> (u8, u8, u8) {
        self.color_y
    }
    pub fn x_name(&self) -> &str {
        &self.x_name
    }
    pub fn y_name(&self) -> &str {
        &self.y_name
    }

    fn cell_color(&self, i: usize, j: usize) -> (u8, u8, u8) {
        bivariate_cell_color(
            self.base,
            self.color_x,
            self.color_y,
            self.nx,
            self.ny,
            i,
            j,
        )
    }

    fn class_label(&self, i: usize, j: usize) -> String {
        format!("{}-{}", i + 1, j + 1)
    }
}

impl Scale for ScaleFillBivariate {
    fn aesthetic(&self) -> Aesthetic {
        self.aesthetic.clone()
    }

    /// No-op: the domain is the fixed `nx × ny` grid, not derived from
    /// data (same reasoning as [`super::steps::ScaleColorSteps`]/
    /// [`super::binned::ScaleFillBinned`]).
    fn train(&mut self, _values: &[Value]) {}

    fn map(&self, _value: &Value) -> f64 {
        0.0
    }

    /// Every `(i, j)` cell, in row-major order — the full grid regardless
    /// of which classes actually appear in the data, so a legend (a
    /// dedicated grid, not the standard discrete-swatch list — see
    /// [`crate::geom::bivariate_legend`]) always shows the complete
    /// palette.
    fn breaks(&self) -> Vec<(f64, String)> {
        let mut out = Vec::with_capacity(self.nx * self.ny);
        for j in 0..self.ny {
            for i in 0..self.nx {
                out.push((0.0, self.class_label(i, j)));
            }
        }
        out
    }

    fn name(&self) -> &str {
        &self.name
    }

    fn set_name(&mut self, name: &str) {
        self.name = name.to_string();
    }

    fn is_discrete(&self) -> bool {
        true
    }

    fn map_to_color(&self, value: &Value) -> Option<(u8, u8, u8)> {
        let s = match value {
            Value::Str(s) => s.as_str(),
            _ => return None,
        };
        let (i, j) = parse_class(s, self.nx, self.ny)?;
        Some(self.cell_color(i, j))
    }

    fn clone_box(&self) -> Box<dyn Scale> {
        Box::new(self.clone())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn low_low_is_the_base_colour() {
        let s = ScaleFillBivariate::tertile(
            Aesthetic::Fill,
            (220, 0, 0),
            (0, 0, 220),
            "income",
            "education",
        );
        assert_eq!(
            s.map_to_color(&Value::Str("1-1".into())),
            Some((255, 255, 255))
        );
    }

    #[test]
    fn high_x_only_tints_toward_color_x() {
        let s = ScaleFillBivariate::tertile(Aesthetic::Fill, (220, 0, 0), (0, 0, 220), "x", "y");
        let c = s.map_to_color(&Value::Str("3-1".into())).unwrap();
        // Multiply-blended with white (255,255,255) in the y channel is a
        // no-op, so this should equal plain color_x.
        assert_eq!(c, (220, 0, 0));
    }

    #[test]
    fn high_high_is_darker_than_either_axis_alone() {
        let s = ScaleFillBivariate::tertile(Aesthetic::Fill, (220, 0, 0), (0, 0, 220), "x", "y");
        let hh = s.map_to_color(&Value::Str("3-3".into())).unwrap();
        let hx = s.map_to_color(&Value::Str("3-1".into())).unwrap();
        let hy = s.map_to_color(&Value::Str("1-3".into())).unwrap();
        let luma = |(r, g, b): (u8, u8, u8)| 0.299 * r as f64 + 0.587 * g as f64 + 0.114 * b as f64;
        assert!(luma(hh) < luma(hx), "high-high darker than high-x-only");
        assert!(luma(hh) < luma(hy), "high-high darker than high-y-only");
    }

    #[test]
    fn all_nine_cells_are_distinguishable() {
        let s =
            ScaleFillBivariate::tertile(Aesthetic::Fill, (217, 95, 2), (117, 112, 179), "x", "y");
        let mut colors = Vec::new();
        for j in 1..=3 {
            for i in 1..=3 {
                colors.push(s.map_to_color(&Value::Str(format!("{i}-{j}"))).unwrap());
            }
        }
        for a in 0..colors.len() {
            for b in (a + 1)..colors.len() {
                assert_ne!(
                    colors[a], colors[b],
                    "cells {a} and {b} collide: {colors:?}"
                );
            }
        }
    }

    /// sRGB → CIE Lab, for the ΔE (CIE76) check below. Standard D65
    /// conversion (sRGB → linear → XYZ → Lab).
    fn srgb_to_lab((r, g, b): (u8, u8, u8)) -> (f64, f64, f64) {
        let to_linear = |c: u8| {
            let c = c as f64 / 255.0;
            if c <= 0.04045 {
                c / 12.92
            } else {
                ((c + 0.055) / 1.055).powf(2.4)
            }
        };
        let (rl, gl, bl) = (to_linear(r), to_linear(g), to_linear(b));
        // sRGB -> XYZ (D65)
        let x = rl * 0.4124564 + gl * 0.3575761 + bl * 0.1804375;
        let y = rl * 0.2126729 + gl * 0.7151522 + bl * 0.0721750;
        let z = rl * 0.0193339 + gl * 0.1191920 + bl * 0.9503041;
        // D65 white point
        let (xn, yn, zn) = (0.95047, 1.0, 1.08883);
        let f = |t: f64| {
            if t > (6.0f64 / 29.0).powi(3) {
                t.cbrt()
            } else {
                t / (3.0 * (6.0f64 / 29.0).powi(2)) + 4.0 / 29.0
            }
        };
        let (fx, fy, fz) = (f(x / xn), f(y / yn), f(z / zn));
        (116.0 * fy - 16.0, 500.0 * (fx - fy), 200.0 * (fy - fz))
    }

    /// CIE76 ΔE: Euclidean distance in Lab space.
    fn delta_e(a: (u8, u8, u8), b: (u8, u8, u8)) -> f64 {
        let (l1, a1, b1) = srgb_to_lab(a);
        let (l2, a2, b2) = srgb_to_lab(b);
        ((l1 - l2).powi(2) + (a1 - a2).powi(2) + (b1 - b2).powi(2)).sqrt()
    }

    /// plan-maps.md §15 D10's own acceptance test: the 9 cells stay
    /// perceptually distinguishable (CIE76 ΔE above a "just noticeable
    /// difference" floor) in both a light-theme (white base) and a
    /// dark-theme ("lifted over the card colour") palette.
    #[test]
    fn all_nine_cells_pass_a_cie76_delta_e_floor_in_light_and_dark() {
        const JND: f64 = 5.0;
        for base in [(255, 255, 255), (45, 45, 48)] {
            let s = ScaleFillBivariate::new(
                Aesthetic::Fill,
                3,
                3,
                base,
                (217, 95, 2),
                (117, 112, 179),
                "x",
                "y",
            );
            let mut colors = Vec::new();
            for j in 1..=3 {
                for i in 1..=3 {
                    colors.push(s.map_to_color(&Value::Str(format!("{i}-{j}"))).unwrap());
                }
            }
            for a in 0..colors.len() {
                for b in (a + 1)..colors.len() {
                    let de = delta_e(colors[a], colors[b]);
                    assert!(
                        de > JND,
                        "base {base:?}: cells {a} ({:?}) and {b} ({:?}) too close: ΔE={de:.2}",
                        colors[a],
                        colors[b]
                    );
                }
            }
        }
    }

    #[test]
    fn class_label_round_trips() {
        let s = ScaleFillBivariate::tertile(Aesthetic::Fill, (1, 2, 3), (4, 5, 6), "x", "y");
        for (_, label) in s.breaks() {
            assert!(
                s.map_to_color(&Value::Str(label.clone())).is_some(),
                "{label}"
            );
        }
        assert_eq!(s.breaks().len(), 9);
    }

    #[test]
    fn out_of_range_class_clamps_instead_of_panicking() {
        let s = ScaleFillBivariate::tertile(Aesthetic::Fill, (1, 2, 3), (4, 5, 6), "x", "y");
        assert_eq!(
            s.map_to_color(&Value::Str("9-9".into())),
            s.map_to_color(&Value::Str("3-3".into()))
        );
        assert_eq!(
            s.map_to_color(&Value::Str("0-0".into())),
            s.map_to_color(&Value::Str("1-1".into()))
        );
    }

    #[test]
    fn malformed_class_string_returns_none() {
        let s = ScaleFillBivariate::tertile(Aesthetic::Fill, (1, 2, 3), (4, 5, 6), "x", "y");
        assert_eq!(s.map_to_color(&Value::Str("nope".into())), None);
        assert_eq!(s.map_to_color(&Value::Float(1.0)), None);
    }

    #[test]
    fn custom_grid_shape_and_base() {
        let s = ScaleFillBivariate::new(
            Aesthetic::Fill,
            4,
            2,
            (240, 240, 240),
            (200, 0, 0),
            (0, 0, 200),
            "x",
            "y",
        );
        assert_eq!(s.nx(), 4);
        assert_eq!(s.ny(), 2);
        assert_eq!(s.breaks().len(), 8);
        assert_eq!(
            s.map_to_color(&Value::Str("1-1".into())),
            Some((240, 240, 240))
        );
    }

    /// The boilerplate `Scale` trait methods: `map`/`name`/`set_name`/
    /// `is_discrete`/`train`/`clone_box`. These aren't reached by a plot
    /// that suppresses the standard per-aesthetic legend (the usual way to
    /// use this scale, since the legend is `GeomBivariateLegend` instead —
    /// `is_discrete`/`breaks`/`name` are legend-routing hooks, not needed
    /// just to colour the data), so they need their own direct coverage.
    #[test]
    fn boilerplate_scale_trait_methods() {
        let mut s =
            ScaleFillBivariate::tertile(Aesthetic::Fill, (200, 0, 0), (0, 0, 200), "x", "y");
        assert_eq!(s.aesthetic(), Aesthetic::Fill);
        assert_eq!(s.map(&Value::Str("1-1".into())), 0.0);
        assert_eq!(s.name(), "");
        s.set_name("bivariate");
        assert_eq!(s.name(), "bivariate");
        assert!(s.is_discrete());
        s.train(&[Value::Str("1-1".into())]); // no-op; must not panic
        let cloned = s.clone_box();
        assert_eq!(cloned.name(), "bivariate");
        assert_eq!(
            cloned.map_to_color(&Value::Str("3-3".into())),
            s.map_to_color(&Value::Str("3-3".into()))
        );
    }

    /// `nx == 1` or `ny == 1` (a one-dimensional "bivariate" — degenerate):
    /// `tx`/`ty` fall back to `1.0` (the single column/row is treated as
    /// the end of that axis's ramp) rather than dividing by zero.
    #[test]
    fn single_row_or_column_grid_does_not_panic() {
        let s = ScaleFillBivariate::new(
            Aesthetic::Fill,
            1,
            3,
            (255, 255, 255),
            (200, 0, 0),
            (0, 0, 200),
            "x",
            "y",
        );
        for j in 1..=3 {
            assert!(s.map_to_color(&Value::Str(format!("1-{j}"))).is_some());
        }
        // The single column is always tx=1.0 (end of the x ramp): row 1
        // (ty=0, no y contribution) is exactly color_x.
        assert_eq!(s.map_to_color(&Value::Str("1-1".into())), Some((200, 0, 0)));

        let s2 = ScaleFillBivariate::new(
            Aesthetic::Fill,
            3,
            1,
            (255, 255, 255),
            (200, 0, 0),
            (0, 0, 200),
            "x",
            "y",
        );
        for i in 1..=3 {
            assert!(s2.map_to_color(&Value::Str(format!("{i}-1"))).is_some());
        }
        assert_eq!(
            s2.map_to_color(&Value::Str("1-1".into())),
            Some((0, 0, 200))
        );
    }
}