Documentation
¶
Overview ¶
draw.go contains geometry rendering functions for the ggplot rendering pipeline. Each draw function maps data coordinates to pixel positions using a coordinate system and renders shapes via the canvas abstraction.
The Drawer interface enables extensible geometry dispatch: third-party geoms can register their own draw functions via RegisterDrawer.
Package ggplot is a production-grade, pure-Go Grammar of Graphics plotting library.
Inspired by R's ggplot2, it provides a declarative, composable API for building statistical visualizations from data, aesthetics, geometries, scales, coordinate systems, facets, and themes.
Quick Start ¶
p := ggplot.New(ds,
aes.X("x"),
aes.Y("y"),
aes.Color("group"),
).
Layer(geom.Point(geom.WithSize(4), geom.WithAlpha(0.7))).
Layer(geom.Smooth(geom.WithMethod("lm"))).
Labs(ggplot.Title("My Plot"), ggplot.XLab("X Axis")).
Theme("minimal").
Save("output.png", 1200, 800)
Architecture ¶
The library follows a strict pipeline:
PlotSpec → Validate → Stat Transform → Scale Training → Layout → Render
All data flows through the dataset.Dataset abstraction. Multiple engine backends are supported: memory (Go slices), Apache Arrow (columnar arrays), and BigQuery (SQL pushdown). Arrow IPC and Parquet ingest provide zero-copy reads; constructing from Go slices requires one copy.
Index ¶
- func RegisterDrawer(t geom.Type, d Drawer)
- type DrawContext
- type Drawer
- type DrawerFunc
- type LabOpt
- type LegendPos
- type Plot
- func (p *Plot) Aes(mappings ...aes.Mapping) *Plot
- func (p *Plot) Coord(c coord.Coord) *Plot
- func (p *Plot) CoordFlip() *Plot
- func (p *Plot) FacetGrid(rowCol, colCol string) *Plot
- func (p *Plot) FacetWrap(col string, opts ...facet.WrapOpt) *Plot
- func (p *Plot) Labs(opts ...LabOpt) *Plot
- func (p *Plot) Layer(l geom.Layer, localAes ...aes.Mapping) *Plot
- func (p *Plot) LegendPosition(pos LegendPos) *Plot
- func (p *Plot) Render(ctx context.Context, width, height int) (*canvas.GGCanvas, error)
- func (p *Plot) Save(ctx context.Context, filename string, width, height int, opts ...RenderOpt) error
- func (p *Plot) ScaleColor(c colormap.Cmap) *Plot
- func (p *Plot) ScaleColorContinuous(c colormap.Cmap, n colormap.Norm) *Plot
- func (p *Plot) ScaleColorManual(m map[string]colormap.Color) *Plot
- func (p *Plot) ScaleFill(c colormap.Cmap) *Plot
- func (p *Plot) ScaleX(scaleType scale.Type) *Plot
- func (p *Plot) ScaleY(scaleType scale.Type) *Plot
- func (p *Plot) Theme(name theme.Name) *Plot
- func (p *Plot) WriteTo(ctx context.Context, w io.Writer, format string, width, height int, ...) (int64, error)
- func (p *Plot) XLim(min, max float64) *Plot
- func (p *Plot) YLim(min, max float64) *Plot
- type RenderOpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type DrawContext ¶
type DrawContext struct {
Canvas canvas.Canvas
Coord coord.Coord
Data dataset.Dataset
Mapping grammar.AesMap
Params geom.Params
Theme theme.Theme // active theme for default styling
ContColorCol string // continuous color column (empty if none)
ContScale *colormap.Scale // continuous color scale (nil if none)
W, H float64 // panel size in pixels
XMin, XMax float64 // data domain bounds
YMin, YMax float64 // data domain bounds
}
DrawContext holds the rendering parameters passed to a Drawer. It encapsulates the canvas, coordinate system, data, aesthetic mappings, and panel bounds so that Drawer implementations are self-contained.
type Drawer ¶
type Drawer interface {
Draw(ctx DrawContext)
}
Drawer renders a geometry type onto the canvas. Implementations are registered via RegisterDrawer and looked up by geom.Type during rendering.
type DrawerFunc ¶
type DrawerFunc func(DrawContext)
DrawerFunc is an adapter to allow use of ordinary functions as [Drawer]s.
type Plot ¶
type Plot struct {
// contains filtered or unexported fields
}
Plot is the immutable, declarative plot builder. Every method returns a new Plot with the modification applied, enabling a fluent chaining style.
Plot is safe to share and reuse — modifying a derived plot does not affect the original.
func (*Plot) CoordFlip ¶
CoordFlip swaps the x and y axes. This is sugar for setting geom.Horizontal orientation on all layers and swapping the axis labels.
func (*Plot) Layer ¶
Layer adds a geometry layer to the plot with optional per-layer aesthetic overrides.
func (*Plot) LegendPosition ¶
LegendPosition sets the legend placement.
func (*Plot) Save ¶
func (p *Plot) Save(ctx context.Context, filename string, width, height int, opts ...RenderOpt) error
Save renders the plot to a file at the given dimensions. The output format is inferred from the file extension:
.png — raster PNG (default) .svg — SVG 1.1 vector .pdf — PDF 1.4 vector
Options: WithScale for HiDPI output.
func (*Plot) ScaleColor ¶
ScaleColor configures the color aesthetic to use the given colormap. The cmap is composed with a default LinearNorm for continuous data, or used as a Listed palette for discrete data. Pass nil to clear an existing override and fall back to defaults.
func (*Plot) ScaleColorContinuous ¶
ScaleColorContinuous installs an explicit continuous color scale composed of the given Cmap and Norm. Use this to control LogNorm / TwoSlopeNorm / PowerNorm / data-range limits beyond the simple Plot.ScaleColor form.
func (*Plot) ScaleColorManual ¶
ScaleColorManual maps category labels to specific colors. Categories not in m fall back to the default Tab10 palette in the order they are encountered during dataset training.
func (*Plot) ScaleFill ¶
ScaleFill is the fill-aesthetic counterpart of Plot.ScaleColor.
func (*Plot) WriteTo ¶
func (p *Plot) WriteTo(ctx context.Context, w io.Writer, format string, width, height int, opts ...RenderOpt) (int64, error)
WriteTo renders the plot and writes the output to w in the given format. Supported formats: "png" (default), "svg", "pdf". Options: WithScale for HiDPI output. Returns the number of bytes written.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package aes provides aesthetic mapping constructors for the Grammar of Graphics.
|
Package aes provides aesthetic mapping constructors for the Grammar of Graphics. |
|
Package colormap provides a matplotlib-style colormap and color-scale API for the ggplot grammar of graphics pipeline.
|
Package colormap provides a matplotlib-style colormap and color-scale API for the ggplot grammar of graphics pipeline. |
|
Package coord defines coordinate systems that control how data positions are mapped to the 2D plotting surface.
|
Package coord defines coordinate systems that control how data positions are mapped to the 2D plotting surface. |
|
Package dataset provides columnar data abstractions for the Grammar of Graphics pipeline.
|
Package dataset provides columnar data abstractions for the Grammar of Graphics pipeline. |
|
arrow
Package arrow provides an Apache Arrow-backed compute engine for the dataset package.
|
Package arrow provides an Apache Arrow-backed compute engine for the dataset package. |
|
bigquery
Package bigquery implements a BigQuery SQL pushdown engine for the dataset library.
|
Package bigquery implements a BigQuery SQL pushdown engine for the dataset library. |
|
compute
Package compute provides portable SIMD primitives for the dataset engines.
|
Package compute provides portable SIMD primitives for the dataset engines. |
|
csv
Package csv provides CSV reading and writing for the dataset package.
|
Package csv provides CSV reading and writing for the dataset package. |
|
math
Package math provides SIMD-accelerated mathematical transforms for the dataset engines.
|
Package math provides SIMD-accelerated mathematical transforms for the dataset engines. |
|
memory
Package memory provides a lightweight Go-slice-backed compute engine for the dataset package.
|
Package memory provides a lightweight Go-slice-backed compute engine for the dataset package. |
|
parquet
Package parquet provides Parquet reading and writing for the dataset package.
|
Package parquet provides Parquet reading and writing for the dataset package. |
|
sort
Package sort provides SIMD-accelerated sorting for the dataset engines.
|
Package sort provides SIMD-accelerated sorting for the dataset engines. |
|
examples
|
|
|
annotations
command
Example: Reference lines and text annotations.
|
Example: Reference lines and text annotations. |
|
butterfly
command
Example: Butterfly curve with continuous color gradient.
|
Example: Butterfly curve with continuous color gradient. |
|
categorical
command
Example: Boxplot and Categorical (Discrete) X Axis
|
Example: Boxplot and Categorical (Discrete) X Axis |
|
clifford
command
Example: (Clifford[https://paulbourke.net/fractals/clifford/] attractor with continuous color gradient.
|
Example: (Clifford[https://paulbourke.net/fractals/clifford/] attractor with continuous color gradient. |
|
color_mapping
command
Example: Multi-Group Scatter with Colour Mapping and Legend
|
Example: Multi-Group Scatter with Colour Mapping and Legend |
|
coord_flip
command
Example: Orientation — every geometry flipped to horizontal.
|
Example: Orientation — every geometry flipped to horizontal. |
|
geometries/area
command
|
|
|
geometries/bar
command
|
|
|
geometries/histogram
command
|
|
|
geometries/line
command
|
|
|
geometries/point
command
|
|
|
geometries/smooth
command
|
|
|
multiline
command
Example: Multi-Line Plot
|
Example: Multi-Line Plot |
|
phase2_features
command
Phase 2: Coordinates, Faceting, Themes, Guides, Aesthetics, LegendPosition
|
Phase 2: Coordinates, Faceting, Themes, Guides, Aesthetics, LegendPosition |
|
phase2_geometries
command
Phase 2: Geometries — Point, Line, Step, Bar, Histogram, Area, Density, Rug, HLine, VLine, Text, BoxPlot, Smooth
|
Phase 2: Geometries — Point, Line, Step, Bar, Histogram, Area, Density, Rug, HLine, VLine, Text, BoxPlot, Smooth |
|
phase2_scales
command
Phase 2: Scales — Linear, Log10, Sqrt, Reverse, Discrete
|
Phase 2: Scales — Linear, Log10, Sqrt, Reverse, Discrete |
|
phase2_statistics
command
Phase 2: Statistics — Identity, Bin/Count, Density (KDE), Smooth (LOESS), Summary, BoxPlot
|
Phase 2: Statistics — Identity, Bin/Count, Density (KDE), Smooth (LOESS), Summary, BoxPlot |
|
showcase
command
Example: Feature Showcase
|
Example: Feature Showcase |
|
themes
command
Example: Theme Showcase
|
Example: Theme Showcase |
|
Package facet splits a dataset into subsets for "small multiple" panel layouts.
|
Package facet splits a dataset into subsets for "small multiple" panel layouts. |
|
Package geom provides geometry specifications for the Grammar of Graphics.
|
Package geom provides geometry specifications for the Grammar of Graphics. |
|
Package guide provides axis, legend, and title rendering for plots.
|
Package guide provides axis, legend, and title rendering for plots. |
|
internal
|
|
|
canvas
Package canvas defines the rendering backend abstraction.
|
Package canvas defines the rendering backend abstraction. |
|
fonts
Package fonts provides a cross-platform, pure-Go typography subsystem.
|
Package fonts provides a cross-platform, pure-Go typography subsystem. |
|
Package output provides rendering output abstractions for exporting and displaying plots.
|
Package output provides rendering output abstractions for exporting and displaying plots. |
|
Package position defines position adjustments that control how overlapping geometries are arranged.
|
Package position defines position adjustments that control how overlapping geometries are arranged. |
|
Package scale provides scale transformations that map data values to visual aesthetic values.
|
Package scale provides scale transformations that map data values to visual aesthetic values. |
|
Package stat provides statistical transformations for the Grammar of Graphics.
|
Package stat provides statistical transformations for the Grammar of Graphics. |
|
Package theme provides visual styling configurations for plots.
|
Package theme provides visual styling configurations for plots. |








