geom

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package geom provides geometry specifications for the Grammar of Graphics. Geometries define how data is visually represented — as points, lines, bars, areas, etc. Each geom is a pure declarative specification; it holds no rendering logic. The rendering pipeline reads the spec and dispatches drawing operations to the [canvas.Canvas] backend.

Every geom constructor returns a Layer that can be added to a plot:

p := ggplot.New(ds, aes.X("x"), aes.Y("y")).
    Layer(geom.Point()).
    Layer(geom.Line())

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterGeomType

func RegisterGeomType(t Type, relevantOpts OptFlag)

RegisterGeomType registers a custom geometry type with its relevant option flags. This makes geom.Type open: third-party packages can define new geometry types that participate in option validation via Layer.Validate.

Combined with [ggplot.RegisterDrawer], this enables fully custom geoms:

// In your package init():
const TypeViolin geom.Type = "violin"
geom.RegisterGeomType(TypeViolin, geom.OptColor|geom.OptFill|geom.OptAlpha|geom.OptWidth)
ggplot.RegisterDrawer(TypeViolin, myViolinDrawer)

Types

type Layer

type Layer struct {
	Geom     Type              // geometry type (point, line, bar, etc.)
	StatName stat.Name         // stat name (stat.Identity, stat.Bin, stat.Count, etc.)
	Position position.Pos      // position adjustment
	Params   Params            // visual parameters specific to this geometry
	Mapping  map[string]string // per-layer aesthetic overrides (channel → column)
	// contains filtered or unexported fields
}

Layer represents a declarative layer specification produced by a geom constructor. It carries the geometry type, optional stat/position overrides, per-layer aesthetic mappings, and visual parameters.

func ABLine added in v0.0.2

func ABLine(opts ...Opt) Layer

ABLine creates a reference line defined by y = slope*x + intercept. Use WithIntercept for the y-intercept and WithSlope for the slope.

Example:

geom.ABLine(geom.WithIntercept(0), geom.WithSlope(1.5), geom.WithColor("#9B59B6"))

Relevant options: WithIntercept, WithSlope, WithColor, WithAlpha, WithLineWidth, WithLabel.

func Area

func Area(opts ...Opt) Layer

Area creates a filled area geometry layer.

Relevant options: WithColor, WithFill, WithAlpha, WithLineWidth.

func Bar

func Bar(opts ...Opt) Layer

Bar creates a bar chart geometry layer. Default stat: count. Default position: stack.

Relevant options: WithColor, WithFill, WithAlpha, WithWidth, WithLineWidth.

func Boxplot

func Boxplot(opts ...Opt) Layer

Boxplot creates a box-and-whisker geometry layer. It uses stat "boxplot" which computes the five-number summary (min, Q1, median, Q3, max) for each unique X group.

Relevant options: WithColor, WithFill, WithAlpha, WithWidth, WithLineWidth.

func Col

func Col(opts ...Opt) Layer

Col creates a column geometry layer that uses raw Y values (stat: identity). This is equivalent to ggplot2's geom_col(). Use Bar when you want automatic counting/aggregation, and Col when you have pre-computed values.

Default stat: identity. Default position: stack.

Relevant options: WithColor, WithFill, WithAlpha, WithWidth, WithLineWidth.

func Density

func Density(opts ...Opt) Layer

Density creates a kernel density estimation geometry layer.

Relevant options: WithColor, WithFill, WithAlpha, WithLineWidth, WithPoints.

func HLine

func HLine(opts ...Opt) Layer

HLine creates a horizontal reference line at the given Y intercept.

Example:

geom.HLine(geom.WithIntercept(0), geom.WithColor("#CC0000"))

Relevant options: WithIntercept, WithColor, WithAlpha, WithLineWidth, WithLabel.

func Histogram

func Histogram(opts ...Opt) Layer

Histogram creates a binned histogram geometry layer. Default stat: bin. Default position: stack.

Relevant options: WithColor, WithFill, WithAlpha, WithWidth, WithBins, WithLineWidth.

func Line

func Line(opts ...Opt) Layer

Line creates a connected line geometry layer.

Relevant options: WithColor, WithAlpha, WithLineWidth, WithSize.

func Point

func Point(opts ...Opt) Layer

Point creates a scatter point geometry layer. Default stat: identity. Default position: identity.

