How a stat computes a layer's table before it is positioned and drawn: counting rows, binning them, summarising them, or anything a plugin's stat does, and the options it takes and the aesthetics it reads, needs, writes and refuses, which a call is checked against.
A stat is a struct whose module adopts this behaviour. Named in a geom function's stat:, as
Giggly.Plot.geom_point(plot, stat: MyPlugin.Scaled), it takes that function's options and its
own, and the plot runs it when it is assembled. Every fact is a function of the struct, so a fact
that depends on the stat's options reads them and one that does not ignores them.
compute_layer/3 is handed the layer's whole table, every panel and group in it: a column per
aesthetic the stat reads (read_aesthetics/1), each named by the aesthetic (:x, :y,
:group), and :panel, with the positions already mapped by their scales, so a discrete value
is its level's position, and every other aesthetic's column under its name as a string
("color"), which it may carry along or drop but never mistakes for a column of its own. It
returns the table the geom draws, keeping :panel and :group for each row, computed with
Giggly.DataFrame's functions, as Giggly's own stats compute theirs; each column it carried is
put back under its own name, unless it computed one of that name.
Writing a stat
use Giggly.Stat adopts the behaviour and gives every callback but compute_layer/3 a
default: no aesthetics accepted, needed, written, consumed, refused or required to be numbers, and
those accepted the ones read; no
default mapping; no computed variables; no exclusive groups; no orientation to check; no nudge
dropped; no way it runs recorded; not computing through the frame's operations alone; no
options, none in conflict, and a struct built from the
options options/0 names. A module states the facts its stat has and computes its table, so a
callback added to this behaviour later reaches it with its default:
defmodule MyPlugin.Scaled do
use Giggly.Stat
alias Giggly.DataFrame
defstruct [:by]
@impl Giggly.Stat
def options, do: [by: [type: :float, default: 2.0, doc: "How many times y."]]
@impl Giggly.Stat
def accepted_aesthetics(_stat), do: [:x, :y, :group]
@impl Giggly.Stat
def required_aesthetics(_stat), do: [:x, :y]
@impl Giggly.Stat
def compute_layer(stat, frame, _axes),
do: DataFrame.mutate(frame, y: {:*, [:y, {:constant, stat.by}]})
endGiggly's tests hold a whole stat written this way, test/support/scaled_stat.ex.
Summary
Types
Each panel's x and y axes, by panel.
One of a panel's axes as a stat reads it: its extent, unwidened, in transformed positions, or
:none where the axis has no scale before the stats run; whether it is discrete; whether it is
binned, its positions bin numbers; a function putting values in data units into transformed
positions, as a stat transforms breaks it is given; and one putting positions back into data
units, a discrete axis's as given.
What an orientation: given at the call must name, checked against the layer's mapping before
the stat: {:runs_along, verb}, the one of x and y mapped, verb what the stat does along it
("count"); :stands_along, the other of the one mapped where only one is; :none, nothing to
check.
A stat: the struct of a module adopting this behaviour, its fields the stat's parameters.
Callbacks
The aesthetics the stat reads as it computes, which a layer may map for it.
The layer's table frame after stat has computed on it, handed each panel's axes, by panel.
The columns the stat computes that a mapping's {:after_stat, ...} can read; an after_stat
naming any other is refused, the layer's own at its call and the plot's when the plot runs.
The computed column each aesthetic takes by default once the stat has run: %{y: :count} puts
the count in y; :none puts nothing in and titles the aesthetic's guide by its own name. Empty
for a stat that maps nothing by default. A value the layer maps for one of them replaces it.
The aesthetics the stat consumes as it computes, so one mapped after it reaches nothing.
Whether the stat drops a nudge mapped before it, so a layer nudging its marks refuses one its own mapping maps there and takes one mapped after the stat: true for a stat that keeps only the aesthetics it computes by, as counting at each place does.
Groups of aesthetics of which a layer maps at most one before the stat, as a stat that runs
along one of x and y and computes the other refuses both.
Whether the stat computes through Giggly.DataFrame's operations alone, never reading a column
of the table it is handed into lists, only columns of tables it has summarised from it, so the
table may be held in any frame that runs those operations. A stat that says not is handed its
table in Giggly's own frame.
The stat's struct from a call's validated options, reading only its own; the options of the geom and the layer are there too.
The aesthetics the stat reads that must hold numbers, each with why: a column of another type, or a constant, mapped before the stat is refused when the plot runs, giving the reason.
What a call's validated options say that the stat cannot take together, each a reason that
follows the function's name ("takes center: or boundary:, not both, ..."), [] for none. It
reads the options, not the struct, since a struct records a default where nothing was given.
The call raises the first.
The options that set the stat's fields, as a NimbleOptions schema written as data, each with
its type, its documentation and its default. A geom function given the stat in stat: takes
them beside its own, and validates the call against both; where both take a name, the stat's
definition of it stands.
What an orientation given at the call must name (orientation_rule/0).
The columns the stat reads as it computes. Every other column of the layer's table is handed to
compute_layer/3 under its name as a string, so a column named as one the stat computes, or
as one it works with along the way, is never taken for it; once the stat has run, each is put
back under its own name, unless the stat computed a column of that name, which stands in its
place.
Whether the stat's table records which way it runs, in a flipped_aes column, as counting
and binning do, so a geom drawing it runs that way whatever it would read on its own, and a
layer dodging its marks refuses dodging's orientation:, which the table's overrides.
The aesthetics the stat refuses mapped before it runs, by the layer's own mapping or the plot's, each with why. An after_stat reads the stat's output, so it is never refused here.
The aesthetics the stat cannot compute without, mapped before it runs: each a name, or a list of names of which one is enough.
The aesthetics the stat writes itself, over any value mapped before it, so its table holds them for the geom after it. A layer's own mapping may not map one of them before the stat unless the stat also reads it.
Functions
The layer's table frame computed on by stat's module's compute_layer/3.
Whether stat computes through the frame's operations alone, by its module's frame_operations?/1.
Types
Each panel's x and y axes, by panel.
@type axis() :: %{ limits: {number(), number()} | :none, discrete: boolean(), binned: boolean(), transform: (list() -> list()), inverse: (list() -> list()) }
One of a panel's axes as a stat reads it: its extent, unwidened, in transformed positions, or
:none where the axis has no scale before the stats run; whether it is discrete; whether it is
binned, its positions bin numbers; a function putting values in data units into transformed
positions, as a stat transforms breaks it is given; and one putting positions back into data
units, a discrete axis's as given.
@type orientation_rule() :: :none | {:runs_along, String.t()} | :stands_along
What an orientation: given at the call must name, checked against the layer's mapping before
the stat: {:runs_along, verb}, the one of x and y mapped, verb what the stat does along it
("count"); :stands_along, the other of the one mapped where only one is; :none, nothing to
check.
@type t() :: struct()
A stat: the struct of a module adopting this behaviour, its fields the stat's parameters.
Callbacks
The aesthetics the stat reads as it computes, which a layer may map for it.
@callback compute_layer(stat :: t(), Giggly.DataFrame.t(), axes()) :: Giggly.DataFrame.t()
The layer's table frame after stat has computed on it, handed each panel's axes, by panel.
The columns the stat computes that a mapping's {:after_stat, ...} can read; an after_stat
naming any other is refused, the layer's own at its call and the plot's when the plot runs.
The computed column each aesthetic takes by default once the stat has run: %{y: :count} puts
the count in y; :none puts nothing in and titles the aesthetic's guide by its own name. Empty
for a stat that maps nothing by default. A value the layer maps for one of them replaces it.
The aesthetics the stat consumes as it computes, so one mapped after it reaches nothing.
Whether the stat drops a nudge mapped before it, so a layer nudging its marks refuses one its own mapping maps there and takes one mapped after the stat: true for a stat that keeps only the aesthetics it computes by, as counting at each place does.
Groups of aesthetics of which a layer maps at most one before the stat, as a stat that runs
along one of x and y and computes the other refuses both.
Whether the stat computes through Giggly.DataFrame's operations alone, never reading a column
of the table it is handed into lists, only columns of tables it has summarised from it, so the
table may be held in any frame that runs those operations. A stat that says not is handed its
table in Giggly's own frame.
The stat's struct from a call's validated options, reading only its own; the options of the geom and the layer are there too.
The aesthetics the stat reads that must hold numbers, each with why: a column of another type, or a constant, mapped before the stat is refused when the plot runs, giving the reason.
What a call's validated options say that the stat cannot take together, each a reason that
follows the function's name ("takes center: or boundary:, not both, ..."), [] for none. It
reads the options, not the struct, since a struct records a default where nothing was given.
The call raises the first.
@callback options() :: keyword()
The options that set the stat's fields, as a NimbleOptions schema written as data, each with
its type, its documentation and its default. A geom function given the stat in stat: takes
them beside its own, and validates the call against both; where both take a name, the stat's
definition of it stands.
@callback orientation_rule(stat :: t()) :: orientation_rule()
What an orientation given at the call must name (orientation_rule/0).
The columns the stat reads as it computes. Every other column of the layer's table is handed to
compute_layer/3 under its name as a string, so a column named as one the stat computes, or
as one it works with along the way, is never taken for it; once the stat has run, each is put
back under its own name, unless the stat computed a column of that name, which stands in its
place.
Whether the stat's table records which way it runs, in a flipped_aes column, as counting
and binning do, so a geom drawing it runs that way whatever it would read on its own, and a
layer dodging its marks refuses dodging's orientation:, which the table's overrides.
The aesthetics the stat refuses mapped before it runs, by the layer's own mapping or the plot's, each with why. An after_stat reads the stat's output, so it is never refused here.
The aesthetics the stat cannot compute without, mapped before it runs: each a name, or a list of names of which one is enough.
The aesthetics the stat writes itself, over any value mapped before it, so its table holds them for the geom after it. A layer's own mapping may not map one of them before the stat unless the stat also reads it.
Functions
@spec compute_layer(t(), Giggly.DataFrame.t(), axes()) :: Giggly.DataFrame.t()
The layer's table frame computed on by stat's module's compute_layer/3.
Whether stat computes through the frame's operations alone, by its module's frame_operations?/1.