ggplot-rs 0.30.1

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
use crate::aes::Aesthetic;
use crate::coord::Coord;
use crate::data::{DataFrame, Value};
use crate::position::identity::PositionIdentity;
use crate::position::Position;
use crate::render::backend::{DrawBackend, LineStyle, Linetype, PointShape, PointStyle, RectStyle};
use crate::render::RenderError;
use crate::scale::ScaleSet;
use crate::stat::boxplot::StatBoxplot;
use crate::stat::Stat;
use crate::theme::Theme;

use super::{Geom, GeomParams};

/// Boxplot geometry.
pub struct GeomBoxplot {
    pub fill: (u8, u8, u8),
    pub color: (u8, u8, u8),
    pub width: f64,
    pub alpha: f64,
}

impl Default for GeomBoxplot {
    fn default() -> Self {
        GeomBoxplot {
            fill: (255, 255, 255),
            color: (50, 50, 50),
            width: 0.75,
            alpha: 1.0,
        }
    }
}

/// Parse a boxplot `outliers` cell (B3-5, D4 extension): a comma list,
/// optionally wrapped in `[...]`/`(...)` (`"7, 9"` or `"[7, 9]"`).
/// Returns the successfully-parsed values and whether any part was
/// unparsable (skipped).
fn parse_outliers(s: &str) -> (Vec<f64>, bool) {
    let trimmed = s.trim();
    let inner = trimmed
        .strip_prefix('[')
        .and_then(|t| t.strip_suffix(']'))
        .or_else(|| trimmed.strip_prefix('(').and_then(|t| t.strip_suffix(')')))
        .unwrap_or(trimmed);
    let mut out = Vec::new();
    let mut bad = false;
    for part in inner.split(',') {
        let part = part.trim();
        if part.is_empty() {
            continue;
        }
        match part.parse::<f64>() {
            Ok(v) if v.is_finite() => out.push(v),
            _ => bad = true,
        }
    }
    (out, bad)
}