Relevant options: WithColor, WithFill, WithAlpha, WithSize, WithShape.

func Polygon

func Polygon(opts ...Opt) Layer

Polygon creates a closed polygon geometry layer.

Relevant options: WithColor, WithFill, WithAlpha, WithLineWidth.

func Rug

func Rug(opts ...Opt) Layer

Rug creates a rug (marginal tick) geometry layer.

Relevant options: WithColor, WithAlpha, WithLineWidth.

func Smooth

func Smooth(opts ...Opt) Layer

Smooth creates a smoothed trendline geometry layer. Default stat: smooth. Default method: lm.

Relevant options: WithColor, WithAlpha, WithLineWidth, WithSize, WithMethod, WithSpan, WithPoints.

func Step

func Step(opts ...Opt) Layer

Step creates a step-function line geometry layer.

Relevant options: WithColor, WithAlpha, WithLineWidth, WithSize.

func Text

func Text(opts ...Opt) Layer

Text creates a text label geometry layer.

Relevant options: WithColor, WithAlpha, WithFontSize, WithFontFamily, WithAngle.

func VLine

func VLine(opts ...Opt) Layer

VLine creates a vertical reference line at the given X intercept.

Example:

geom.VLine(geom.WithIntercept(5), geom.WithColor("#006600"))

Relevant options: WithIntercept, WithColor, WithAlpha, WithLineWidth, WithLabel.

func (*Layer) Validate

func (l *Layer) Validate() []string

Validate checks if the configured params are meaningful for this geometry type and returns a list of warning messages for irrelevant options.

Example:

layer := geom.Point(geom.WithBins(30))  // bins are for histograms, not points
warnings := layer.Validate()
// warnings = ["geom_point: WithBins has no effect (relevant for: histogram)"]

func (*Layer) Warnings

func (l *Layer) Warnings() []string

Warnings returns any validation warnings generated during construction.

type Opt

type Opt func(*Layer)

Opt is a functional option for configuring geometry layers.

func WithAlpha

func WithAlpha(a float64) Opt

WithAlpha sets the opacity.

func WithAngle

func WithAngle(deg float64) Opt

WithAngle sets the text rotation angle in degrees.

func WithBins

func WithBins(n int) Opt

WithBins sets the number of bins for histograms.

func WithColor

func WithColor(hex string) Opt

WithColor sets a fixed color override.

func WithFill

func WithFill(hex string) Opt

WithFill sets a fixed fill color override.

func WithFontFamily

func WithFontFamily(family string) Opt

WithFontFamily sets the text font family.

func WithFontSize

func WithFontSize(size float64) Opt

WithFontSize sets the text font size.

func WithGap

func WithGap(g float64) Opt

WithGap sets the gap between bars as a fraction [0, 1]. 0 means bars touch (no gap), 1 means 100% gap (bars invisible). The bar width is derived as 1 - gap. Default gap is 0.2.

func WithIntercept

func WithIntercept(v float64) Opt

WithIntercept sets the intercept value for reference lines. For HLine, this is the Y value. For VLine, this is the X value.

func WithLabel

func WithLabel(name string) Opt

WithLabel sets a legend label for this layer. When used together with WithColor, a legend entry is generated even without aes.Color grouping. This is the idiomatic way to add legends in wide-format multi-layer plots.

func WithLineWidth

func WithLineWidth(w float64) Opt

WithLineWidth sets the stroke width.

func WithMethod

func WithMethod(m string) Opt

WithMethod sets the smoothing method.

func WithNotch

func WithNotch(enabled bool) Opt

WithNotch enables notched boxplots that show the 95% confidence interval around the median.

func WithOrientation

func WithOrientation(o Orientation) Opt

WithOrientation sets the axis extension direction for directional geoms. Horizontal makes bars grow rightward, boxplots lay sideways, etc.

func WithPoints

func WithPoints(n int) Opt

WithPoints sets the interpolation point count.

func WithPosition

func WithPosition(p position.Pos) Opt

WithPosition sets the position adjustment for this layer.

func WithShape

func WithShape(shape string) Opt

WithShape sets the point shape.

func WithSize

func WithSize(s float64) Opt

WithSize sets the point radius. Use WithLineWidth for stroke width.

func WithSlope added in v0.0.2

