Gadfly transforms a plot specification into a Compose scene graph consisting of guides (e.g., axis ticks, color keys) and one or more layers of geometry.
Layer Specifications
Each layer in a plot is defined by:
- Data source: The dataset used (e.g.,
dataset("ggplot2", "diamonds")). - Geometry: The visual representation (e.g.,
Geom.bar, Geom.point). - Mappings: Associating data elements with aesthetics (e.g.,
x = :Price, color = :Cut). - Statistics: Optional layer-wise transformations (e.g.,
Stat.histogram).
Shared Plot Elements
All layers in a single plot share:
- Coordinates: The coordinate system (e.g.,
Coord.cartesian, Coord.polar). - Scales: How data values map to aesthetics (e.g.,
Scale.x_continuous, Scale.color_discrete). - Statistics: Plot-wide transformations applied to all layers.
- Guides: Visual aids like
Guide.xticks, Guide.yticks, or Guide.colorkey.
The Rendering Process
- Data Mapping: Subsets of the data source are mapped to a
Data object (e.g., mapping :Price to the :x field). - Scale Application: Scales transform data into plottable aesthetics (e.g., mapping categorical strings to specific colors).
- Statistical Transformation: Layer-wise and plot-wise statistics are applied (e.g.,
Stat.histogram transforming x values into bin positions and calculating y counts). - Coordinate Transformation: A Compose context is created using the coordinate system to map data to screen coordinates.
- Geometry Rendering: Each layer renders its specific geometry.
- Guide Rendering: Guides are computed and rendered on top of the plot context.
# A minimal specification where many elements are inferred or defaulted
p = plot(df,
x = :Price, color = :Cut,
Stat.histogram,
Geom.bar)
# A full specification explicitly defining all components
p = plot(layer(df,
x = :Price, color = :Cut,
Stat.histogram,
Geom.bar),
Scale.x_continuous,
Scale.color_discrete,
Coord.cartesian,
Guide.xticks, Guide.yticks,
Guide.xlabel("Price"),
Guide.colorkey(title="Cut"))