ggplot_rs/build.rs
1use crate::aes::{mapping::apply_after_stat, mapping::resolve_mappings, Aes, Aesthetic};
2use crate::annotate::Annotation;
3use crate::coord::Coord;
4use crate::data::DataFrame;
5use crate::facet::{Facet, FacetScales, Panel};
6use crate::geom::Geom;
7use crate::plot::{GGError, GGPlot, Labels, Layer};
8use crate::position::PositionParams;
9use crate::scale::ScaleSet;
10use crate::theme::Theme;
11
12/// A built layer ready for rendering.
13pub struct BuiltLayer {
14 pub data: DataFrame,
15 pub geom: Box<dyn Geom>,
16 pub show_legend: Option<bool>,
17 /// Legend key glyph (the layer's override, else the geom's).
18 pub key_glyph: Option<crate::guide::key::KeyGlyph>,
19 /// Per-aesthetic `show.legend` overrides.
20 pub show_legend_aes: Vec<(Aesthetic, bool)>,
21}
22
23impl BuiltLayer {
24 /// Whether this layer appears in `aes`'s legend.
25 pub(crate) fn shows_legend_for(&self, aes: &Aesthetic) -> Option<bool> {
26 self.show_legend_aes
27 .iter()
28 .find(|(a, _)| a == aes)
29 .map(|(_, s)| *s)
30 .or(self.show_legend)
31 }
32}
33
34/// A fully built plot ready for rendering.
35pub struct BuiltPlot {
36 pub layers: Vec<BuiltLayer>,
37 pub scales: ScaleSet,
38 pub coord: Box<dyn Coord>,
39 pub theme: Theme,
40 pub labels: Labels,
41 pub facet: Facet,
42 /// Which facet panels get axes (#412).
43 pub facet_axes: crate::facet::FacetAxes,
44 pub panels: Vec<Panel>,
45 /// Per-panel layer data. panels_data[panel_idx][layer_idx] = data for that panel+layer.
46 pub panels_data: Vec<Vec<DataFrame>>,
47 pub annotations: Vec<Annotation>,
48 pub guide_legend: crate::guide::config::GuideLegend,
49 /// Aesthetics suppressed from the legend (all layers with that aes set show_legend=false).
50 pub suppressed_aes: std::collections::HashSet<Aesthetic>,
51 /// Per-panel scale sets for free facets. Empty when FacetScales::Fixed.
52 pub panel_scales: Vec<ScaleSet>,
53 /// Non-fatal problems found while building (ggplot2's warnings), e.g.
54 /// "geom_point: removed 2 rows containing non-finite values". See
55 /// [`BuiltPlot::warnings`].
56 pub warnings: Vec<String>,
57 /// Every discrete legend's B3-2 layout plan (explicit `nrow`/`ncol`
58 /// grid, or auto-wrap), keyed by aesthetic, computed exactly once in
59 /// `layout_built` and reused as-is by `draw_legend_planned` — never
60 /// recomputed at draw time, so the warning (based on these same
61 /// plans) always matches what's drawn (review #158 finding 2:
62 /// computing it twice, once against the pre-reservation panel height
63 /// and once against the post-reservation one, could warn "+7 more"
64 /// while drawing "+15 more"). A guide missing from this map draws via
65 /// the 0.21 single-row/single-column path unchanged. Several
66 /// simultaneous discrete guides each get their own independent entry
67 /// (sipemu/ggplot-rs#155) — previously this only ever held at most
68 /// one plan, for a single discrete guide.
69 pub(crate) legend_plans: std::collections::HashMap<Aesthetic, crate::guide::legend::LegendPlan>,
70 /// Where `legend_inside_auto` put the legend (`inside-bottom-right`,
71 /// `top`, …), written as the root `data-legend-at` attribute (#332).
72 pub(crate) legend_at: Option<String>,
73 /// Clip the data layers to the panel (`clip = "on"`, #411).
74 pub clip: bool,
75 /// Semantic CSS classes on the SVG's non-data elements (#465).
76 pub svg_classes: bool,
77}
78
79impl BuiltPlot {
80 /// Non-fatal build warnings, in layer order: rows dropped for non-finite
81 /// positions, and layers skipped because their stat produced no data
82 /// (e.g. a density of a single value). Rendering still succeeds.
83 pub fn warnings(&self) -> &[String] {
84 &self.warnings
85 }
86}
87
88/// Columns holding *position* values: rows with non-finite values here are
89/// dropped before stats (and again after, for stat output).
90const POSITION_COLS: &[&str] = &[
91 "x",
92 "y",
93 "xmin",
94 "xmax",
95 "ymin",
96 "ymax",
97 "xend",
98 "yend",
99 "open",
100 "high",
101 "low",
102 "close",
103 "xintercept",
104 "yintercept",
105 "slope",
106 "intercept",
107 // Boxplot stat="identity" hinges (B3-5). `outliers` is a comma-list
108 // string, not a position value, so it's not listed here — a non-finite
109 // outlier is skipped individually with its own warning when parsed.
110 "lower",
111 "middle",
112 "upper",
113];
114
115/// Columns a position scale's transformation applies to (pre-stat).
116const X_FAMILY: &[&str] = &["x", "xmin", "xmax", "xend", "xintercept"];
117const Y_FAMILY: &[&str] = &[
118 "y",
119 "ymin",
120 "ymax",
121 "yend",
122 "open",
123 "high",
124 "low",
125 "close",
126 "yintercept",
127 "lower",
128 "middle",
129 "upper",
130];
131
132/// Reference-line intercept columns and the position scale they train
133/// (ggplot2: `xintercept`/`yintercept` are x/y aesthetics).
134const INTERCEPT_COLS: [(&str, Aesthetic); 2] =
135 [("xintercept", Aesthetic::X), ("yintercept", Aesthetic::Y)];
136
137/// Train `scale` on reference-line intercepts. A numeric intercept on a
138/// discrete axis is a position *between* categories (ggplot2), not a level.
139fn train_intercepts(scale: &mut Box<dyn crate::scale::Scale>, values: &[crate::data::Value]) {
140 if scale.is_discrete() && values.iter().all(|v| v.as_f64().is_some() || v.is_na()) {
141 return;
142 }
143 scale.train(values);
144}
145
146/// Map a canonical column name to the aesthetic whose scale formats it
147/// (B2-5 tooltip templates / B2-4 `data-fvalue`-style formatting). Columns
148/// outside this list (group, label, or a stat output like `count`) have no
149/// scale of their own: [`render_tooltip_template`] falls back to the plain
150/// display format for them.
151fn column_aesthetic(name: &str) -> Option<Aesthetic> {
152 match name {
153 "x" => Some(Aesthetic::X),
154 "y" => Some(Aesthetic::Y),
155 "fill" => Some(Aesthetic::Fill),
156 "color" | "colour" => Some(Aesthetic::Color),
157 "size" => Some(Aesthetic::Size),
158 "linewidth" => Some(Aesthetic::Linewidth),
159 "alpha" => Some(Aesthetic::Alpha),
160 // Position columns (B3-5, D9 allowed #7): formatted through the
161 // x/y scale, same as the bare `x`/`y` they're derived from.
162 "xmin" | "xmax" | "xend" => Some(Aesthetic::X),
163 "ymin" | "ymax" | "yend" | "lower" | "middle" | "upper" => Some(Aesthetic::Y),
164 _ => None,
165 }
166}
167
168/// Render one row of a `GGPlot::tooltip` template: `{column}` is filled
169/// from `data`'s row `row`, formatted through that column's scale when
170/// [`column_aesthetic`] names one and it has a
171/// [`Scale::format_value`](crate::scale::Scale::format_value) (else the
172/// plain display format); a column that doesn't exist on this layer is
173/// empty, not literally `{column}`. `{{`/`}}` are literal braces.
174fn render_tooltip_template(
175 template: &str,
176 data: &DataFrame,
177 scales: &ScaleSet,
178 row: usize,
179) -> String {
180 let mut out = String::new();
181 let mut chars = template.chars().peekable();
182 while let Some(c) = chars.next() {
183 match c {
184 '{' if chars.peek() == Some(&'{') => {
185 chars.next();
186 out.push('{');
187 }
188 '}' if chars.peek() == Some(&'}') => {
189 chars.next();
190 out.push('}');
191 }
192 '{' => {
193 let mut name = String::new();
194 let mut closed = false;
195 for nc in chars.by_ref() {
196 if nc == '}' {
197 closed = true;
198 break;
199 }
200 name.push(nc);
201 }
202 if closed {
203 out.push_str(&render_placeholder(&name, data, scales, row));
204 } else {
205 // Unterminated `{…` to end of string: not a placeholder,
206 // kept literally.
207 out.push('{');
208 out.push_str(&name);
209 }
210 }
211 c => out.push(c),
212 }
213 }
214 out
215}
216
217fn render_placeholder(name: &str, data: &DataFrame, scales: &ScaleSet, row: usize) -> String {
218 let Some(col) = data.column(name) else {
219 return String::new();
220 };
221 let Some(v) = col.get(row) else {
222 return String::new();
223 };
224 if v.is_na() {
225 return String::new();
226 }
227 if let Some(formatted) = column_aesthetic(name)
228 .and_then(|aes| scales.get(&aes))
229 .and_then(|s| s.format_value(v))
230 {
231 return formatted;
232 }
233 crate::geom::tip_value(v)
234}
235
236/// A position scale's transform applied to one column: `None` when it
237/// changes nothing (an identity scale). A transform that is undefined for a
238/// value (log of 0 or a negative) yields NaN, so the non-finite filter drops
239/// the row with a warning instead of drawing it at 0.
240fn transform_column(
241 scale: &dyn crate::scale::Scale,
242 col: &[crate::data::Value],
243) -> Option<Vec<crate::data::Value>> {
244 let transformed: Vec<_> = col
245 .iter()
246 .map(|v| match (scale.transform(v), v.as_f64()) {
247 (crate::data::Value::Na, Some(_)) => crate::data::Value::Float(f64::NAN),
248 (t, _) => t,
249 })
250 .collect();
251 let any_changed =
252 transformed
253 .iter()
254 .zip(col.iter())
255 .any(|(t, o)| match (t.as_f64(), o.as_f64()) {
256 (Some(a), Some(b)) => a.is_nan() || (a - b).abs() > f64::EPSILON,
257 _ => false,
258 });
259 any_changed.then_some(transformed)
260}
261
262/// The grammar pipeline: transforms a GGPlot specification into render-ready data.
263pub struct PlotBuilder;
264
265impl PlotBuilder {
266 pub fn build(plot: GGPlot) -> Result<BuiltPlot, GGError> {
267 let GGPlot {
268 data: plot_data,
269 mapping: plot_mapping,
270 layers,
271 scales: user_scales,
272 mut coord,
273 theme,
274 labels,
275 facet,
276 facet_axes,
277 annotations,
278 guide_legend,
279 warnings: plot_warnings,
280 default_aspect_ratio,
281 hide_position_guides,
282 hide_y_guides,
283 legend_bottom_share: _,
284 clip,
285 svg_classes,
286 legend_auto: _,
287 locale,
288 svg_root_data: _,
289 root_attrs: _,
290 svg_warnings_attr: _,
291 svg_formatted_values: _,
292 layer_svg_attrs,
293 svg_title: _,
294 svg_desc: _,
295 } = plot;
296 let mut theme = theme;
297 if theme.aspect_ratio.is_none() {
298 theme.aspect_ratio = default_aspect_ratio;
299 }
300 if hide_position_guides {
301 theme.hide_position_guides();
302 }
303 if hide_y_guides {
304 theme.hide_y_guides();
305 }
306
307 // Malformed input (e.g. mismatched column lengths) is reported here as
308 // a validation error instead of panicking when the frame is assembled.
309 plot_data.validate()?;
310 for layer in &layers {
311 if let Some(d) = &layer.data {
312 d.validate()?;
313 }
314 }
315
316 // #412: `facet_grid(margins = TRUE)` — copy the rows into "(all)"
317 // levels before any stat runs, so the margin panels are computed on
318 // the pooled data (as ggplot2's `reshape_add_margins`).
319 let (plot_data, layers) = match &facet {
320 Facet::Grid {
321 margins: true,
322 row_var,
323 col_var,
324 ..
325 } => {
326 let add =
327 |d: DataFrame| Self::add_margins(d, row_var.as_deref(), col_var.as_deref());
328 let layers = layers
329 .into_iter()
330 .map(|mut l| {
331 l.data = l.data.take().map(add);
332 l
333 })
334 .collect();
335 (add(plot_data), layers)
336 }
337 _ => (plot_data, layers),
338 };
339
340 let mut scale_set = ScaleSet::new();
341
342 // Add user-specified scales
343 for s in user_scales {
344 scale_set.add(s);
345 }
346
347 let mut built_layers = Vec::new();
348 let mut warnings: Vec<String> = plot_warnings;
349
350 // Faceting variables — used to group stat computation per panel so a
351 // computed stat (density/histogram) is estimated per panel, not pooled.
352 let facet_vars = Self::facet_vars(&facet);
353
354 for (li, mut layer) in layers.into_iter().enumerate() {
355 // #221: a layer with `layer_svg_attr`s draws through a wrapper
356 // that hands them to the backend around its own `draw`.
357 let attrs: Vec<(String, String)> = layer_svg_attrs
358 .iter()
359 .filter(|(i, _, _)| *i == li)
360 .map(|(_, n, v)| (n.clone(), v.clone()))
361 .collect();
362 if !attrs.is_empty() {
363 let geom =
364 std::mem::replace(&mut layer.geom, Box::new(crate::geom::blank::GeomBlank));
365 layer.geom = Box::new(crate::geom::layer_attrs::LayerSvgAttrs(geom, attrs));
366 }
367 let built = Self::build_layer(
368 layer,
369 &plot_data,
370 &plot_mapping,
371 &mut scale_set,
372 &theme,
373 &facet_vars,
374 &mut warnings,
375 )?;
376 built_layers.push(built);
377 }
378
379 // Final scale training pass across all layers
380 for bl in &built_layers {
381 scale_set.train_layer(&bl.data);
382 for (col, aes) in &INTERCEPT_COLS {
383 // No layer created this position scale: the intercepts do.
384 if let (Some(values), None) = (bl.data.column(col), scale_set.get(aes)) {
385 let mut frame = DataFrame::new();
386 frame.add_column(aes.col_name().to_string(), values.to_vec());
387 scale_set.ensure_scale(aes, &frame);
388 }
389 if let (Some(values), Some(scale)) = (bl.data.column(col), scale_set.get_mut(aes)) {
390 train_intercepts(scale, values);
391 }
392 }
393 }
394
395 // Validate each scale's final configuration (e.g. a transform
396 // undefined at its own domain limits) now that training is done —
397 // `map`/`map_to_size` run per mark at draw time and have no
398 // warnings sink of their own.
399 for scale in scale_set.iter() {
400 scale.validate(&mut warnings);
401 }
402
403 // An empty plot (no layers, or only layers without data) still gets a
404 // panel with axes: make sure both position scales exist. They stay
405 // untrained, so they draw no breaks and map everything to the centre.
406 if built_layers.iter().all(|bl| bl.data.nrows() == 0) {
407 for aes in [Aesthetic::X, Aesthetic::Y] {
408 if scale_set.get(&aes).is_none() {
409 scale_set.add(Box::new(
410 crate::scale::continuous::ScaleContinuous::new().for_aesthetic(aes),
411 ));
412 }
413 }
414 }
415
416 // Apply coord zoom limits (coord_cartesian xlim/ylim)
417 if let Some((min, max)) = coord.zoom_x() {
418 scale_set.set_limits(&Aesthetic::X, min, max);
419 }
420 if let Some((min, max)) = coord.zoom_y() {
421 scale_set.set_limits(&Aesthetic::Y, min, max);
422 }
423
424 // Let the coordinate system adjust/inspect the trained scales.
425 coord.train_scales(&mut scale_set);
426
427 // Supply trained axis spans to the coordinate system (used by coord_trans).
428 // pmin/pmax are the panel positions of the domain endpoints, so the coord
429 // can invert the scale's (linearly expanded) mapping exactly.
430 let axis_span = |aes: &Aesthetic| {
431 scale_set.get(aes).and_then(|s| {
432 s.domain().map(|(min, max)| crate::coord::AxisSpan {
433 min,
434 max,
435 pmin: s.map(&crate::data::Value::Float(min)),
436 pmax: s.map(&crate::data::Value::Float(max)),
437 })
438 })
439 };
440 let x_span = axis_span(&Aesthetic::X);
441 let y_span = axis_span(&Aesthetic::Y);
442 coord.set_domains(x_span, y_span);
443
444 // Apply after_scale() color derivations: copy the source aesthetic's
445 // column to the target and register a lightness-modified clone of the
446 // source scale, so the target aesthetic draws the mapped source color
447 // adjusted in lightness (e.g. a darker border derived from the fill).
448 for spec in &plot_mapping.after_scale {
449 if let Some(src_scale) = scale_set.get(&spec.source) {
450 let modified = crate::scale::modified::ScaleColorModified::new(
451 src_scale.clone_box(),
452 spec.target.clone(),
453 spec.lightness,
454 );
455 let (src_col, tgt_col) = (spec.source.col_name(), spec.target.col_name());
456 for bl in &mut built_layers {
457 if !bl.data.has_column(tgt_col) {
458 if let Some(vals) = bl.data.column(src_col) {
459 let vals = vals.to_vec();
460 bl.data.add_column(tgt_col.to_string(), vals);
461 }
462 }
463 }
464 scale_set.add(Box::new(modified));
465 }
466 }
467
468 // Plot-level locale for default break labels (before panel scales are
469 // cloned from the base set, so free facets inherit it).
470 if let Some(locale) = &locale {
471 scale_set.set_default_locale(locale);
472 }
473
474 // Compute facet panels
475 let (panels, panels_data) = Self::compute_facets(&facet, &built_layers, &plot_data);
476
477 // Compute suppressed aesthetics from show_legend flags.
478 let mut suppressed_aes = Self::compute_suppressed_aes(&built_layers);
479 // #409: `guides(<aes> = "none")`.
480 suppressed_aes.extend(guide_legend.hidden.iter().cloned());
481 // #410: each layer's legend glyph and the legend aesthetics it shows.
482 let mut guide_legend = guide_legend;
483 guide_legend.layer_keys = built_layers
484 .iter()
485 .map(|bl| crate::guide::key::LayerKey {
486 glyph: bl.key_glyph,
487 fill: bl.geom.key_fill(),
488 aesthetics: crate::guide::legend::LEGEND_AESTHETICS
489 .iter()
490 .filter(|a| bl.data.has_column(a.col_name()))
491 .filter(|a| bl.shows_legend_for(a) != Some(false))
492 .cloned()
493 .collect(),
494 })
495 .collect();
496
497 // Compute per-panel scales for free facets
498 let facet_scales_mode = match &facet {
499 Facet::Wrap { scales, .. } => scales.clone(),
500 Facet::Grid { scales, .. } => scales.clone(),
501 Facet::None => FacetScales::Fixed,
502 };
503 let panel_scales = Self::compute_panel_scales(&facet_scales_mode, &panels_data, &scale_set);
504
505 Ok(BuiltPlot {
506 layers: built_layers,
507 scales: scale_set,
508 coord,
509 theme,
510 labels,
511 facet,
512 facet_axes,
513 panels,
514 panels_data,
515 annotations,
516 guide_legend,
517 suppressed_aes,
518 panel_scales,
519 warnings,
520 legend_plans: std::collections::HashMap::new(),
521 legend_at: None,
522 clip,
523 svg_classes,
524 })
525 }
526
527 /// Keep only the rows whose `keep` flag is set.
528 fn retain_rows(data: &mut DataFrame, keep: &[bool]) {
529 if keep.iter().all(|&k| k) {
530 return;
531 }
532 let mut result = DataFrame::new();
533 for col_name in data.column_names() {
534 if let Some(src) = data.column(col_name) {
535 let vals: Vec<_> = src
536 .iter()
537 .zip(keep)
538 .filter(|(_, &k)| k)
539 .map(|(v, _)| v.clone())
540 .collect();
541 result.add_column(col_name.to_string(), vals);
542 }
543 }
544 *data = result;
545 }
546
547 /// Drop rows with a non-finite value in any position column (`NaN` always;
548 /// `±Inf` unless the geom gives infinities meaning). Returns the number of
549 /// rows removed.
550 /// `keep_as_break`: for a connecting geom ([`Geom::keeps_na_as_break`]),
551 /// a bad row is counted (for the warning) but not deleted — it stays in
552 /// `data` as a non-finite pixel position, which `DrawBackend::draw_line`
553 /// splits the polyline on, instead of being spliced out and silently
554 /// joining the finite points across the gap.
555 /// `exclude`: column names to skip entirely this call — `lower`/
556 /// `middle`/`upper` present in the raw data but not mapped as that
557 /// aesthetic on this layer (review #158's final check: these must
558 /// never cost an unrelated geom its rows; see `raw_unmapped` at the
559 /// call site).
560 fn drop_non_finite(
561 data: &mut DataFrame,
562 allow_infinite: bool,
563 keep_as_break: bool,
564 exclude: &[&str],
565 ) -> usize {
566 let n = data.nrows();
567 let mut keep = vec![true; n];
568 for col in POSITION_COLS {
569 if exclude.contains(col) {
570 continue;
571 }
572 if let Some(values) = data.column(col) {
573 // Several position columns are legitimately discrete
574 // (`Value::Str`) in this codebase — not just `x`/`y`, e.g.
575 // `geom_bracket`'s `xmin`/`xmax` hold category names for a
576 // bracket spanning two discrete groups. `Value::Na` is
577 // unambiguously missing everywhere, though (review #158
578 // finding 4: a SQL NULL in a boxplot position column used
579 // to silently map to 0 instead of being dropped with the
580 // standard warning; only `Float(NaN)` was caught before).
581 // The new boxplot-only `lower`/`middle`/`upper` columns
582 // have no legitimate discrete use anywhere, so a
583 // non-numeric, non-`Na` value there (a stray string) is
584 // missing too.
585 let strictly_numeric = matches!(*col, "lower" | "middle" | "upper");
586 for (i, v) in values.iter().enumerate() {
587 let bad = match v {
588 crate::data::Value::Na => true,
589 crate::data::Value::Float(f) => {
590 f.is_nan() || (f.is_infinite() && !allow_infinite)
591 }
592 _ if strictly_numeric => v.as_f64().is_none(),
593 _ => false,
594 };
595 if bad {
596 keep[i] = false;
597 }
598 }
599 }
600 }
601 let removed = keep.iter().filter(|&&k| !k).count();
602 if removed > 0 && !keep_as_break {
603 Self::retain_rows(data, &keep);
604 }
605 removed
606 }
607
608 /// The column name(s) a facet splits on, if any.
609 fn facet_vars(facet: &Facet) -> Vec<String> {
610 match facet {
611 Facet::None => vec![],
612 Facet::Wrap { var, .. } => vec![var.clone()],
613 Facet::Grid {
614 row_var, col_var, ..
615 } => row_var.iter().chain(col_var.iter()).cloned().collect(),
616 }
617 }
618
619 fn compute_facets(
620 facet: &Facet,
621 built_layers: &[BuiltLayer],
622 _plot_data: &DataFrame,
623 ) -> (Vec<Panel>, Vec<Vec<DataFrame>>) {
624 match facet {
625 Facet::None => (vec![], vec![]),
626 Facet::Wrap {
627 var,
628 ncol,
629 labeller,
630 levels: fixed,
631 vertical,
632 as_table,
633 ..
634 } => {
635 // Explicit levels (#329), else unique values across all
636 // layers in first-appearance order.
637 let mut levels: Vec<String> = fixed.clone().unwrap_or_default();
638 for bl in built_layers.iter().filter(|_| fixed.is_none()) {
639 if let Some(col) = bl.data.column(var) {
640 for v in col {
641 let key = v.to_group_key();
642 if !levels.contains(&key) {
643 levels.push(key);
644 }
645 }
646 }
647 }
648
649 // Panels will be positioned during rendering (depends on layout)
650 let panels: Vec<Panel> = levels
651 .iter()
652 .enumerate()
653 .map(|(i, value)| {
654 let ncols =
655 ncol.unwrap_or_else(|| (levels.len() as f64).sqrt().ceil() as usize);
656 let formatted = labeller.format(var, value);
657 Panel {
658 row: crate::facet::wrap::wrap_cell(
659 i,
660 levels.len(),
661 ncols,
662 *vertical,
663 *as_table,
664 )
665 .0,
666 col: crate::facet::wrap::wrap_cell(
667 i,
668 levels.len(),
669 ncols,
670 *vertical,
671 *as_table,
672 )
673 .1,
674 label: formatted.clone(),
675 row_label: None,
676 col_label: Some(formatted),
677 rect: crate::render::Rect {
678 x: 0.0,
679 y: 0.0,
680 width: 0.0,
681 height: 0.0,
682 },
683 }
684 })
685 .collect();
686
687 // Split data per panel per layer
688 let panels_data: Vec<Vec<DataFrame>> = levels
689 .iter()
690 .map(|level| {
691 built_layers
692 .iter()
693 .map(|bl| Self::filter_data_by_var(&bl.data, var, level))
694 .collect()
695 })
696 .collect();
697
698 (panels, panels_data)
699 }
700 Facet::Grid {
701 row_var,
702 col_var,
703 labeller,
704 row_levels: fixed_rows,
705 col_levels: fixed_cols,
706 margins,
707 ..
708 } => {
709 // #412: fixed levels (+ the "(all)" margin) when given.
710 let with_margin = |l: &Vec<String>| {
711 let mut l = l.clone();
712 if *margins {
713 l.push(Self::MARGIN_LEVEL.to_string());
714 }
715 l
716 };
717 let mut row_levels: Vec<String> =
718 fixed_rows.as_ref().map(with_margin).unwrap_or_default();
719 let mut col_levels: Vec<String> =
720 fixed_cols.as_ref().map(with_margin).unwrap_or_default();
721
722 for bl in built_layers {
723 if let (Some(rv), None) = (row_var, fixed_rows) {
724 if let Some(col) = bl.data.column(rv) {
725 for v in col {
726 let key = v.to_group_key();
727 if !row_levels.contains(&key) {
728 row_levels.push(key);
729 }
730 }
731 }
732 }
733 if let (Some(cv), None) = (col_var, fixed_cols) {
734 if let Some(col) = bl.data.column(cv) {
735 for v in col {
736 let key = v.to_group_key();
737 if !col_levels.contains(&key) {
738 col_levels.push(key);
739 }
740 }
741 }
742 }
743 }
744
745 if row_levels.is_empty() {
746 row_levels.push("".to_string());
747 }
748 if col_levels.is_empty() {
749 col_levels.push("".to_string());
750 }
751
752 let mut panels = Vec::new();
753 let mut panels_data = Vec::new();
754
755 for (ri, rl) in row_levels.iter().enumerate() {
756 for (ci, cl) in col_levels.iter().enumerate() {
757 let row_fmt = if rl.is_empty() {
758 None
759 } else {
760 let rv = row_var.as_deref().unwrap_or("");
761 Some(labeller.format(rv, rl))
762 };
763 let col_fmt = if cl.is_empty() {
764 None
765 } else {
766 let cv = col_var.as_deref().unwrap_or("");
767 Some(labeller.format(cv, cl))
768 };
769 let label = match (&row_fmt, &col_fmt) {
770 (Some(r), Some(c)) => format!("{r} | {c}"),
771 (Some(r), None) => r.clone(),
772 (None, Some(c)) => c.clone(),
773 (None, None) => String::new(),
774 };
775 panels.push(Panel {
776 row: ri,
777 col: ci,
778 label,
779 row_label: row_fmt,
780 col_label: col_fmt,
781 rect: crate::render::Rect {
782 x: 0.0,
783 y: 0.0,
784 width: 0.0,
785 height: 0.0,
786 },
787 });
788
789 let layer_data: Vec<DataFrame> = built_layers
790 .iter()
791 .map(|bl| {
792 let mut data = bl.data.clone();
793 if let Some(rv) = row_var {
794 if !rl.is_empty() {
795 data = Self::filter_data_by_var(&data, rv, rl);
796 }
797 }
798 if let Some(cv) = col_var {
799 if !cl.is_empty() {
800 data = Self::filter_data_by_var(&data, cv, cl);
801 }
802 }
803 data
804 })
805 .collect();
806 panels_data.push(layer_data);
807 }
808 }
809
810 (panels, panels_data)
811 }
812 }
813 }
814
815 /// The "(all)" margin level of `facet_grid(margins = TRUE)` (#412).
816 pub(crate) const MARGIN_LEVEL: &'static str = "(all)";
817
818 /// `data` plus a copy with the row variable set to "(all)", then all of
819 /// that plus a copy with the column variable set to "(all)".
820 fn add_margins(data: DataFrame, row: Option<&str>, col: Option<&str>) -> DataFrame {
821 let mut out = data;
822 for var in [row, col].into_iter().flatten() {
823 if !out.has_column(var) || out.nrows() == 0 {
824 continue;
825 }
826 let mut copy = out.clone();
827 if let Some(c) = copy.column_mut(var) {
828 c.fill(crate::data::Value::Str(Self::MARGIN_LEVEL.to_string()));
829 }
830 out.vstack(©);
831 }
832 out
833 }
834
835 fn filter_data_by_var(data: &DataFrame, var: &str, level: &str) -> DataFrame {
836 if let Some(col) = data.column(var) {
837 let indices: Vec<usize> = col
838 .iter()
839 .enumerate()
840 .filter(|(_, v)| v.to_group_key() == level)
841 .map(|(i, _)| i)
842 .collect();
843
844 let mut result = DataFrame::new();
845 for col_name in data.column_names() {
846 if let Some(src) = data.column(col_name) {
847 let vals: Vec<_> = indices.iter().map(|&i| src[i].clone()).collect();
848 result.add_column(col_name.to_string(), vals);
849 }
850 }
851 result
852 } else {
853 data.clone()
854 }
855 }
856
857 fn build_layer(
858 layer: Layer,
859 plot_data: &DataFrame,
860 plot_mapping: &Aes,
861 scale_set: &mut ScaleSet,
862 theme: &Theme,
863 facet_vars: &[String],
864 warnings: &mut Vec<String>,
865 ) -> Result<BuiltLayer, GGError> {
866 let primary = theme.primary;
867 let geom_theme = &theme.geom;
868 let Layer {
869 data: layer_data,
870 mapping: layer_mapping,
871 mut geom,
872 stat,
873 position,
874 params: _,
875 show_legend,
876 explicit_style,
877 tooltip_template,
878 key_glyph,
879 show_legend_aes,
880 } = layer;
881 let key_glyph = key_glyph.or_else(|| geom.key_glyph());
882
883 // Step 1: Resolve data — use layer data if provided, else plot data
884 // Borrow the plot data when the layer has none of its own (no per-layer
885 // full-frame clone; `resolve_mappings` copies only the columns it needs).
886 let source_data = layer_data.as_ref().unwrap_or(plot_data);
887
888 // Step 2: Merge mappings — layer overrides plot-level
889 // (Reference lines don't inherit the plot mapping — ggplot2's
890 // `inherit.aes = FALSE`.)
891 let merged_mapping = if geom.inherit_aes() {
892 plot_mapping.merge(&layer_mapping)
893 } else {
894 layer_mapping.clone()
895 };
896
897 // B2-7: geom ink/paper/accent palette, applied before the brand
898 // colour below so a primary colour still wins when both are set.
899 // Skipped when the layer was configured explicitly
900 // (`geom_*_with(...)`) — the same `explicit_style` gate
901 // `set_series_color` uses below, and for the same reason: a
902 // field-value comparison against the hard-coded default (e.g.
903 // `color == (0, 0, 0)`) can't tell an explicitly-set black apart
904 // from a black that was simply never touched (review PR#141
905 // finding 4), but `explicit_style` can.
906 if !explicit_style {
907 geom.apply_element_geom(geom_theme);
908 }
909
910 // Brand/primary color: apply to a single-series geom only when the layer
911 // maps neither color nor fill (an explicit aesthetic always wins) and
912 // the geom wasn't configured explicitly via `geom_*_with(...)`.
913 if let Some(color) = primary.filter(|_| !explicit_style) {
914 let has_color = merged_mapping.get_mapping(&Aesthetic::Color).is_some();
915 let has_fill = merged_mapping.get_mapping(&Aesthetic::Fill).is_some();
916 if !has_color && !has_fill {
917 geom.set_series_color(color);
918 }
919 }
920
921 // Step 3: Evaluate aes — rename columns to canonical names
922 let mut working_data = resolve_mappings(source_data, &merged_mapping, warnings);
923
924 // D3 (B3-5): drop a "mapped-only" column (e.g. `lower`/`middle`/
925 // `upper`/`outliers` for the boxplot) unless the user actually
926 // mapped that aesthetic — so a raw column that merely happens to
927 // share the name (a SQL alias, say) is never mistaken for one, and
928 // so a raw `lower` is never drawn as a boxplot hinge on
929 // `geom_boxplot` itself.
930 //
931 // Review #158's *final check* found that making this drop
932 // universal (every geom, round 3's `MAPPED_ONLY_COLS`) was itself
933 // a regression: it removed the raw `lower`/`middle`/`upper`/
934 // `outliers` columns from a layer's data entirely, so a forecast
935 // chart's `tooltip("{y} [{lower}, {upper}]")` on an unmapped
936 // `geom_line` rendered `[, ]` instead of the actual bounds, and
937 // `facet_wrap("upper")` on an unmapped column silently rendered
938 // unfaceted. ggplot2 never drops a raw data column just because
939 // its name matches an unused extension aesthetic — it only ever
940 // special-cases `lower`/`middle`/`upper`/`outliers` for
941 // `geom_boxplot`'s own hinges, so this drop goes back to being
942 // scoped to `geom.mapped_only_columns()` alone (empty by default,
943 // `GeomBoxplot` returns the four names). The narrower position/
944 // non-finite/y-scale-training concerns from the *previous*
945 // re-review (the forecast y-domain-stretch/row-drop regression)
946 // are instead handled per layer below (`raw_unmapped`), without
947 // ever removing the columns from `working_data`.
948 for col in geom.mapped_only_columns() {
949 let is_mapped = merged_mapping
950 .mappings
951 .iter()
952 .any(|m| m.aesthetic.col_name() == *col);
953 if !is_mapped {
954 working_data.remove_column(col);
955 }
956 }
957
958 // `lower`/`middle`/`upper` present in the raw (pre-stat) data under
959 // their canonical name, but *not* actually mapped as that
960 // aesthetic on this layer — e.g. a forecast's confidence-bound
961 // columns, never mapped on a plain `geom_line`/`geom_point` layer.
962 // These stay in `working_data` (see above), but mustn't be treated
963 // as position data a geom never asked for: excluded below from the
964 // non-finite/NA position drop, the `Y_FAMILY` pre-stat scale
965 // transform, and the y-scale training.
966 //
967 // Computed once, right here (before the stat runs), so it reflects
968 // only what the *raw* data actually had under these names.
969 // `GeomBoxplot`'s own default (non-identity) stat produces *fresh*
970 // `lower`/`middle`/`upper` columns later (not present yet, so never
971 // in this set) — those stay subject to all three, unaffected; an
972 // identity-stat boxplot has them mapped (required by
973 // `validate_layer` below), so they're not in this set either.
974 let raw_unmapped: Vec<&'static str> = ["lower", "middle", "upper"]
975 .into_iter()
976 .filter(|col| {
977 working_data.has_column(col)
978 && !merged_mapping
979 .mappings
980 .iter()
981 .any(|m| m.aesthetic.col_name() == *col)
982 })
983 .collect();
984
985 // Remember which columns the user actually supplied (pre-stat). A required
986 // aesthetic is satisfied if it was present here OR is synthesized by the
987 // stat (checked after Step 6) — e.g. boxplot maps `y` then the stat turns
988 // it into ymin/ymax, while StatEcdf produces `y` that wasn't mapped.
989 let pre_stat_columns: Vec<String> = working_data
990 .column_names()
991 .iter()
992 .map(|s| s.to_string())
993 .collect();
994
995 // B3-5: stat-dependent validation beyond `required_aes` (e.g.
996 // `geom_boxplot` requires lower/middle/upper under stat="identity",
997 // else y) — before Step 4, so a bad mapping fails before any scale
998 // work happens.
999 if let Err(msg) = geom.validate_layer(&pre_stat_columns, stat.name()) {
1000 return Err(GGError::ValidationError(msg));
1001 }
1002
1003 // Step 4: Ensure scales exist for each mapped aesthetic. An
1004 // `after_stat` mapping's column only exists after the stat, so its
1005 // scale is chosen from the stat output (Step 6a below) — choosing it
1006 // here would see no values and default a discrete fill to continuous.
1007 for m in &merged_mapping.mappings {
1008 if m.stage != crate::aes::MappingStage::AfterStat {
1009 scale_set.ensure_scale(&m.aesthetic, &working_data);
1010 }
1011 }
1012
1013 // Step 5: Scale transformation (e.g., log10 before stats). A position
1014 // scale transforms every column of its family (y also ymin/ymax/…).
1015 for scale in scale_set.iter() {
1016 let aes = scale.aesthetic();
1017 let cols: Vec<&str> = match aes {
1018 Aesthetic::X => X_FAMILY.to_vec(),
1019 // `lower`/`middle`/`upper` present but unmapped (raw
1020 // forecast-shaped passthrough columns, say) are not
1021 // position data this layer's geom reads, so a log/sqrt/…
1022 // y-scale transform must not touch them (review #158's
1023 // final check).
1024 Aesthetic::Y => Y_FAMILY
1025 .iter()
1026 .copied()
1027 .filter(|c| !raw_unmapped.contains(c))
1028 .collect(),
1029 _ => vec![aes.col_name()],
1030 };
1031 for col_name in cols {
1032 let Some(col) = working_data.column(col_name) else {
1033 continue;
1034 };
1035 // A transform that is undefined for a value (log of 0 or a
1036 // negative) yields NaN, so the non-finite filter below drops
1037 // the row with a warning instead of drawing it at 0.
1038 if let Some(transformed) = transform_column(scale, col) {
1039 if let Some(col_mut) = working_data.column_mut(col_name) {
1040 *col_mut = transformed;
1041 }
1042 }
1043 }
1044 }
1045
1046 // Step 5a: a binned position scale moves each x / y to its bin's
1047 // midpoint before the stat (`scale_x_binned`, #408).
1048 for (aes, col) in [(Aesthetic::X, "x"), (Aesthetic::Y, "y")] {
1049 let Some(values) = working_data.column(col).map(|c| c.to_vec()) else {
1050 continue;
1051 };
1052 if let Some(binned) = scale_set
1053 .get_mut(&aes)
1054 .and_then(|s| s.bin_positions(&values))
1055 {
1056 if let Some(c) = working_data.column_mut(col) {
1057 *c = binned;
1058 }
1059 }
1060 }
1061
1062 // Step 5b: Filter out-of-bounds data (xlim/ylim filter before stats)
1063 Self::filter_oob_data(&mut working_data, scale_set);
1064
1065 // Step 5c: Drop rows whose position is NaN (or ±Inf, unless the geom
1066 // reads infinities as "panel edge"), like ggplot2's
1067 // "Removed n rows containing non-finite values".
1068 let geom_label = format!("geom_{}", geom.name());
1069 let removed = Self::drop_non_finite(
1070 &mut working_data,
1071 geom.allows_infinite(),
1072 geom.keeps_na_as_break(),
1073 &raw_unmapped,
1074 );
1075 if removed > 0 {
1076 warnings.push(format!(
1077 "{geom_label}: removed {removed} row{} containing non-finite values",
1078 if removed == 1 { "" } else { "s" }
1079 ));
1080 }
1081 let input_rows = working_data.nrows();
1082
1083 // Validate the stat's own configuration once per layer (e.g.
1084 // `StatBinHex::binwidth` — a problem with fixed parameters, not
1085 // with the data), before it runs per-group below.
1086 stat.validate(warnings);
1087
1088 // Step 6: Compute statistics. Group by aesthetic groups AND the facet
1089 // variables, so a computed stat (density/histogram/…) is estimated per
1090 // panel rather than on pooled data; the facet column is then re-attached
1091 // to each group's output so faceting can split it back out.
1092 // Panelwise stats (e.g. stat_compare_means) see the whole panel at once,
1093 // so we group only by facet variables, not by aesthetic groups.
1094 let mut group_cols = if stat.panelwise() {
1095 Vec::new()
1096 } else {
1097 Self::detect_group_columns(&working_data)
1098 };
1099 for fv in facet_vars {
1100 if working_data.has_column(fv) && !group_cols.contains(fv) {
1101 group_cols.push(fv.clone());
1102 }
1103 }
1104
1105 working_data = if !group_cols.is_empty() {
1106 let groups =
1107 working_data.group_by(&group_cols.iter().map(|s| s.as_str()).collect::<Vec<_>>());
1108 let mut result = DataFrame::new();
1109 for group in groups {
1110 let mut computed = stat.compute_group(&group, scale_set);
1111 let n = computed.nrows();
1112 if n > 0 {
1113 for fv in facet_vars {
1114 if !computed.has_column(fv) {
1115 if let Some(val) = group.column(fv).and_then(|c| c.first()).cloned() {
1116 computed.add_column(fv.clone(), vec![val; n]);
1117 }
1118 }
1119 }
1120 }
1121 result.vstack(&computed);
1122 }
1123 result
1124 } else {
1125 stat.compute_group(&working_data, scale_set)
1126 };
1127
1128 // Step 6a: Apply after_stat() mappings (rename stat-computed columns)
1129 // and give each one a scale detected from the values it now holds.
1130 apply_after_stat(&mut working_data, &merged_mapping, warnings);
1131 for m in &merged_mapping.mappings {
1132 if m.stage == crate::aes::MappingStage::AfterStat {
1133 scale_set.ensure_scale(&m.aesthetic, &working_data);
1134 }
1135 }
1136
1137 // Step 6a-transform (#490): a `y` the stat computed (a count, a
1138 // density) or an `after_stat()` y is in data units, but the y scale
1139 // works in transformed space — so it goes through the transform here,
1140 // as ggplot2's `map_statistic` does. Without this, `scale_y_log10`
1141 // read a count of 30 as 10^30. A zero count becomes NaN and is
1142 // dropped below (log of 0), like ggplot2's non-finite removal.
1143 let y_after_stat = merged_mapping
1144 .mappings
1145 .iter()
1146 .any(|m| m.stage == crate::aes::MappingStage::AfterStat && m.aesthetic == Aesthetic::Y);
1147 let y_computed = stat.computed_y() && !pre_stat_columns.iter().any(|c| c == "y");
1148 if y_after_stat || y_computed {
1149 if let (Some(scale), Some(col)) =
1150 (scale_set.get(&Aesthetic::Y), working_data.column("y"))
1151 {
1152 if let Some(transformed) = transform_column(scale, col) {
1153 if let Some(col_mut) = working_data.column_mut("y") {
1154 *col_mut = transformed;
1155 }
1156 }
1157 }
1158 }
1159
1160 // Stat output can contain NaN (e.g. a fit on degenerate input): drop
1161 // those rows too, so nothing non-finite reaches a geom. `raw_unmapped`
1162 // was computed pre-stat, so a passthrough stat (e.g. `StatIdentity`
1163 // under `geom_line`/`geom_point`) still excludes the same raw,
1164 // unmapped `lower`/`middle`/`upper` here as it did above; a stat
1165 // that *produces* these fresh (`StatBoxplot`'s default, non-identity
1166 // path) was never in that set, since they didn't exist pre-stat.
1167 let removed = Self::drop_non_finite(
1168 &mut working_data,
1169 geom.allows_infinite(),
1170 geom.keeps_na_as_break(),
1171 &raw_unmapped,
1172 );
1173 if removed > 0 {
1174 warnings.push(format!(
1175 "{geom_label}: removed {removed} row{} containing missing values (stat_{} output)",
1176 if removed == 1 { "" } else { "s" },
1177 stat.name()
1178 ));
1179 }
1180
1181 // A layer with no data draws nothing — an empty input, or a stat that
1182 // cannot estimate anything (a density of one value, loess on two
1183 // points). It must not fail the whole plot. A stat's own required
1184 // aesthetics are still validated when there *was* input.
1185 if working_data.nrows() == 0 {
1186 if input_rows > 0 {
1187 for aes in &stat.required_aes() {
1188 let col_name = aes.col_name();
1189 if !pre_stat_columns.iter().any(|c| c == col_name) {
1190 return Err(GGError::ValidationError(format!(
1191 "stat_{} requires aesthetic '{}' but it was not provided",
1192 stat.name(),
1193 col_name
1194 )));
1195 }
1196 }
1197 warnings.push(format!(
1198 "{geom_label}: stat_{} produced no data from {input_rows} row{} \
1199 (too few or degenerate values); layer skipped",
1200 stat.name(),
1201 if input_rows == 1 { "" } else { "s" }
1202 ));
1203 }
1204 return Ok(BuiltLayer {
1205 data: DataFrame::new(),
1206 geom,
1207 show_legend,
1208 key_glyph,
1209 show_legend_aes,
1210 });
1211 }
1212
1213 // Step 6a-validate: A required aesthetic must have been supplied by the
1214 // user (pre-stat) or synthesized by the stat (post-stat). This lets
1215 // StatEcdf produce `y` for geom_step, while boxplot — which maps `y` then
1216 // consumes it into ymin/ymax — still validates. Empty input has the
1217 // column in neither place, so genuinely-missing aesthetics still error.
1218 for aes in &geom.required_aes() {
1219 let col_name = aes.col_name();
1220 let supplied = pre_stat_columns.iter().any(|c| c == col_name);
1221 if !supplied && !working_data.has_column(col_name) {
1222 return Err(GGError::ValidationError(format!(
1223 "geom_{} requires aesthetic '{}' but it was not provided",
1224 geom.name(),
1225 col_name
1226 )));
1227 }
1228 }
1229
1230 // Step 6b: Ensure scales for stat-computed aesthetics (e.g. y from StatCount/StatBin)
1231 let stat_aes = [
1232 ("x", Aesthetic::X),
1233 ("y", Aesthetic::Y),
1234 ("xmin", Aesthetic::X),
1235 ("xmax", Aesthetic::X),
1236 ("ymin", Aesthetic::Y),
1237 ("ymax", Aesthetic::Y),
1238 // Segment / curve ends are positions too (ggplot2 trains them;
1239 // #411's clip audit found segments running off the panel).
1240 ("xend", Aesthetic::X),
1241 ("yend", Aesthetic::Y),
1242 // Boxplot stat="identity" hinges (B3-5): live on the y scale.
1243 ("lower", Aesthetic::Y),
1244 ("middle", Aesthetic::Y),
1245 ("upper", Aesthetic::Y),
1246 // Boxplot outlier extent (B3-5, allowed #6): `GeomBoxplot::
1247 // setup_data` adds these (under an internal-prefix name — see
1248 // review #158 finding 5 and `aes::BOXPLOT_YMIN_FINAL`'s doc,
1249 // so a raw column can never forge them) from the parsed
1250 // `outliers` column, so the y scale trains on them too (step
1251 // 8b below).
1252 (crate::aes::BOXPLOT_YMIN_FINAL, Aesthetic::Y),
1253 (crate::aes::BOXPLOT_YMAX_FINAL, Aesthetic::Y),
1254 ];
1255 // `raw_unmapped` (a raw, unmapped `lower`/`middle`/`upper` passthrough
1256 // column) never trains or gets a scale here — only a layer that
1257 // actually mapped (or whose stat computed) that name does (review
1258 // #158's final check).
1259 for (col, aes) in &stat_aes {
1260 if working_data.has_column(col) && !raw_unmapped.contains(col) {
1261 scale_set.ensure_scale(aes, &working_data);
1262 }
1263 }
1264
1265 // Step 6c: bars/histograms/area draw from a 0 baseline, so the Y scale
1266 // must include 0 — for stat-computed Y (count/bin) and for an explicitly
1267 // mapped Y alike (e.g. geom_col), matching ggplot2.
1268 let y_is_user_mapped = merged_mapping.get_mapping(&Aesthetic::Y).is_some();
1269 if (!y_is_user_mapped || geom.include_zero_baseline()) && working_data.has_column("y") {
1270 if let Some(y_scale) = scale_set.get_mut(&Aesthetic::Y) {
1271 y_scale.train(&[crate::data::Value::Float(0.0)]);
1272 }
1273 }
1274
1275 // Step 7: Position adjustment. `validate` runs once per layer (D2)
1276 // regardless of whether the position is per-panel.
1277 position.validate(warnings);
1278 // #428: dodge groups in the legend's (scale level) order, whatever
1279 // the row order — precomputed `stat = "identity"` rows included.
1280 let params = PositionParams {
1281 group_order: position.group_order(&working_data).map(|order| {
1282 crate::position::dodge::order_by_scale_levels(order, &working_data, scale_set)
1283 }),
1284 ..PositionParams::default()
1285 };
1286 if position.per_panel() {
1287 Self::compute_position_per_panel(
1288 position.as_ref(),
1289 &mut working_data,
1290 ¶ms,
1291 facet_vars,
1292 );
1293 } else {
1294 position.compute(&mut working_data, ¶ms);
1295 }
1296
1297 // Step 7b: Geom-specific setup (e.g. tile/candlestick extents), then
1298 // make sure position scales exist for any extent columns it added.
1299 geom.setup_data(&mut working_data, warnings);
1300 for (col, aes) in &stat_aes {
1301 if working_data.has_column(col) && !raw_unmapped.contains(col) {
1302 scale_set.ensure_scale(aes, &working_data);
1303 }
1304 }
1305
1306 // Step 8: Train scales on this layer's data
1307 scale_set.train_layer(&working_data);
1308 for (col, aes) in &INTERCEPT_COLS {
1309 if let (Some(values), Some(scale)) = (working_data.column(col), scale_set.get_mut(aes))
1310 {
1311 train_intercepts(scale, values);
1312 }
1313 }
1314
1315 // Step 8b: Positional scales also need to see stat-computed extent columns
1316 // (e.g. boxplot/errorbar/pointrange emit ymin/ymax but no "y"). Without
1317 // this the Y (or X) scale would never train on the range and collapse.
1318 // Same `raw_unmapped` exclusion as Step 6b/7b above: an unmapped
1319 // raw `lower`/`middle`/`upper` must never stretch the y domain.
1320 for (col, aes) in &stat_aes {
1321 if raw_unmapped.contains(col) {
1322 continue;
1323 }
1324 if let Some(values) = working_data.column(col) {
1325 if let Some(scale) = scale_set.get_mut(aes) {
1326 scale.train(values);
1327 }
1328 }
1329 }
1330
1331 // Step 9 (B2-5): render a `tooltip()` template against the built
1332 // (post-stat) data, into the same internal column `Aes::tooltip`
1333 // uses — so it replaces the geom's default `<title>` the same way,
1334 // with no further geom-level plumbing.
1335 if let Some(template) = &tooltip_template {
1336 // Review PR#141 finding 6 (LOW): a template on a geom that
1337 // never reads that internal column (`set_row_extras`) did
1338 // nothing, silently — e.g. `geom_area`/`geom_boxplot`/
1339 // `geom_ribbon`.
1340 if !geom.consumes_tooltips() {
1341 warnings.push(format!(
1342 "{geom_label}: tooltip(\"{template}\") has no effect — this geom doesn't read tooltips, or the layer is decorative()"
1343 ));
1344 }
1345 let rendered: Vec<crate::data::Value> = (0..working_data.nrows())
1346 .map(|i| {
1347 crate::data::Value::Str(render_tooltip_template(
1348 template,
1349 &working_data,
1350 scale_set,
1351 i,
1352 ))
1353 })
1354 .collect();
1355 working_data.add_column(crate::aes::TOOLTIP_INTERNAL_NAME.to_string(), rendered);
1356 }
1357
1358 // #221: sanitize `Aes::class` values once per layer — keep valid
1359 // tokens, drop the rest with one warning — so `set_row_extras`
1360 // hands the backend only clean tokens.
1361 if let Some(col) = working_data.column_mut(crate::aes::CLASS_INTERNAL_NAME) {
1362 let mut rejected: Vec<String> = Vec::new();
1363 for v in col.iter_mut() {
1364 if v.is_na() {
1365 continue;
1366 }
1367 let (ok, bad) = crate::aes::split_class_tokens(&crate::geom::tip_value(v));
1368 for b in bad {
1369 if !rejected.contains(&b) {
1370 rejected.push(b);
1371 }
1372 }
1373 *v = crate::data::Value::Str(ok.join(" "));
1374 }
1375 if !rejected.is_empty() {
1376 let shown: Vec<&String> = rejected.iter().take(5).collect();
1377 warnings.push(format!(
1378 "{geom_label}: class: {} invalid class token(s) dropped (must match [A-Za-z][A-Za-z0-9_-]*), e.g. {shown:?}",
1379 rejected.len()
1380 ));
1381 }
1382 }
1383
1384 Ok(BuiltLayer {
1385 data: working_data,
1386 geom,
1387 show_legend,
1388 key_glyph,
1389 show_legend_aes,
1390 })
1391 }
1392
1393 /// Remove rows where x or y falls outside scale limits set via xlim/ylim.
1394 fn filter_oob_data(data: &mut DataFrame, scale_set: &ScaleSet) {
1395 let x_limits = scale_set.get(&Aesthetic::X).and_then(|s| s.filter_limits());
1396 let y_limits = scale_set.get(&Aesthetic::Y).and_then(|s| s.filter_limits());
1397
1398 if x_limits.is_none() && y_limits.is_none() {
1399 return;
1400 }
1401
1402 let nrows = data.nrows();
1403 let mut keep = vec![true; nrows];
1404
1405 if let Some((min, max)) = x_limits {
1406 if let Some(col) = data.column("x") {
1407 for (i, v) in col.iter().enumerate() {
1408 if let Some(f) = v.as_f64() {
1409 if f < min || f > max {
1410 keep[i] = false;
1411 }
1412 }
1413 }
1414 }
1415 }
1416
1417 if let Some((min, max)) = y_limits {
1418 if let Some(col) = data.column("y") {
1419 for (i, v) in col.iter().enumerate() {
1420 if let Some(f) = v.as_f64() {
1421 if f < min || f > max {
1422 keep[i] = false;
1423 }
1424 }
1425 }
1426 }
1427 }
1428
1429 // If nothing was filtered, skip the rebuild
1430 if keep.iter().all(|&k| k) {
1431 return;
1432 }
1433
1434 let indices: Vec<usize> = keep
1435 .iter()
1436 .enumerate()
1437 .filter(|(_, &k)| k)
1438 .map(|(i, _)| i)
1439 .collect();
1440
1441 let mut result = DataFrame::new();
1442 for col_name in data.column_names() {
1443 if let Some(src) = data.column(col_name) {
1444 let vals: Vec<_> = indices.iter().map(|&i| src[i].clone()).collect();
1445 result.add_column(col_name.to_string(), vals);
1446 }
1447 }
1448 *data = result;
1449 }
1450
1451 /// Compute per-panel scale sets for free facet scales.
1452 /// For each panel, clones the base scale set, resets freed axes, and retrains on panel data.
1453 fn compute_panel_scales(
1454 facet_scales: &FacetScales,
1455 panels_data: &[Vec<DataFrame>],
1456 base_scales: &ScaleSet,
1457 ) -> Vec<ScaleSet> {
1458 if matches!(facet_scales, FacetScales::Fixed) || panels_data.is_empty() {
1459 return vec![];
1460 }
1461
1462 let free_x = matches!(facet_scales, FacetScales::FreeX | FacetScales::Free);
1463 let free_y = matches!(facet_scales, FacetScales::FreeY | FacetScales::Free);
1464
1465 panels_data
1466 .iter()
1467 .map(|panel_layers| {
1468 let mut panel_set = base_scales.clone();
1469
1470 // Reset freed axis scales
1471 if free_x {
1472 if let Some(s) = panel_set.get_mut(&Aesthetic::X) {
1473 s.reset_training();
1474 }
1475 }
1476 if free_y {
1477 if let Some(s) = panel_set.get_mut(&Aesthetic::Y) {
1478 s.reset_training();
1479 }
1480 }
1481
1482 // Retrain on this panel's data, including extent columns
1483 // (bars/tiles/ranges), so free scales cover whole marks.
1484 for layer_data in panel_layers {
1485 panel_set.train_layer(layer_data);
1486 for (cols, aes) in [
1487 (["xmin", "xmax", "xend", "xintercept"], Aesthetic::X),
1488 (["ymin", "ymax", "yend", "yintercept"], Aesthetic::Y),
1489 ] {
1490 let freed = match aes {
1491 Aesthetic::X => free_x,
1492 _ => free_y,
1493 };
1494 if !freed {
1495 continue;
1496 }
1497 for c in cols {
1498 if let (Some(vals), Some(s)) =
1499 (layer_data.column(c), panel_set.get_mut(&aes))
1500 {
1501 if c.ends_with("intercept") {
1502 train_intercepts(s, vals);
1503 } else {
1504 s.train(vals);
1505 }
1506 }
1507 }
1508 }
1509 }
1510
1511 panel_set
1512 })
1513 .collect()
1514 }
1515
1516 /// Compute which aesthetics should be suppressed from the legend.
1517 /// An aesthetic is suppressed if every layer that has the corresponding column
1518 /// sets show_legend=Some(false), and no layer has it as None or Some(true).
1519 fn compute_suppressed_aes(built_layers: &[BuiltLayer]) -> std::collections::HashSet<Aesthetic> {
1520 use std::collections::HashSet;
1521 let legend_aes = [
1522 Aesthetic::Color,
1523 Aesthetic::Fill,
1524 Aesthetic::Shape,
1525 Aesthetic::Linetype,
1526 Aesthetic::Size,
1527 Aesthetic::Alpha,
1528 Aesthetic::Linewidth,
1529 ];
1530 let mut suppressed = HashSet::new();
1531 for aes in &legend_aes {
1532 let col_name = aes.col_name();
1533 let mut any_has = false;
1534 let mut all_hidden = true;
1535 for bl in built_layers {
1536 if bl.data.has_column(col_name) {
1537 any_has = true;
1538 match bl.shows_legend_for(aes) {
1539 Some(false) => {} // still hidden
1540 _ => {
1541 all_hidden = false;
1542 break;
1543 }
1544 }
1545 }
1546 }
1547 if any_has && all_hidden {
1548 suppressed.insert(aes.clone());
1549 }
1550 }
1551 suppressed
1552 }
1553
1554 /// Run a per-panel [`Position`](crate::position::Position) (B3-3, D8):
1555 /// partition the rows by the facet variables' joined key, in
1556 /// first-seen order, run `compute` on each panel separately, then
1557 /// scatter the rows back into their original positions — NA-filling
1558 /// any column a panel's `compute` added that another panel's didn't
1559 /// (there currently are none in practice: `PositionStack`/
1560 /// `PositionFill` add the same `ymin` everywhere). Falls back to the
1561 /// plain pooled `compute` when there's no facet column, or only one
1562 /// distinct panel — both byte-identical to the pooled path, since
1563 /// there's nothing to partition.
1564 fn compute_position_per_panel(
1565 position: &dyn crate::position::Position,
1566 data: &mut DataFrame,
1567 params: &PositionParams,
1568 facet_vars: &[String],
1569 ) {
1570 let active_vars: Vec<&str> = facet_vars
1571 .iter()
1572 .map(|s| s.as_str())
1573 .filter(|v| data.has_column(v))
1574 .collect();
1575 if active_vars.is_empty() {
1576 position.compute(data, params);
1577 return;
1578 }
1579
1580 let n = data.nrows();
1581 let mut order: Vec<String> = Vec::new();
1582 let mut groups: indexmap::IndexMap<String, Vec<usize>> = indexmap::IndexMap::new();
1583 for i in 0..n {
1584 let key = active_vars
1585 .iter()
1586 .map(|v| data.column(v).unwrap()[i].key_str().into_owned())
1587 .collect::<Vec<_>>()
1588 .join("\u{1}");
1589 if !groups.contains_key(&key) {
1590 order.push(key.clone());
1591 }
1592 groups.entry(key).or_default().push(i);
1593 }
1594 if order.len() < 2 {
1595 position.compute(data, params);
1596 return;
1597 }
1598
1599 // Review #167 finding 2: a per-panel position's group *rank*
1600 // (e.g. `PositionDodge`) must come from the whole layer, computed
1601 // here against the still-pooled `data` — before it's split into
1602 // the panels below — so every panel ranks a given group the same
1603 // way regardless of which group its own rows happen to list
1604 // first (sipemu/ggplot-rs#153; matches 0.22.2 and ggplot2 4.0.3's
1605 // global `group` id). Positions that don't override `group_order`
1606 // (stack/fill/jitter) get `None` back, unchanged from before.
1607 let mut panel_params = params.clone();
1608 if panel_params.group_order.is_none() {
1609 panel_params.group_order = position.group_order(data);
1610 }
1611
1612 let col_names: Vec<String> = data.column_names().iter().map(|s| s.to_string()).collect();
1613 let mut panels: Vec<(Vec<usize>, DataFrame)> = Vec::with_capacity(order.len());
1614 for key in &order {
1615 let indices = &groups[key];
1616 let mut sub = DataFrame::new();
1617 for name in &col_names {
1618 let col = data.column(name).unwrap();
1619 let values: Vec<crate::data::Value> =
1620 indices.iter().map(|&i| col[i].clone()).collect();
1621 sub.add_column(name.clone(), values);
1622 }
1623 position.compute(&mut sub, &panel_params);
1624 panels.push((indices.clone(), sub));
1625 }
1626
1627 // Union of every column present after `compute` (new columns like
1628 // `ymin` may have been added), so a panel that for some reason
1629 // didn't add one still gets NA there rather than a stale value.
1630 let mut all_cols = col_names.clone();
1631 for (_, sub) in &panels {
1632 for name in sub.column_names() {
1633 if !all_cols.iter().any(|c| c == name) {
1634 all_cols.push(name.to_string());
1635 }
1636 }
1637 }
1638 for name in &all_cols {
1639 let mut full = vec![crate::data::Value::Na; n];
1640 for (indices, sub) in &panels {
1641 if let Some(col) = sub.column(name) {
1642 for (j, &i) in indices.iter().enumerate() {
1643 full[i] = col[j].clone();
1644 }
1645 }
1646 }
1647 data.add_column(name.clone(), full);
1648 }
1649 }
1650
1651 /// Detect which columns to group by for statistics.
1652 /// Checks group/color/fill plus discrete x (like R's auto-grouping by discrete x).
1653 fn detect_group_columns(data: &DataFrame) -> Vec<String> {
1654 detect_group_columns(data)
1655 }
1656}
1657
1658/// The columns whose discrete values split a layer's rows into aesthetic
1659/// groups (`group`, `color`, `fill`, a discrete `x`). Also used by panelwise
1660/// stats that regroup inside a panel ([`crate::stat::align::StatAlign`]).
1661pub(crate) fn detect_group_columns(data: &DataFrame) -> Vec<String> {
1662 let candidates = ["group", "color", "fill", "x"];
1663 let mut group_cols = Vec::new();
1664 for &col in &candidates {
1665 if data.has_column(col) {
1666 if let Some(values) = data.column(col) {
1667 let is_discrete = values
1668 .iter()
1669 .any(|v| matches!(v, crate::data::Value::Str(_)));
1670 if is_discrete {
1671 group_cols.push(col.to_string());
1672 }
1673 }
1674 }
1675 }
1676 group_cols
1677}