Faceting

Faceting draws the same layers once per panel. It is a property of the chart, not of any mark, so every recipe on this site can be faceted by adding one call.

Grid faceting (two dimensions)

Arrange panels by two categorical columns — here vs across and am down.

//! Grid faceting: arrange panels by two categorical dimensions.
//!
//! The built-in `mtcars` dataset contains observations for all four `vs x am`
//! combinations. Each panel therefore has real vehicle observations instead
//! of empty combinations produced by sparse categorical fields.

use charton::prelude::*;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let ds = load_dataset("mtcars")?;

    Chart::build(ds)?
        .mark_point()?
        .configure_point(|point| point.with_size(4.0).with_opacity(0.85))
        .encode((
            alt::x("wt"),
            alt::y("mpg"),
            alt::color("gear").with_scale(Scale::Discrete),
        ))?
        // vs: engine shape (0 = V-shaped, 1 = straight)
        // am: transmission (0 = automatic, 1 = manual)
        .facet(FacetSpec::grid("vs", "am").with_strategy("fixed"))
        .with_size(800, 500)
        .with_left_margin(0.0)
        .with_top_margin(0.0)
        .with_bottom_margin(0.0)
        .save("docs/src/images/facet_grid.svg")?;

    println!("Saved docs/src/images/facet_grid.svg");
    Ok(())
}

Wrap faceting (one dimension)

A single categorical column wrapped into a grid. See the faceted violin for a complete example, or tests/test_facet_wrap.rs for the unemployment-by-country recipe.

Free scales

By default every panel shares one scale (the point of faceting: panels are comparable). FacetSpec::wrap(...).with_strategy("free") (and free_x / free_y) lets each panel train its own, for panels whose ranges differ wildly.

See also