Skip to main content

ggplot_rs/position/
dodge.rs

1use crate::data::{DataFrame, Value};
2
3use super::{Position, PositionParams, DODGE_N_COL, DODGE_OFFSET_COL};
4
5/// Place groups side-by-side (ggplot2's `position_dodge(width)`).
6///
7/// Within each x position the groups present there (group / fill / colour
8/// level, in first-appearance order — the legend order — or reversed with
9/// [`with_reverse`](Self::with_reverse)) share `width` (in x units; one
10/// category on a discrete axis) and are centred on x:
11/// `offset = (k − (n − 1)/2) · width / n` for the k-th of n groups. On a
12/// continuous axis x itself moves; on a discrete axis the offset is stored in
13/// the `.x_dodge_offset` column and applied by the geoms that support it
14/// (point, pointrange, errorbar, linerange, ribbon, censor marks), so several
15/// models' intervals per term sit side by side — also under `coord_flip`.
16///
17/// `PositionDodge` is also usable as a value (the default, `width` from the
18/// layer, 0.9), as before 0.17.
19#[derive(Clone, Debug, Default, PartialEq)]
20pub struct PositionDodge {
21    /// Total dodge width; `None` = the layer default (0.9).
22    pub width: Option<f64>,
23    /// Reverse the group order (ggplot2 ≥ 3.5 `reverse = TRUE`).
24    pub reverse: bool,
25    /// ggplot2's `preserve = "single"`: every element gets the width of one
26    /// group in the fullest slot, so a slot with fewer groups doesn't widen
27    /// its elements (they stay centred). `false` = `preserve = "total"`.
28    pub preserve_single: bool,
29}
30
31/// The default dodge (`width` 0.9, groups in legend order): lets
32/// `.position(PositionDodge)` keep compiling now that the type has fields.
33#[allow(non_upper_case_globals)]
34pub const PositionDodge: PositionDodge = PositionDodge {
35    width: None,
36    reverse: false,
37    preserve_single: false,
38};
39
40impl PositionDodge {
41    /// Dodge with an explicit total `width` (ggplot2's `position_dodge(width)`).
42    pub fn new(width: f64) -> Self {
43        PositionDodge {
44            width: Some(width),
45            reverse: false,
46            preserve_single: false,
47        }
48    }
49
50    /// Reverse the order of the dodged groups.
51    pub fn with_reverse(mut self, reverse: bool) -> Self {
52        self.reverse = reverse;
53        self
54    }
55
56    /// `preserve = "single"` (see [`preserve_single`](Self::preserve_single)).
57    pub fn with_preserve_single(mut self) -> Self {
58        self.preserve_single = true;
59        self
60    }
61}
62
63/// `position_dodge(width)` — see [`PositionDodge`](struct@PositionDodge).
64pub fn position_dodge(width: f64) -> PositionDodge {
65    PositionDodge::new(width)
66}
67
68impl Position for PositionDodge {
69    fn compute(&self, data: &mut DataFrame, params: &PositionParams) {
70        dodge_rows(
71            data,
72            self.width.unwrap_or(params.width),
73            None,
74            self.reverse,
75            self.preserve_single,
76            params.group_order.as_deref(),
77        );
78    }
79
80    fn name(&self) -> &str {
81        "dodge"
82    }
83
84    /// ggplot2 computes `position_dodge`'s group count/offset per facet
85    /// panel (`compute_panel`), not pooled across panels (#153) — verified
86    /// against ggplot2 4.0.3: a group missing from one panel's x level
87    /// doesn't shift the groups that panel does have.
88    fn per_panel(&self) -> bool {
89        true
90    }
91
92    /// Review #167 finding 2: the group *rank* still comes from the whole
93    /// layer (first-appearance order across every panel), even though the
94    /// per-x group *count* stays per-panel. Without this, a panel whose
95    /// rows happen to list groups in a different order than another panel
96    /// drew that group on the opposite side — a regression vs 0.22.2 and
97    /// ggplot2 4.0.3, which both rank by the global `group` id.
98    fn group_order(&self, data: &DataFrame) -> Option<Vec<String>> {
99        group_keys(data).map(|keys| dedup_order(&keys))
100    }
101}
102
103/// First-appearance-order, de-duplicated group keys — the "legend order"
104/// `group_order` (and, absent an external order, `dodge_rows` itself)
105/// ranks groups by.
106pub(crate) fn dedup_order(keys: &[String]) -> Vec<String> {
107    let set: indexmap::IndexSet<&str> = keys.iter().map(|k| k.as_str()).collect();
108    set.into_iter().map(str::to_string).collect()
109}
110
111/// The dodge key of each row: its group / fill / colour levels combined
112/// (`None` when the frame has none of these columns).
113pub(crate) fn group_keys(data: &DataFrame) -> Option<Vec<String>> {
114    let cols: Vec<&[Value]> = ["group", "fill", "color"]
115        .iter()
116        .filter_map(|c| data.column(c))
117        .collect();
118    if cols.is_empty() {
119        return None;
120    }
121    Some(
122        (0..data.nrows())
123            .map(|i| {
124                cols.iter()
125                    .map(|c| c[i].to_group_key())
126                    .collect::<Vec<_>>()
127                    .join("\u{1f}")
128            })
129            .collect(),
130    )
131}
132
133/// Shared dodge / dodge2 implementation. `padding` (dodge2) also rewrites
134/// `xmin`/`xmax` on a continuous axis to the shrunken element extent.
135///
136/// `external_order` (review #167 finding 2) is the whole layer's group
137/// rank order, computed by `build::compute_position_per_panel` from
138/// [`Position::group_order`] before the layer's rows were split into
139/// panels. When given, it — not this call's own (possibly per-panel)
140/// `data` — decides group rank; `None` (the non-per-panel call path, and
141/// every pre-#153 caller) falls back to the previous, locally-computed
142/// order.
143pub(crate) fn dodge_rows(
144    data: &mut DataFrame,
145    width: f64,
146    padding: Option<f64>,
147    reverse: bool,
148    preserve_single: bool,
149    external_order: Option<&[String]>,
150) {
151    let Some(x_col) = data.column("x").map(|c| c.to_vec()) else {
152        return;
153    };
154    let Some(keys) = group_keys(data) else {
155        return;
156    };
157    let width = if width.is_finite() && width > 0.0 {
158        width
159    } else {
160        0.9
161    };
162    // Group order (legend order), optionally reversed: the whole layer's
163    // order when given (#167 finding 2), else this call's own data —
164    // identical to the whole layer's order whenever there's no per-panel
165    // split (the non-per-panel path, and direct `compute()` calls in unit
166    // tests below).
167    let mut order: indexmap::IndexSet<&str> = match external_order {
168        Some(ext) => ext.iter().map(|s| s.as_str()).collect(),
169        None => keys.iter().map(|k| k.as_str()).collect(),
170    };
171    if order.len() <= 1 {
172        return;
173    }
174    if reverse {
175        order.reverse();
176    }
177    let rank = |k: &str| order.get_index_of(k).unwrap_or(0);
178
179    // Groups present at each x position, sorted by the global order.
180    let mut slots: indexmap::IndexMap<String, Vec<usize>> = indexmap::IndexMap::new();
181    for (i, x) in x_col.iter().enumerate() {
182        if x.is_na() {
183            continue;
184        }
185        let present = slots.entry(x.to_group_key()).or_default();
186        let r = rank(&keys[i]);
187        if !present.contains(&r) {
188            present.push(r);
189        }
190    }
191    for present in slots.values_mut() {
192        present.sort_unstable();
193    }
194    // `preserve = "single"`: size every element as one of the fullest slot's.
195    let max_n = slots.values().map(Vec::len).max().unwrap_or(1) as f64;
196
197    let discrete = x_col
198        .iter()
199        .any(|v| matches!(v, Value::Str(_) | Value::Bool(_)));
200    let mut offsets = vec![Value::Float(0.0); x_col.len()];
201    let mut counts = vec![Value::Float(1.0); x_col.len()];
202    let mut new_x = x_col.clone();
203    let mut new_xmin = data.column("xmin").map(|c| c.to_vec());
204    let mut new_xmax = data.column("xmax").map(|c| c.to_vec());
205    for (i, x) in x_col.iter().enumerate() {
206        let Some(present) = slots.get(&x.to_group_key()).filter(|_| !x.is_na()) else {
207            continue;
208        };
209        let n = present.len() as f64;
210        let k = present
211            .iter()
212            .position(|&r| r == rank(&keys[i]))
213            .unwrap_or(0) as f64;
214        let n_width = if preserve_single { max_n } else { n };
215        let group_width = width / n_width;
216        let offset = (k - (n - 1.0) / 2.0) * group_width;
217        if discrete {
218            offsets[i] = Value::Float(offset);
219            counts[i] = Value::Float(n_width);
220            continue;
221        }
222        let Some(xv) = x.as_f64() else {
223            continue;
224        };
225        let center = xv + offset;
226        new_x[i] = Value::Float(center);
227        if let Some(pad) = padding {
228            let half = group_width * (1.0 - pad) / 2.0;
229            if let Some(c) = new_xmin.as_mut() {
230                c[i] = Value::Float(center - half);
231            }
232            if let Some(c) = new_xmax.as_mut() {
233                c[i] = Value::Float(center + half);
234            }
235        }
236    }
237
238    if discrete {
239        if let Some(col) = data.column_mut(DODGE_OFFSET_COL) {
240            *col = offsets;
241        } else {
242            data.add_column(DODGE_OFFSET_COL.to_string(), offsets);
243        }
244        if let Some(col) = data.column_mut(DODGE_N_COL) {
245            *col = counts;
246        } else {
247            data.add_column(DODGE_N_COL.to_string(), counts);
248        }
249        return;
250    }
251    if let Some(col) = data.column_mut("x") {
252        *col = new_x;
253    }
254    if padding.is_some() {
255        for (name, vals) in [("xmin", new_xmin), ("xmax", new_xmax)] {
256            if let (Some(vals), Some(col)) = (vals, data.column_mut(name)) {
257                *col = vals;
258            }
259        }
260    }
261}
262
263#[cfg(test)]
264mod tests {
265    use super::*;
266
267    fn frame(x: Vec<Value>, g: &[&str]) -> DataFrame {
268        let mut df = DataFrame::new();
269        df.add_column("x".into(), x);
270        df.add_column(
271            "color".into(),
272            g.iter().map(|s| Value::Str(s.to_string())).collect(),
273        );
274        df
275    }
276
277    fn col(df: &DataFrame, c: &str) -> Vec<f64> {
278        df.column(c)
279            .unwrap()
280            .iter()
281            .map(|v| v.as_f64().unwrap())
282            .collect()
283    }
284
285    #[test]
286    fn continuous_dodge_matches_ggplot2() {
287        // ggplot2: position_dodge(width = 0.6) of 3 groups at x = 1 →
288        // 0.8, 1.0, 1.2.
289        let mut df = frame(vec![Value::Float(1.0); 3], &["a", "b", "c"]);
290        PositionDodge::new(0.6).compute(&mut df, &PositionParams::default());
291        let x = col(&df, "x");
292        for (got, want) in x.iter().zip([0.8, 1.0, 1.2]) {
293            assert!((got - want).abs() < 1e-12);
294        }
295        let mut df = frame(vec![Value::Float(1.0); 3], &["a", "b", "c"]);
296        PositionDodge::new(0.6)
297            .with_reverse(true)
298            .compute(&mut df, &PositionParams::default());
299        let x = col(&df, "x");
300        assert!((x[0] - 1.2).abs() < 1e-12 && (x[2] - 0.8).abs() < 1e-12);
301    }
302
303    #[test]
304    fn discrete_dodge_stores_offsets_and_centres_partial_slots() {
305        let x: Vec<Value> = ["t1", "t1", "t2"]
306            .iter()
307            .map(|s| Value::Str(s.to_string()))
308            .collect();
309        let mut df = frame(x, &["m1", "m2", "m2"]);
310        PositionDodge::new(0.5).compute(&mut df, &PositionParams::default());
311        // x itself untouched (a category), offsets in category units.
312        assert_eq!(df.column("x").unwrap()[0].as_str(), Some("t1"));
313        let off = col(&df, DODGE_OFFSET_COL);
314        assert!((off[0] + 0.125).abs() < 1e-12);
315        assert!((off[1] - 0.125).abs() < 1e-12);
316        // t2 has only m2 → centred (ggplot2's preserve = "total").
317        assert_eq!(off[2], 0.0);
318    }
319
320    #[test]
321    fn preserve_single_keeps_element_width_in_partial_slots() {
322        let x: Vec<Value> = ["t1", "t1", "t1", "t2"]
323            .iter()
324            .map(|s| Value::Str(s.to_string()))
325            .collect();
326        let mut df = frame(x, &["a", "b", "c", "a"]);
327        PositionDodge::new(0.9)
328            .with_preserve_single()
329            .compute(&mut df, &PositionParams::default());
330        // Every element is one third of the slot wide, also t2's lone one,
331        // which stays centred.
332        assert_eq!(col(&df, DODGE_N_COL), vec![3.0, 3.0, 3.0, 3.0]);
333        assert_eq!(col(&df, DODGE_OFFSET_COL)[3], 0.0);
334        // Continuous x: t2-like partial slot keeps its centre.
335        let mut df = frame(
336            vec![Value::Float(1.0), Value::Float(1.0), Value::Float(2.0)],
337            &["a", "b", "a"],
338        );
339        PositionDodge::new(0.6)
340            .with_preserve_single()
341            .compute(&mut df, &PositionParams::default());
342        let xs = col(&df, "x");
343        assert!((xs[0] - 0.85).abs() < 1e-12 && (xs[1] - 1.15).abs() < 1e-12);
344        assert!((xs[2] - 2.0).abs() < 1e-12);
345    }
346
347    #[test]
348    fn unit_value_still_works() {
349        let mut df = frame(vec![Value::Float(1.0); 2], &["a", "b"]);
350        PositionDodge.compute(&mut df, &PositionParams::default());
351        let x = col(&df, "x");
352        assert!((x[0] - 0.775).abs() < 1e-12 && (x[1] - 1.225).abs() < 1e-12);
353    }
354}