func WithSlope(s float64) Opt

WithSlope sets the slope for ABLine. The line equation is y = slope*x + intercept.

func WithSpan

func WithSpan(s float64) Opt

WithSpan sets the loess smoothing span.

func WithStat

func WithStat(name stat.Name) Opt

WithStat sets the statistical transform for this layer.

func WithWhisker

func WithWhisker(rule string) Opt

WithWhisker sets the boxplot whisker rule: "tukey" (1.5×IQR, default) or "range" (min-max).

func WithWidth

func WithWidth(w float64) Opt

WithWidth sets the relative bar width [0, 1].

type OptFlag

type OptFlag uint32

OptFlag tracks which parameters were explicitly set by the user. Exported so third-party packages can compose relevance masks for RegisterGeomType.

const (
	OptColor       OptFlag = 1 << iota // common
	OptFill                            // common
	OptAlpha                           // common
	OptLineWidth                       // common
	OptSize                            // point, (also sets LineWidth)
	OptShape                           // point
	OptWidth                           // bar, histogram
	OptBins                            // histogram
	OptFontSize                        // text
	OptFontFamily                      // text
	OptAngle                           // text
	OptMethod                          // smooth
	OptSpan                            // smooth
	OptPoints                          // smooth, density
	OptWhisker                         // boxplot
	OptNotch                           // boxplot
	OptOrientation                     // bar, histogram, boxplot, area, density, rug
)

OptColor tracks whether WithColor was set.

type Orientation

type Orientation string

Orientation controls which axis a directional geom extends along.

const (
	Vertical   Orientation = "v" // default: bars grow upward, boxplots are vertical
	Horizontal Orientation = "h" // bars grow rightward, boxplots are horizontal
)

Vertical is the default orientation: bars grow upward.

type Params

type Params struct {
	// Common
	Color     string  // hex color override (e.g., "#4C72B0")
	Fill      string  // hex fill color override
	Alpha     float64 // opacity [0, 1]
	LineWidth float64 // stroke width in pixels

	// Point-specific
	Size  float64 // point radius in pixels (default = 3)
	Shape string  // "circle", "square", "triangle", "diamond" (default = "circle")

	// Bar/Histogram-specific
	Width float64 // relative bar width [0, 1] (default = 0.8)
	Gap   float64 // gap between bars [0, 1] (0 = touching, 1 = invisible; default = 0.2)
	Bins  int     // number of bins (histogram, default = 30)

	// Text-specific
	FontSize   float64 // text font size in points
	FontFamily string  // font family name
	Angle      float64 // rotation angle in degrees

	// Smooth-specific
	Method string  // "lm", "loess"
	Span   float64 // loess span
	Points int     // number of interpolation points

	// Boxplot-specific
	Whisker string // whisker rule: "tukey" (default, 1.5×IQR), "range" (min-max)
	Notch   bool   // if true, compute notch confidence interval around median

	// Orientation
	Orientation Orientation // "v" (default) or "h" — controls axis extension direction

	// Legend
	Label string // legend label for this layer (used with manual colors)

	// Reference lines
	Intercept float64 // y-intercept (hline) or x-intercept (vline)
	Slope     float64 // slope for abline (y = slope*x + intercept)
}

Params holds visual parameters for geometries. Not all fields apply to every geometry type; unused fields are ignored during rendering but Layer.Validate will emit warnings for irrelevant options.

type Type

type Type string

Type identifies the kind of geometry.

const (
	TypePoint     Type = "point"
	TypeLine      Type = "line"
	TypeBar       Type = "bar"
	TypeHistogram Type = "histogram"
	TypeArea      Type = "area"
	TypePolygon   Type = "polygon"
	TypeSmooth    Type = "smooth"
	TypeText      Type = "text"
	TypeBoxPlot   Type = "boxplot"
	TypeErrorBar  Type = "errorbar"
	TypeDensity   Type = "density"
	TypeTile      Type = "tile"
	TypeRug       Type = "rug"
	TypeSegment   Type = "segment"
	TypeStep      Type = "step"
	TypeHLine     Type = "hline"
	TypeVLine     Type = "vline"
	TypeABLine    Type = "abline"
)

TypePoint identifies a scatter point geometry.

Jump to

Keyboard shortcuts

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