ggplot-rs 0.9.0

A Rust implementation of ggplot2's Grammar of Graphics
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use super::elements::{ElementLine, ElementRect, ElementText};
use super::{LegendPosition, Margin, Theme};

/// Default base font size (matches ggplot2's default of 11pt).
const DEFAULT_BASE_SIZE: f64 = 11.0;

/// Compute text sizes relative to a base size, matching ggplot2 proportions.
fn text_sizes(base_size: f64) -> (f64, f64, f64) {
    let title = base_size * 1.2; // rel(1.2)
    let axis_title = base_size; // inherits from text (rel(1.0))
    let axis_text = base_size * 0.8; // rel(0.8)
    (title, axis_title, axis_text)
}

// ─── theme_gray ──────────────────────────────────────────────────

/// Classic ggplot2 gray theme with default base size.
pub fn theme_gray() -> Theme {
    theme_gray_base(DEFAULT_BASE_SIZE)
}

/// Classic ggplot2 gray theme with custom base font size.
///
/// Matches ggplot2's `theme_grey()` defaults:
/// - Black text, grey30 axis text, grey20 ticks
/// - Grey92 panel background with white gridlines
/// - Axis lines blank (panel defined by background, not lines)
/// - Grey85 strip background, grey10 strip text
pub fn theme_gray_base(base_size: f64) -> Theme {
    let (title_size, axis_title_size, axis_text_size) = text_sizes(base_size);
    let half_line = base_size / 2.0;
    Theme {
        text: ElementText {
            size: base_size,
            color: (0, 0, 0),
            ..Default::default()
        },
        title: ElementText {
            size: title_size,
            color: (0, 0, 0),
            ..Default::default()
        },
        axis_text_x: ElementText {
            size: axis_text_size,
            color: (77, 77, 77), // grey30
            ..Default::default()
        },
        axis_text_y: ElementText {
            size: axis_text_size,
            color: (77, 77, 77), // grey30
            hjust: 1.0,
            ..Default::default()
        },
        axis_title_x: ElementText {
            size: axis_title_size,
            color: (0, 0, 0),
            ..Default::default()
        },
        axis_title_y: ElementText {
            size: axis_title_size,
            color: (0, 0, 0),
            angle: 90.0,
            ..Default::default()
        },
        axis_line: ElementLine::blank(), // ggplot2: element_blank()
        axis_ticks: ElementLine {
            color: (51, 51, 51), // grey20
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_background: ElementRect {
            fill: Some((235, 235, 235)), // grey92
            color: None,
            width: 0.0,
            visible: true,
        },
        panel_grid_major: ElementLine {
            color: (255, 255, 255), // white
            width: 1.0,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_minor: ElementLine {
            color: (255, 255, 255), // white (same as major, just thinner)
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        plot_background: ElementRect {
            fill: Some((255, 255, 255)),
            color: None,
            width: 0.0,
            visible: true,
        },
        legend_position: LegendPosition::Right,
        plot_margin: Margin {
            top: half_line,
            right: half_line,
            bottom: half_line,
            left: half_line,
        },

        // ── New text elements ──
        subtitle: ElementText {
            size: base_size,
            color: (0, 0, 0),
            ..Default::default()
        },
        caption: ElementText {
            size: axis_text_size, // rel(0.8)
            color: (0, 0, 0),
            hjust: 1.0, // right-aligned by default (R's plot.caption)
            ..Default::default()
        },
        legend_title: ElementText {
            size: base_size,
            color: (0, 0, 0),
            ..Default::default()
        },
        legend_text: ElementText {
            size: axis_text_size, // rel(0.8)
            color: (0, 0, 0),
            ..Default::default()
        },
        strip_text: ElementText {
            size: axis_text_size, // rel(0.8)
            color: (26, 26, 26),  // grey10
            ..Default::default()
        },

        // ── Per-axis overrides (None = inherit) ──
        axis_line_x: None,
        axis_line_y: None,
        axis_ticks_x: None,
        axis_ticks_y: None,
        panel_grid_major_x: None,
        panel_grid_major_y: None,
        panel_grid_minor_x: None,
        panel_grid_minor_y: None,

        // ── New rect/line elements ──
        panel_border: ElementLine::blank(),
        legend_background: ElementRect {
            fill: Some((255, 255, 255)),
            color: None,
            width: 0.0,
            visible: true,
        },
        legend_key: ElementRect {
            fill: Some((242, 242, 242)), // grey95
            color: None,
            width: 0.0,
            visible: true,
        },
        strip_background: ElementRect {
            fill: Some((217, 217, 217)), // grey85
            color: None,                 // colour = NA
            width: 0.0,
            visible: true,
        },

        // ── Scalar spacing/sizing ──
        axis_ticks_length: half_line / 2.0,
        axis_text_x_dodge: 1,
        legend_key_width: 12.0,
        legend_key_height: 18.0,
        legend_spacing: 4.0,
        legend_margin: Margin {
            top: 10.0,
            right: 15.0,
            bottom: 10.0,
            left: 10.0,
        },
        panel_spacing: half_line,
        panel_spacing_x: None,
        panel_spacing_y: None,
        primary: None,
        aspect_ratio: None,
        panel_ontop: false,
        axis_minor_ticks: false,
        title_position: crate::theme::TitlePosition::Panel,
        tag_position: crate::theme::TagPosition::TopLeft,
        legend_direction: None,
    }
}

// ─── theme_bw ────────────────────────────────────────────────────

/// Black and white theme (default base size).
pub fn theme_bw() -> Theme {
    theme_bw_base(DEFAULT_BASE_SIZE)
}

/// Black and white theme with custom base font size.
///
/// Inherits from theme_grey. White panel with grey20 border,
/// grey92 gridlines on white background.
pub fn theme_bw_base(base_size: f64) -> Theme {
    Theme {
        panel_background: ElementRect {
            fill: Some((255, 255, 255)),
            color: None,
            width: 0.0,
            visible: true,
        },
        panel_border: ElementLine {
            color: (51, 51, 51), // grey20
            width: 1.0,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_major: ElementLine {
            color: (235, 235, 235), // grey92
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_minor: ElementLine {
            color: (235, 235, 235), // grey92, thinner
            width: 0.25,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        strip_background: ElementRect {
            fill: Some((217, 217, 217)), // grey85
            color: Some((51, 51, 51)),   // grey20
            width: 0.5,
            visible: true,
        },
        legend_key: ElementRect {
            fill: Some((255, 255, 255)), // white
            color: None,
            width: 0.0,
            visible: true,
        },
        ..theme_gray_base(base_size)
    }
}

// ─── theme_minimal ───────────────────────────────────────────────

/// Minimal theme with no panel background (default base size).
pub fn theme_minimal() -> Theme {
    theme_minimal_base(DEFAULT_BASE_SIZE)
}

/// Minimal theme with no backgrounds. Inherits from theme_bw.
pub fn theme_minimal_base(base_size: f64) -> Theme {
    Theme {
        axis_ticks: ElementLine::blank(),
        panel_background: ElementRect::blank(),
        panel_border: ElementLine::blank(),
        panel_grid_major: ElementLine {
            color: (235, 235, 235), // grey92 (from bw)
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_minor: ElementLine {
            color: (235, 235, 235),
            width: 0.25,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        plot_background: ElementRect::blank(),
        legend_background: ElementRect::blank(),
        legend_key: ElementRect::blank(),
        strip_background: ElementRect::blank(),
        ..theme_bw_base(base_size)
    }
}

// ─── theme_classic ───────────────────────────────────────────────

/// Classic theme: white background, no gridlines, L-shaped axis lines only.
/// Traditional academic/publication style. Inherits from theme_bw.
pub fn theme_classic() -> Theme {
    theme_classic_base(DEFAULT_BASE_SIZE)
}

/// Classic theme with custom base font size.
pub fn theme_classic_base(base_size: f64) -> Theme {
    Theme {
        panel_border: ElementLine::blank(),
        panel_grid_major: ElementLine::blank(),
        panel_grid_minor: ElementLine::blank(),
        axis_line: ElementLine {
            color: (0, 0, 0),
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        axis_ticks: ElementLine {
            color: (0, 0, 0),
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        strip_background: ElementRect {
            fill: Some((255, 255, 255)),
            color: Some((0, 0, 0)),
            width: 1.0,
            visible: true,
        },
        ..theme_bw_base(base_size)
    }
}

// ─── theme_linedraw ──────────────────────────────────────────────

/// Linedraw theme: white background, black panel border, very thin black gridlines.
/// Technical drawing aesthetic. Inherits from theme_bw.
pub fn theme_linedraw() -> Theme {
    theme_linedraw_base(DEFAULT_BASE_SIZE)
}

/// Linedraw theme with custom base font size.
pub fn theme_linedraw_base(base_size: f64) -> Theme {
    Theme {
        panel_border: ElementLine {
            color: (0, 0, 0),
            width: 1.0,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_major: ElementLine {
            color: (0, 0, 0), // black, very thin
            width: 0.1,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_minor: ElementLine {
            color: (0, 0, 0), // black, extremely thin
            width: 0.05,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        axis_ticks: ElementLine {
            color: (0, 0, 0),
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        strip_background: ElementRect {
            fill: Some((0, 0, 0)), // black
            color: None,
            width: 0.0,
            visible: true,
        },
        strip_text: ElementText {
            size: base_size * 0.8,
            color: (255, 255, 255), // white text on black strip
            ..Default::default()
        },
        ..theme_bw_base(base_size)
    }
}

// ─── theme_light ─────────────────────────────────────────────────

/// Light theme: white background, light gray panel border and gridlines.
/// Softer version with grey70 accents. Inherits from theme_grey.
pub fn theme_light() -> Theme {
    theme_light_base(DEFAULT_BASE_SIZE)
}

/// Light theme with custom base font size.
pub fn theme_light_base(base_size: f64) -> Theme {
    Theme {
        panel_background: ElementRect {
            fill: Some((255, 255, 255)),
            color: None,
            width: 0.0,
            visible: true,
        },
        panel_border: ElementLine {
            color: (179, 179, 179), // grey70
            width: 1.0,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_major: ElementLine {
            color: (222, 222, 222), // grey87
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_minor: ElementLine {
            color: (222, 222, 222), // grey87, thinner
            width: 0.25,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        axis_ticks: ElementLine {
            color: (179, 179, 179), // grey70
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        legend_key: ElementRect {
            fill: Some((255, 255, 255)),
            color: None,
            width: 0.0,
            visible: true,
        },
        strip_background: ElementRect {
            fill: Some((179, 179, 179)), // grey70
            color: None,
            width: 0.0,
            visible: true,
        },
        strip_text: ElementText {
            size: base_size * 0.8,
            color: (255, 255, 255), // white text on grey70 strip
            ..Default::default()
        },
        ..theme_gray_base(base_size)
    }
}

// ─── theme_dark ──────────────────────────────────────────────────

/// Dark theme: white plot background with dark grey50 panel.
/// Makes colored data pop. Inherits from theme_grey.
pub fn theme_dark() -> Theme {
    theme_dark_base(DEFAULT_BASE_SIZE)
}

/// Dark theme with custom base font size.
///
/// Note: ggplot2's theme_dark has a white plot background but dark panel.
/// Text remains black (inherited from theme_grey).
pub fn theme_dark_base(base_size: f64) -> Theme {
    Theme {
        panel_background: ElementRect {
            fill: Some((127, 127, 127)), // grey50
            color: None,
            width: 0.0,
            visible: true,
        },
        panel_grid_major: ElementLine {
            color: (107, 107, 107), // ~grey42
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        panel_grid_minor: ElementLine {
            color: (107, 107, 107), // ~grey42, thinner
            width: 0.25,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        axis_ticks: ElementLine {
            color: (51, 51, 51), // grey20
            width: 0.5,
            visible: true,
            linetype: crate::render::backend::Linetype::Solid,
        },
        strip_background: ElementRect {
            fill: Some((38, 38, 38)), // ~grey15
            color: None,
            width: 0.0,
            visible: true,
        },
        strip_text: ElementText {
            size: base_size * 0.8,
            color: (230, 230, 230), // grey90
            ..Default::default()
        },
        legend_key: ElementRect {
            fill: Some((127, 127, 127)), // grey50, matches panel
            color: None,
            width: 0.0,
            visible: true,
        },
        ..theme_gray_base(base_size)
    }
}

// ─── theme_void ──────────────────────────────────────────────────

/// Void theme: completely blank — no axes, ticks, gridlines, labels, or background.
/// Canvas for maps or custom visualizations. Legend is retained.
pub fn theme_void() -> Theme {
    theme_void_base(DEFAULT_BASE_SIZE)
}

/// Void theme with custom base font size.
pub fn theme_void_base(base_size: f64) -> Theme {
    let (title_size, _, axis_text_size) = text_sizes(base_size);
    Theme {
        text: ElementText::blank(),
        title: ElementText {
            size: title_size,
            ..Default::default()
        },
        axis_text_x: ElementText::blank(),
        axis_text_y: ElementText::blank(),
        axis_title_x: ElementText::blank(),
        axis_title_y: ElementText::blank(),
        axis_line: ElementLine::blank(),
        axis_ticks: ElementLine::blank(),
        panel_background: ElementRect::blank(),
        panel_grid_major: ElementLine::blank(),
        panel_grid_minor: ElementLine::blank(),
        plot_background: ElementRect::blank(),
        legend_position: LegendPosition::Right, // ggplot2 keeps legend in void
        plot_margin: Margin {
            top: 0.0,
            right: 0.0,
            bottom: 0.0,
            left: 0.0,
        },

        subtitle: ElementText::blank(),
        caption: ElementText::blank(),
        legend_title: ElementText {
            size: axis_text_size, // rel(0.8)
            ..Default::default()
        },
        legend_text: ElementText {
            size: axis_text_size, // rel(0.8)
            ..Default::default()
        },
        strip_text: ElementText {
            size: axis_text_size, // rel(0.8)
            ..Default::default()
        },

        axis_line_x: None,
        axis_line_y: None,
        axis_ticks_x: None,
        axis_ticks_y: None,
        panel_grid_major_x: None,
        panel_grid_major_y: None,
        panel_grid_minor_x: None,
        panel_grid_minor_y: None,

        panel_border: ElementLine::blank(),
        legend_background: ElementRect::blank(),
        legend_key: ElementRect::blank(),
        strip_background: ElementRect::blank(),

        axis_ticks_length: 0.0,
        axis_text_x_dodge: 1,
        legend_key_width: 12.0,
        legend_key_height: 18.0,
        legend_spacing: 4.0,
        legend_margin: Margin {
            top: 0.0,
            right: 0.0,
            bottom: 0.0,
            left: 0.0,
        },
        panel_spacing: 0.0,
        panel_spacing_x: None,
        panel_spacing_y: None,
        primary: None,
        aspect_ratio: None,
        panel_ontop: false,
        axis_minor_ticks: false,
        title_position: crate::theme::TitlePosition::Panel,
        tag_position: crate::theme::TagPosition::TopLeft,
        legend_direction: None,
    }
}