facet

package
v0.0.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package facet splits a dataset into subsets for "small multiple" panel layouts. Faceting is a core component of the Grammar of Graphics, allowing the same plot specification to be repeated across levels of a categorical variable.

Index

Constants

View Source
const MarginLabel = "All"

MarginLabel is the display value used for aggregate margin panels.

Variables

View Source
var ErrFacetConfig = errors.New("facet: invalid configuration")

ErrFacetConfig is returned for invalid facet configuration.

Functions

This section is empty.

Types

type Facet

type Facet interface {
	// Split partitions the dataset into panels. Each panel has a label
	// and a filtered subset of the data.
	Split(ctx context.Context, ds dataset.Dataset) ([]Panel, error)

	// GridDims returns the (rows, cols) grid dimensions for layout.
	// For Wrap, this is computed from the number of panels and nCols.
	GridDims(nPanels int) (rows, cols int)

	// FreeScales returns whether X and Y scales are independent per panel.
	// When false (default), all panels share unified scale bounds.
	FreeScales() (freeX, freeY bool)

	// SpaceMode returns the space allocation mode for panels:
	//   "fixed"  — all panels have equal size (default)
	//   "free"   — panel sizes proportional to data range (both axes)
	//   "free_x" — column widths proportional to X range
	//   "free_y" — row heights proportional to Y range
	SpaceMode() string

	// StripPositions returns the strip label placement for column and row strips.
	// col: "top" (default) or "bottom"
	// row: "right" (default) or "left"
	StripPositions() (col, row string)

	// String returns a human-readable description.
	String() string
}

Facet defines how a dataset is split into panels for small-multiple layouts.

func Grid

func Grid(rowCol, colCol string, opts ...GridOpt) Facet

Grid creates a facet that arranges panels in a row × col matrix, splitting by one variable for rows and another for columns.

func None

func None() Facet

None returns a no-op facet that produces a single panel.

func Wrap

func Wrap(col string, opts ...WrapOpt) Facet

Wrap creates a facet that wraps panels across a grid layout, splitting the data by the given column's distinct values.

type GridOpt added in v0.0.9

type GridOpt func(*gridFacet)

GridOpt configures a Grid facet.

func GridDrop added in v0.0.9

func GridDrop(drop bool) GridOpt

GridDrop controls whether empty panels are omitted (default: true).

func GridFreeX added in v0.0.9

func GridFreeX() GridOpt

GridFreeX makes each column's X scale independent.

func GridFreeY added in v0.0.9

func GridFreeY() GridOpt

GridFreeY makes each row's Y scale independent.

func GridLabeller added in v0.0.9

func GridLabeller(l Labeller) GridOpt

GridLabeller sets the label formatter for Grid facets.

func GridMargins added in v0.0.9

func GridMargins(margins bool) GridOpt

GridMargins enables aggregate margin panels (default: false). When true, extra panels are added that aggregate across row/column values.

func GridSpace added in v0.0.9

func GridSpace(mode string) GridOpt

GridSpace sets the space allocation mode for grid panels. Valid modes: "fixed" (default), "free", "free_x", "free_y". "free" modes make panel sizes proportional to their data range. Requires the corresponding free scale to be meaningful.

func GridStripBottom added in v0.0.9

func GridStripBottom() GridOpt

GridStripBottom places column strip labels at the bottom of the grid.

func GridStripLeft added in v0.0.9

func GridStripLeft() GridOpt

GridStripLeft places row strip labels at the left of the grid.

type Labeller added in v0.0.9

type Labeller func(variable, value string) string

Labeller formats facet panel strip labels. It receives the faceting variable name and the panel's raw value, returning the display string.

func Label added in v0.0.9

func Label(fn func(variable, value string) string) Labeller

Label returns a custom labeller from a user-supplied function.

func LabelBoth added in v0.0.9

func LabelBoth() Labeller

LabelBoth returns a labeller that shows "variable: value".

"species: setosa", "species: versicolor"

func LabelContext added in v0.0.9

func LabelContext() Labeller

LabelContext returns a labeller that shows "variable: value" for Grid facets (where context is ambiguous) and just "value" for Wrap facets. The choice is made internally by the facet type at Split time.

func LabelValue added in v0.0.9

func LabelValue() Labeller

LabelValue returns a labeller that shows only the value. This is the default labeller.

"setosa", "versicolor", "virginica"

type Panel

type Panel struct {
	Label    string // formatted display label
	RowVal   string // raw row facet value (Grid only, "" for Wrap)
	ColVal   string // raw column facet value (Grid only, "" for Wrap)
	Dataset  dataset.Dataset
	NumRows  int  // number of rows matching this panel's filter
	IsMargin bool // true for aggregate margin panels
}

Panel represents a single facet panel containing a data subset.

type WrapOpt

type WrapOpt func(*wrapFacet)

WrapOpt configures a Wrap facet.

func FreeX added in v0.0.9

func FreeX() WrapOpt

FreeX makes each panel's X scale independent.

func FreeXY added in v0.0.9

func FreeXY() WrapOpt

FreeXY makes both X and Y scales independent per panel.

func FreeY added in v0.0.9

func FreeY() WrapOpt

FreeY makes each panel's Y scale independent.

func NCols

func NCols(n int) WrapOpt

NCols sets the number of columns for the wrapped layout.

func NRows

func NRows(n int) WrapOpt

NRows sets the number of rows for the wrapped layout.

func StripBottom added in v0.0.9

func StripBottom() WrapOpt

StripBottom places wrap strip labels at the bottom of each panel.

func WithDrop added in v0.0.9

func WithDrop(drop bool) WrapOpt

WithDrop controls whether empty panels are omitted (default: true). When false, panels with zero rows are emitted for all distinct values.

func WithLabeller added in v0.0.9

func WithLabeller(l Labeller) WrapOpt

WithLabeller sets the label formatter for Wrap facets.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL