Skip to main content
A stat summarises a layer’s data before it’s drawn. Instead of plotting raw rows, the layer plots a computed result — a count, a mean, or a fitted regression line. Stats are set per layer through a geom’s stat option, and default to identity (draw the data as-is).

Available stats

Trendlines with smooth

The smooth stat fits a regression through a layer’s points — the basis for trendlines. Add it to a second geom.line() layer over your raw data:
method selects the regression family:
method
'linear' | 'loess' | 'exponential' | 'logarithmic' | 'quadratic' | 'power' | 'polynomial'
required
The regression method to fit.
order
number
Polynomial order — only used when method: 'polynomial'.
bandwidth
number
Smoothing bandwidth — only used when method: 'loess'.

A mean reference line

Pair stat.mean() with geom.rule() to draw a horizontal line at a series’ average:

Next