impl Geom for GeomBoxplot {
    fn draw(
        &self,
        data: &DataFrame,
        coord: &dyn Coord,
        scales: &ScaleSet,
        _theme: &Theme,
        backend: &mut dyn DrawBackend,
    ) -> Result<(), RenderError> {
        let x_col = data
            .column("x")
            .ok_or(RenderError::MissingAesthetic("x".into()))?;
        let lower_col = data.column("lower");
        let middle_col = data.column("middle");
        let upper_col = data.column("upper");
        let ymin_col = data.column("ymin");
        let ymax_col = data.column("ymax");
        let outliers_col = data.column("outliers");
        let fill_col = data.column("fill");

        let plot_area = backend.plot_area();
        let x_scale = scales.get(&Aesthetic::X);
        let y_scale = scales.get(&Aesthetic::Y);

        let x_is_discrete = x_scale.map(|s| s.is_discrete()).unwrap_or(false);

        for i in 0..data.nrows() {
            let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.5);

            let half_width = if x_is_discrete {
                let n = x_scale.map(|s| s.breaks().len()).unwrap_or(1);
                self.width / (n.max(1) as f64 * 2.5)
            } else {
                0.03
            };

            // Get boxplot values
            let lower = lower_col.and_then(|c| c[i].as_f64()).unwrap_or(0.0);
            let middle = middle_col.and_then(|c| c[i].as_f64()).unwrap_or(0.0);
            let upper = upper_col.and_then(|c| c[i].as_f64()).unwrap_or(0.0);
            let ymin = ymin_col.and_then(|c| c[i].as_f64()).unwrap_or(lower);
            let ymax = ymax_col.and_then(|c| c[i].as_f64()).unwrap_or(upper);

            let map_y = |v: f64| y_scale.map(|s| s.map(&Value::Float(v))).unwrap_or(0.0);

            // Hover tooltip for the whole box: group + median (IQR) — the
            // geom's own default. B3-5: `set_row_extras` below overrides it
            // with a mapped `Aes::tooltip`/`data_attr`, if any, same as
            // every other tooltip-consuming geom.
            let group = super::tip_value(&x_col[i]);
            let r = |v: f64| (v * 100.0).round() / 100.0;
            let tip = if group.is_empty() {
                format!("median {} ({}–{})", r(middle), r(lower), r(upper))
            } else {
                format!("{group}: median {} ({}–{})", r(middle), r(lower), r(upper))
            };
            let middle_value = Value::Float(middle);
            super::set_mark(
                backend,
                Some(tip),
                Some(group.clone()).filter(|g| !g.is_empty()),
                super::series_key(data, i).or(Some(group).filter(|g| !g.is_empty())),
                super::raw_value(&middle_value),
            );
            super::set_mark_formatted(
                backend,
                scales,
                Some(&x_col[i]),
                &Aesthetic::Y,
                Some(&middle_value),
            );
            super::set_row_extras(backend, data, i);

            // Box (IQR) — honour a mapped `fill` (e.g. by group), else the default.
            let box_fill = fill_col
                .and_then(|fc| scales.map_color(&Aesthetic::Fill, &fc[i]))
                .unwrap_or(self.fill);
            let (box_left, box_top) = coord.transform((nx - half_width, map_y(upper)), &plot_area);
            let (box_right, box_bottom) =
                coord.transform((nx + half_width, map_y(lower)), &plot_area);
            backend.draw_rect(
                (box_left, box_top),
                (box_right, box_bottom),
                &RectStyle {
                    fill: Some(box_fill),
                    stroke: Some(self.color),
                    stroke_width: 1.0,
                    alpha: self.alpha,
                    clip: true,
                    corner_radius: 0.0,
                },
            )?;

            // Median line
            let (med_left, med_y) = coord.transform((nx - half_width, map_y(middle)), &plot_area);
            let (med_right, _) = coord.transform((nx + half_width, map_y(middle)), &plot_area);
            backend.draw_line(
                &[(med_left, med_y), (med_right, med_y)],
                &LineStyle {
                    color: self.color,
                    width: 2.0,
                    alpha: 1.0,
                    linetype: Linetype::Solid,
                    ..Default::default()
                },
            )?;

            // Lower whisker
            let (_, whisker_bottom) = coord.transform((nx, map_y(ymin)), &plot_area);
            let (center_x, _) = coord.transform((nx, map_y(lower)), &plot_area);
            backend.draw_line(
                &[(center_x, box_bottom), (center_x, whisker_bottom)],
                &LineStyle {
                    color: self.color,
                    width: 1.0,
                    alpha: 1.0,
                    linetype: Linetype::Solid,
                    ..Default::default()
                },
            )?;
            // Lower whisker cap
            let (wl, _) = coord.transform((nx - half_width * 0.5, map_y(ymin)), &plot_area);
            let (wr, _) = coord.transform((nx + half_width * 0.5, map_y(ymin)), &plot_area);
            backend.draw_line(
                &[(wl, whisker_bottom), (wr, whisker_bottom)],
                &LineStyle {
                    color: self.color,
                    width: 1.0,
                    alpha: 1.0,
                    linetype: Linetype::Solid,
                    ..Default::default()
                },
            )?;

            // Upper whisker
            let (_, whisker_top) = coord.transform((nx, map_y(ymax)), &plot_area);
            backend.draw_line(
                &[(center_x, box_top), (center_x, whisker_top)],
                &LineStyle {
                    color: self.color,
                    width: 1.0,
                    alpha: 1.0,
                    linetype: Linetype::Solid,
                    ..Default::default()
                },
            )?;
            // Upper whisker cap
            let (wl, _) = coord.transform((nx - half_width * 0.5, map_y(ymax)), &plot_area);
            let (wr, _) = coord.transform((nx + half_width * 0.5, map_y(ymax)), &plot_area);
            backend.draw_line(
                &[(wl, whisker_top), (wr, whisker_top)],
                &LineStyle {
                    color: self.color,
                    width: 1.0,
                    alpha: 1.0,
                    linetype: Linetype::Solid,
                    ..Default::default()
                },
            )?;

            // Outliers — `setup_data` has already normalised this cell to a
            // clean, bracket-free "v1, v2" list (any unparsable part was
            // warned on and dropped there), so a plain split/parse suffices.
            if let Some(oc) = outliers_col {
                if let Value::Str(s) = &oc[i] {
                    for part in s.split(',') {
                        if let Ok(val) = part.trim().parse::<f64>() {
                            let ny = map_y(val);
                            let (ox, oy) = coord.transform((nx, ny), &plot_area);
                            backend.draw_circle(
                                (ox, oy),
                                2.0,
                                &PointStyle {
                                    color: self.color,
                                    alpha: 1.0,
                                    filled: false,
                                    shape: PointShape::Circle,
                                },
                            )?;
                        }
                    }
                }
            }
        }
        super::clear_mark(backend);
        super::clear_row_extras(backend);

