Skip to main content
A stat summarizes a layer’s data before it is drawn. Instead of plotting raw rows, the layer plots a computed result: a count, a mean, or a fitted regression line. A stat is set per layer through the geom’s stat option. A layer that names none runs its geom’s default stat, which is identity (draw the data as-is) for every built-in geom.

Available stats

count, sum and summary group by x plus the layer’s grouping variables, so the groups a reader compares stay separate. mean reduces the layer to one observation, which is why it pairs with geom.rule() rather than a bar or a line. count computes y for you; mapping y alongside it is a CONFLICTING_STAT_MAPPING error. sum, mean, summary and smooth need a numeric y; a non-numeric one is an INCOMPATIBLE_TYPE error. Every stat also has a string shorthand, so geom.bar({ stat: 'count' }) is the same as geom.bar({ stat: stat.count() }). The names are 'identity', 'count', 'smooth', 'mean', 'sum' and 'summary'.

Trendlines with smooth

The smooth stat fits a regression through a layer’s points. Add it to a second geom.line() layer over the raw data:
method selects the regression family:
'linear' | 'loess' | 'exponential' | 'logarithmic' | 'quadratic' | 'power' | 'polynomial'
default:"linear"
The regression method to fit. The object builder asks for it; the 'smooth' string shorthand takes the default.
number
default:"3"
Polynomial order. Only used when method: 'polynomial'.
number
default:"0.3"
Smoothing bandwidth. Only used when method: 'loess'.

A mean reference line

stat.mean() reduces the layer to one observation, which is what a rule needs. Pair the two to draw a horizontal line at the layer’s average:

Estimates and intervals with summary

The summary stat reduces y to one observation per x-value and group: an estimate, and an interval around it if you ask for one. The estimate is mapped to y and the interval’s ends to lower and upper, which is what a geom drawing an interval reads.
'mean' | 'median'
default:"mean"
The value each x-value and group reduces to.
'stderr' | 'stdev' | 'ci' | 'iqr' | 'range'
The interval around the estimate. It has no default: leave it out and the stat computes the estimate alone, so a chart that draws an interval always says which one it is.
  • 'stderr': the mean ± mult standard errors
  • 'stdev': the mean ± mult standard deviations
  • 'ci': a level confidence interval for the mean, from Student’s t distribution
  • 'iqr': the first to the third quartile
  • 'range': the smallest value to the largest
'stderr', 'stdev' and 'ci' describe a mean, so they need estimate: 'mean'. 'iqr' describes a median, so it needs estimate: 'median'.
number
default:"0.95"
The confidence level, between 0 and 1. Only used with interval: 'ci'.
number
default:"1"
How many standard errors or deviations each end sits from the mean. Only used with interval: 'stderr' or 'stdev'.
An x-value and group with a single value, or with values that are all equal, gets no interval: there is no spread to measure. A param outside its allowed values fails with INVALID_STAT_PARAM.

Setting a stat at runtime

Two commands reach the stat:

Custom stats

A plugin can contribute its own stat: a Stat subclass registered through plugins, which gets a typed builder beside the built-ins. Its spec is only { type }, so it reads any settings from the layer’s params. A custom geom can also declare one as its defaultStat; its layers then summarize without naming it, and registering the geom registers the stat. See a summary is a stat for writing one, and Extending for registration.

Next