Skip to main content

ggplot_rs/geom/
boxplot.rs

1use crate::aes::Aesthetic;
2use crate::coord::Coord;
3use crate::data::{DataFrame, Value};
4use crate::position::identity::PositionIdentity;
5use crate::position::Position;
6use crate::render::backend::{DrawBackend, LineStyle, Linetype, PointShape, PointStyle, RectStyle};
7use crate::render::RenderError;
8use crate::scale::ScaleSet;
9use crate::stat::boxplot::StatBoxplot;
10use crate::stat::Stat;
11use crate::theme::Theme;
12
13use super::{Geom, GeomParams};
14
15/// Boxplot geometry.
16pub struct GeomBoxplot {
17    pub fill: (u8, u8, u8),
18    pub color: (u8, u8, u8),
19    pub width: f64,
20    pub alpha: f64,
21}
22
23impl Default for GeomBoxplot {
24    fn default() -> Self {
25        GeomBoxplot {
26            fill: (255, 255, 255),
27            color: (50, 50, 50),
28            width: 0.75,
29            alpha: 1.0,
30        }
31    }
32}
33
34impl Geom for GeomBoxplot {
35    fn draw(
36        &self,
37        data: &DataFrame,
38        coord: &dyn Coord,
39        scales: &ScaleSet,
40        _theme: &Theme,
41        backend: &mut dyn DrawBackend,
42    ) -> Result<(), RenderError> {
43        let x_col = data
44            .column("x")
45            .ok_or(RenderError::MissingAesthetic("x".into()))?;
46        let lower_col = data.column("lower");
47        let middle_col = data.column("middle");
48        let upper_col = data.column("upper");
49        let ymin_col = data.column("ymin");
50        let ymax_col = data.column("ymax");
51        let outliers_col = data.column("outliers");
52
53        let plot_area = backend.plot_area();
54        let x_scale = scales.get(&Aesthetic::X);
55        let y_scale = scales.get(&Aesthetic::Y);
56
57        let x_is_discrete = x_scale.map(|s| s.is_discrete()).unwrap_or(false);
58
59        for i in 0..data.nrows() {
60            let nx = x_scale.map(|s| s.map(&x_col[i])).unwrap_or(0.5);
61
62            let half_width = if x_is_discrete {
63                let n = x_scale.map(|s| s.breaks().len()).unwrap_or(1);
64                self.width / (n.max(1) as f64 * 2.5)
65            } else {
66                0.03
67            };
68
69            // Get boxplot values
70            let lower = lower_col.and_then(|c| c[i].as_f64()).unwrap_or(0.0);
71            let middle = middle_col.and_then(|c| c[i].as_f64()).unwrap_or(0.0);
72            let upper = upper_col.and_then(|c| c[i].as_f64()).unwrap_or(0.0);
73            let ymin = ymin_col.and_then(|c| c[i].as_f64()).unwrap_or(lower);
74            let ymax = ymax_col.and_then(|c| c[i].as_f64()).unwrap_or(upper);
75
76            let map_y = |v: f64| y_scale.map(|s| s.map(&Value::Float(v))).unwrap_or(0.0);
77
78            // Box (IQR)
79            let (box_left, box_top) = coord.transform((nx - half_width, map_y(upper)), &plot_area);
80            let (box_right, box_bottom) =
81                coord.transform((nx + half_width, map_y(lower)), &plot_area);
82            backend.draw_rect(
83                (box_left, box_top),
84                (box_right, box_bottom),
85                &RectStyle {
86                    fill: Some(self.fill),
87                    stroke: Some(self.color),
88                    stroke_width: 1.0,
89                    alpha: self.alpha,
90                    clip: true,
91                },
92            )?;
93
94            // Median line
95            let (med_left, med_y) = coord.transform((nx - half_width, map_y(middle)), &plot_area);
96            let (med_right, _) = coord.transform((nx + half_width, map_y(middle)), &plot_area);
97            backend.draw_line(
98                &[(med_left, med_y), (med_right, med_y)],
99                &LineStyle {
100                    color: self.color,
101                    width: 2.0,
102                    alpha: 1.0,
103                    linetype: Linetype::Solid,
104                },
105            )?;
106
107            // Lower whisker
108            let (_, whisker_bottom) = coord.transform((nx, map_y(ymin)), &plot_area);
109            let (center_x, _) = coord.transform((nx, map_y(lower)), &plot_area);
110            backend.draw_line(
111                &[(center_x, box_bottom), (center_x, whisker_bottom)],
112                &LineStyle {
113                    color: self.color,
114                    width: 1.0,
115                    alpha: 1.0,
116                    linetype: Linetype::Solid,
117                },
118            )?;
119            // Lower whisker cap
120            let (wl, _) = coord.transform((nx - half_width * 0.5, map_y(ymin)), &plot_area);
121            let (wr, _) = coord.transform((nx + half_width * 0.5, map_y(ymin)), &plot_area);
122            backend.draw_line(
123                &[(wl, whisker_bottom), (wr, whisker_bottom)],
124                &LineStyle {
125                    color: self.color,
126                    width: 1.0,
127                    alpha: 1.0,
128                    linetype: Linetype::Solid,
129                },
130            )?;
131
132            // Upper whisker
133            let (_, whisker_top) = coord.transform((nx, map_y(ymax)), &plot_area);
134            backend.draw_line(
135                &[(center_x, box_top), (center_x, whisker_top)],
136                &LineStyle {
137                    color: self.color,
138                    width: 1.0,
139                    alpha: 1.0,
140                    linetype: Linetype::Solid,
141                },
142            )?;
143            // Upper whisker cap
144            let (wl, _) = coord.transform((nx - half_width * 0.5, map_y(ymax)), &plot_area);
145            let (wr, _) = coord.transform((nx + half_width * 0.5, map_y(ymax)), &plot_area);
146            backend.draw_line(
147                &[(wl, whisker_top), (wr, whisker_top)],
148                &LineStyle {
149                    color: self.color,
150                    width: 1.0,
151                    alpha: 1.0,
152                    linetype: Linetype::Solid,
153                },
154            )?;
155
156            // Outliers
157            if let Some(oc) = outliers_col {
158                if let Value::Str(s) = &oc[i] {
159                    for part in s.split(',') {
160                        if let Ok(val) = part.trim().parse::<f64>() {
161                            let ny = map_y(val);
162                            let (ox, oy) = coord.transform((nx, ny), &plot_area);
163                            backend.draw_circle(
164                                (ox, oy),
165                                2.0,
166                                &PointStyle {
167                                    color: self.color,
168                                    alpha: 1.0,
169                                    filled: false,
170                                    shape: PointShape::Circle,
171                                },
172                            )?;
173                        }
174                    }
175                }
176            }
177        }
178
179        Ok(())
180    }
181
182    fn required_aes(&self) -> Vec<Aesthetic> {
183        vec![Aesthetic::X, Aesthetic::Y]
184    }
185
186    fn default_stat(&self) -> Box<dyn Stat> {
187        Box::new(StatBoxplot)
188    }
189
190    fn default_position(&self) -> Box<dyn Position> {
191        Box::new(PositionIdentity)
192    }
193
194    fn default_params(&self) -> GeomParams {
195        GeomParams::default()
196    }
197
198    fn name(&self) -> &str {
199        "boxplot"
200    }
201
202    fn set_series_color(&mut self, color: (u8, u8, u8)) {
203        self.fill = color;
204    }
205}