facet

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package facet splits one chart into small multiples.

A facet takes the layers of a plot, cuts their rows by the values of a column, and hands each group its own panel. Every panel is drawn with the same geoms and — unless told otherwise — the same scales, which is the whole point: small multiples work because the only thing that varies between panels is the data.

p.Facet(facet.Wrap("region", facet.Columns(3)))

Wrap flows panels left to right and wraps; Grid crosses two columns, one down the rows and one across the columns. Either way the panels' axes are aligned by the layout solver, so a position in one panel means the same as the same position in the next.

Scales

Shared by default. FreeX, FreeY and Free give each panel a scale of its own, which answers a different question — "what is the shape of this group" rather than "how do these groups compare" — and should be a deliberate choice, because a reader who does not notice the axes changed will read the panels as comparable when they are not.

Layers without data

A layer that holds no rows — an annotation, a reference line — cannot be split, so it is drawn in every panel. That is what you want: a threshold applies to all of them.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoColumn = errors.New("figure/facet: column not found in any layer")

ErrNoColumn reports a facet column that none of the layers has.

Functions

This section is empty.

Types

type Desc

type Desc struct {
	// Row and Col name the columns split on. A wrapped facet leaves Row empty
	// and flows its panels; a grid crosses the two.
	Row, Col string
	// Wrap reports a wrapped facet rather than a grid.
	Wrap bool
	// Columns caps how many panels a wrap puts in a row. Zero means the
	// derived, roughly square default.
	Columns int
	// FreeX and FreeY give each panel its own axis.
	FreeX, FreeY bool
}

Desc is a facet specification reduced to what configures it, so that a faceted chart can be written down and read back.

type Option

type Option func(*Spec)

Option configures a facet.

func Columns

func Columns(n int) Option

Columns caps how many panels a Wrap puts in a row. The default is derived from the panel count, aiming for a roughly square grid.

func Free

func Free() Option

Free gives each panel its own scales on both axes.

func FreeX

func FreeX() Option

FreeX gives each panel its own horizontal scale.

func FreeY

func FreeY() Option

FreeY gives each panel its own vertical scale.

type Panel

type Panel struct {
	// Row and Col place the panel in the grid.
	Row, Col int
	// Strip is the label above the panel.
	Strip string
	// RightStrip is the label beside it, used by [Grid] to name a row once
	// rather than on every panel in it.
	RightStrip string
	// Layers are the plot's layers restricted to this panel's rows. Layers
	// that hold no data are carried through unchanged.
	Layers []geom.Geom
}

Panel is one facet: where it sits, what it is called, and the layers cut down to its rows.

type Spec

type Spec struct {
	// contains filtered or unexported fields
}

Spec describes how a plot is split into panels.

func FromDesc

func FromDesc(d Desc) *Spec

FromDesc builds the facet d describes. It returns nil for a Desc naming no column, which is how "this chart is not faceted" is written down.

func Grid

func Grid(rowCol, colCol string, opts ...Option) *Spec

Grid crosses two columns: rowCol runs down the rows, colCol across the columns. A panel exists for every combination that has rows; the rest are holes, which is itself information.

func Wrap

func Wrap(col string, opts ...Option) *Spec

Wrap splits by one column, flowing panels left to right and wrapping onto the next row.

func (*Spec) Describe

func (s *Spec) Describe() Desc

Describe returns the spec's configuration.

func (*Spec) FreeScales

func (s *Spec) FreeScales() (x, y bool)

FreeScales reports whether each panel gets its own X or Y scale.

func (*Spec) Split

func (s *Spec) Split(layers []geom.Geom) (panels []Panel, rows, cols int, err error)

Split cuts layers into panels and reports the shape of the grid.

Jump to

Keyboard shortcuts

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