        Ok(())
    }

    fn required_aes(&self) -> Vec<Aesthetic> {
        // B3-5: `x` is always required; whether `y` or `lower`/`middle`/
        // `upper` is required depends on the stat (identity vs the default
        // `StatBoxplot`) — see `validate_layer`, which build.rs calls
        // separately because `required_aes` has no access to the stat.
        vec![Aesthetic::X]
    }

    fn default_stat(&self) -> Box<dyn Stat> {
        Box::new(StatBoxplot)
    }

    fn default_position(&self) -> Box<dyn Position> {
        Box::new(PositionIdentity)
    }

    fn default_params(&self) -> GeomParams {
        GeomParams::default()
    }

    fn name(&self) -> &str {
        "boxplot"
    }

    fn apply_element_geom(&mut self, el: &crate::theme::elements::ElementGeom) {
        if let (true, Some(ink)) = (self.color == (50, 50, 50), el.ink) {
            self.color = ink;
        }
        if let (true, Some(paper)) = (self.fill == (255, 255, 255), el.paper) {
            self.fill = paper;
        }
    }

    fn set_series_color(&mut self, color: (u8, u8, u8)) {
        self.fill = color;
    }

    fn mapped_only_columns(&self) -> &'static [&'static str] {
        // #160, follow-up to #158/review #158's final check: `ymin`/`ymax`
        // get the same "only when actually mapped" treatment as `lower`/
        // `middle`/`upper`/`outliers` — otherwise a raw, unmapped `ymin`/
        // `ymax` column (e.g. a SQL alias) satisfied `validate_layer`'s
        // stat="identity" requirement below the same way a raw `y`
        // satisfies `required_aes` everywhere else, silently accepting an
        // incomplete five-number summary ggplot2 itself rejects. This is
        // scoped to `GeomBoxplot` alone (`build.rs`'s drop loop runs per
        // layer), so a raw `ymin`/`ymax` on any *other* geom — e.g. a
        // forecast's confidence band on an unmapped `geom_line` — is
        // untouched; the universal raw-y rule stays exactly as it is
        // everywhere else.
        &["lower", "middle", "upper", "outliers", "ymin", "ymax"]
    }

    fn validate_layer(&self, pre_stat_columns: &[String], stat_name: &str) -> Result<(), String> {
        let has = |c: &str| pre_stat_columns.iter().any(|x| x == c);
        if stat_name == "identity" {
            // ggplot2 4.0.3's own stat="identity" boxplot requires the
            // full five-number summary (plan §0's R table) — review #158
            // finding 5: this previously only required lower/middle/upper,
            // silently rendering whisker-less boxes with no ymin/ymax.
            for c in ["lower", "middle", "upper", "ymin", "ymax"] {
                if !has(c) {
                    return Err(format!(
                        "geom_boxplot requires aesthetic '{c}' under stat=\"identity\" but it was not provided"
                    ));
                }
            }
        } else if !has("y") {
            return Err("geom_boxplot requires aesthetic 'y' but it was not provided".to_string());
        }
        Ok(())
    }

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

    /// B3-5: normalise the `outliers` cell (strip brackets, drop unparsable
    /// parts with one warning) and widen `ymin_final`/`ymax_final` to
    /// include every outlier, so the y scale trains on the full extent
    /// (allowed #6) instead of silently clipping outliers that fall
    /// outside the whiskers.
    fn setup_data(&self, data: &mut DataFrame, warnings: &mut Vec<String>) {
        if !data.has_column("outliers") {
            return;
        }
        let n = data.nrows();
        let outliers_col = data
            .column("outliers")
            .map(|c| c.to_vec())
            .unwrap_or_default();
        let ymin_col = data.column("ymin").map(|c| c.to_vec());
        let ymax_col = data.column("ymax").map(|c| c.to_vec());

        let mut cleaned = Vec::with_capacity(n);
        let mut ymin_final = Vec::with_capacity(n);
        let mut ymax_final = Vec::with_capacity(n);
        let mut had_unparsable = false;

        for i in 0..n {
            let (vals, cleaned_cell) = match outliers_col.get(i) {
                Some(Value::Str(s)) => {
                    let (vals, bad) = parse_outliers(s);
                    had_unparsable |= bad;
                    let joined = vals
                        .iter()
                        .map(|v| v.to_string())
                        .collect::<Vec<_>>()
                        .join(", ");
                    (vals, Value::Str(joined))
                }
                // Review #158 finding 5: a numeric `outliers` cell (a
                // single value, not a comma list) is a single outlier, not
                // silently ignored. Normalised to the same `Value::Str`
                // shape as the parsed-list case, so `draw`'s plain
                // split/parse keeps working unchanged.
                Some(v) if v.as_f64().is_some_and(f64::is_finite) => {
                    let f = v.as_f64().unwrap();
                    (vec![f], Value::Str(f.to_string()))
                }
                Some(other) => (Vec::new(), other.clone()),
                None => (Vec::new(), Value::Na),
            };
            cleaned.push(cleaned_cell);

            let mut lo = ymin_col
                .as_ref()
                .and_then(|c| c.get(i))
                .and_then(|v| v.as_f64());
            let mut hi = ymax_col
                .as_ref()
                .and_then(|c| c.get(i))
                .and_then(|v| v.as_f64());
            for v in &vals {
                lo = Some(lo.map_or(*v, |l| l.min(*v)));
                hi = Some(hi.map_or(*v, |h| h.max(*v)));
            }
            ymin_final.push(lo.map(Value::Float).unwrap_or(Value::Na));
            ymax_final.push(hi.map(Value::Float).unwrap_or(Value::Na));
        }

        if had_unparsable {
            warnings.push(
                "geom_boxplot: outliers(...) contained an unparsable value; skipped".to_string(),
            );
        }

        data.add_column("outliers".to_string(), cleaned);
        // Review #158 finding 5: an internal-prefix name (never a raw
        // source column can forge it — see `BOXPLOT_YMIN_FINAL`'s doc),
        // so only this geom's own `setup_data` can ever produce these,
        // not a SQL alias/leftover column on an unrelated geom.
        data.add_column(crate::aes::BOXPLOT_YMIN_FINAL.to_string(), ymin_final);
        data.add_column(crate::aes::BOXPLOT_YMAX_FINAL.to_string(), ymax_final);
    }
}