You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/syntax/clause/draw.qmd
+42Lines changed: 42 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -76,6 +76,48 @@ The `SETTING` clause can be used for two different things:
76
76
#### Position
77
77
A special setting is `position` which controls how overlapping objects are repositioned to avoid overlapping etc. Position adjustments have special mapping requirements so all position adjustments will not be relevant for all layer types. Different layers have different defaults as detailed in their documentation. You can read about each different position adjustment at [their own documentation sites](../index.qmd#position-adjustments).
78
78
79
+
#### Aggregate
80
+
Some layers support aggregation of their data through the `aggregate` setting. Their documentation will state this. `aggregate` collapses each group to a single row, replacing every numeric mapping in place with its aggregated value. Groups are defined by `PARTITION BY` together with all discrete mappings.
81
+
82
+
The `aggregate` setting takes a single string or an array of strings. Each string is one of:
83
+
84
+
***Untargeted** — `'<func>'` (no prefix). With one untargeted aggregation, the function applies to every numeric mapping that doesn't have a targeted aggregation. With two untargeted aggregations, the first is used for the lower side of range layers (e.g. `x`/`xmin`) plus all non-range layers, and the second is used for the upper side of range layers (e.g. `xend`/`xmax`). More than two untargeted aggregations is not allowed.
85
+
***Targeted** — `'<aes>:<func>'`. Applies `func` to the named aesthetic only (`<aes>` is a name like `x`, `y`, `xmin`, `xmax`, `xend`, `yend`, `color`, `size`, …). A target overrides any untargeted aggregation for that aesthetic.
86
+
87
+
A numeric mapping is dropped from the layer with a warning, when it has neither a target nor an applicable default.
88
+
89
+
##### Aggregate functions
90
+
Aggregation can either be a simple function or a band function. The simple functions are:
91
+
92
+
*`'count'`: Non-null tally of the bound column.
93
+
*`'sum'` and `'prod'`: The sum or product
94
+
*`'min'`, `'max'`: Extremes
95
+
*`'range'` (max - min), `'mid'` (min + max) / 2
96
+
*`'mean'`, and `'median'`: Central tendency
97
+
*`'geomean'`, `'harmean'`, and `'rms'`: Geometric, harmonic, and root-mean-square
98
+
*`'sdev'`, `'var'`, `'iqr'`, and `'se'`: Standard deviation, variance, interquartile range, and standard error
99
+
*`'p05'`, `'p10'`, `'p25'`, `'p50'`, `'p75'`, `'p90'`, and `'p95'`: Percentiles
100
+
*`'first'` and `'last'`: The first or last value in the group, in row order. Note that the row order within a group is engine-defined unless the source query has an `ORDER BY` — these are most useful when the upstream SQL provides an explicit ordering.
101
+
*`'diff'`: `last - first`. The change between the first and last value in row order — same ordering caveat applies.
102
+
103
+
For band functions you combine an offset with an expansion, potentially multiplied. An example could be `'mean-1.96sdev'` which does exactly what you'd expect it to be. The general form is `<offset>±<multiplier><expansion>` with `<multiplier>` being optional (defaults to `1`).
Allowed expansions are: `'sdev'`, `'se'`, `'var'`, `'iqr'`, and `'range'`
108
+
109
+
##### Exploded aggregation
110
+
You can also target the same aesthetic more than once to produce *multiple rows per group* — one for each function. We call that *exploded aggregation*. For example `aggregate => ('y:min', 'y:max')` emits a min row and a max row per group, so a single `DRAW line` produces two summary lines that connect within each group rather than across them. When multiple rows are created, a synthetic `aggregate` column is made that tags each row with the name of the aggregation function. You can use this with a `REMAPPING` to drive another aesthetic — e.g. `REMAPPING aggregate AS stroke` to colour the two lines differently. The column's value is built from the per-row function names of the *exploded* targets, deduplicated, and joined with `/`:
*`aggregate => ('y:min', 'y:max', 'color:median')` → rows tagged `'min'`, `'max'` (the single-function `color` target is recycled across rows and is not part of the label).
When several aesthetics are targeted with the same number of functions, they explode in lockstep: row 1 uses each aesthetic's first function, row 2 the second, and so on. Aesthetics with a single function — and the unprefixed defaults — are reused unchanged across every row. Mixing different numbers of aggregation metrics above 1 across aesthetics is not allowed.
118
+
119
+
In the single-row (reduction) case aggregation applies in place — no `REMAPPING` is needed and no synthetic column is added. Only the multi-row (explosion) case described above introduces the synthetic `aggregate` column.
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/area.qmd
+6-1Lines changed: 6 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -25,9 +25,14 @@ The following aesthetics are recognised by the area layer.
25
25
*`orientation`: The orientation of the layer, see the [Orientation section](#orientation). One of the following:
26
26
*`'aligned'` to align the layer's primary axis with the coordinate system's first axis.
27
27
*`'transposed'` to align the layer's primary axis with the coordinate system's second axis.
28
+
*`aggregate` Aggregation functions to apply per group:
29
+
*`null` apply no group aggregation (default).
30
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
28
31
29
32
## Data transformation
30
-
The area layer sorts the data along its primary axis
33
+
This layer supports aggregation through the `aggregate` setting. Aggregation groups are defined by `PARTITION BY`, all discrete mappings, but also the primary axis. Within each group, every numeric mapping is replaced in place by its aggregated value. Use a default like `'mean'` or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
34
+
35
+
Further, the area layer sorts the data along its primary axis before returning it.
31
36
32
37
## Orientation
33
38
Area plots are sorted and connected along their primary axis. Since the primary axis cannot be deduced from the mapping it must be specified using the `orientation` setting. E.g. if you wish to create a vertical area plot you need to set `orientation => 'transposed'` to indicate that the primary layer axis follows the second axis of the coordinate system.
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/bar.qmd
+17Lines changed: 17 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -25,10 +25,15 @@ The bar layer has no required aesthetics
25
25
## Settings
26
26
*`position`: Position adjustment. One of `'identity'`, `'stack'` (default), `'dodge'`, or `'jitter'`
27
27
*`width`: The width of the bars as a proportion of the available width (0 to 1)
28
+
*`aggregate` Aggregation functions to apply per group:
29
+
*`null` apply no group aggregation (default).
30
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
28
31
29
32
## Data transformation
30
33
If the secondary axis has not been mapped the layer will calculate counts for you and display these as the secondary axis.
31
34
35
+
This layer supports aggregation through the `aggregate` setting. Aggregation groups are defined by `PARTITION BY` and all discrete mappings. Within each group, every numeric mapping is replaced in place by its aggregated value. Use a default like `'mean'` or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
36
+
32
37
### Properties
33
38
34
39
*`weight`: If mapped, the sum of the weights within each group is calculated instead of the count in each group
@@ -116,3 +121,15 @@ DRAW bar
116
121
MAPPING species AS fill
117
122
PROJECT TO polar
118
123
```
124
+
125
+
Use a different type of aggregation for the bars through the `aggregate` setting. The `range` layer needs both `ymin` and `ymax` mapped; with two defaults, the first is applied to the lower bound and the second to the upper bound.
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/line.qmd
+18-2Lines changed: 18 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -24,9 +24,15 @@ The following aesthetics are recognised by the line layer.
24
24
*`orientation`: The orientation of the layer, see the [Orientation section](#orientation). One of the following:
25
25
*`'aligned'` to align the layer's primary axis with the coordinate system's first axis.
26
26
*`'transposed'` to align the layer's primary axis with the coordinate system's second axis.
27
+
*`aggregate` Aggregation functions to apply per group:
28
+
*`null` apply no group aggregation (default).
29
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
27
30
28
31
## Data transformation
29
-
The line layer sorts the data along its primary axis.
32
+
This layer supports aggregation through the `aggregate` setting. Aggregation groups are defined by `PARTITION BY`, all discrete mappings, but also the primary axis. Within each group, every numeric mapping is replaced in place by its aggregated value. Use a default like `'mean'` or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
33
+
34
+
Further, the line layer sorts the data along its primary axis before returning it.
35
+
30
36
If the line has a variable `stroke` or `opacity` aesthetic within groups, the line is broken into segments.
31
37
Each segment gets the property of the preceding datapoint, so the last datapoint in a group does not transfer these properties.
32
38
@@ -89,4 +95,14 @@ VISUALISE x, y FROM data
89
95
DRAW line
90
96
MAPPING z AS linewidth
91
97
SCALE linewidth TO (0, 30)
92
-
```
98
+
```
99
+
100
+
Use aggregation to draw min and max lines from a set of observations on a single layer. Targeting `y` twice produces one summary row per function within the same group. A synthetic `aggregate` column tags each row with the different function names, that you can remap to colour the lines distinctly:
101
+
102
+
```{ggsql}
103
+
VISUALISE Day AS x, Temp AS y FROM ggsql:airquality
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/point.qmd
+14-1Lines changed: 14 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -23,9 +23,12 @@ The following aesthetics are recognised by the point layer.
23
23
24
24
## Settings
25
25
*`position`: Position adjustment. One of `'identity'` (default), `'stack'`, `'dodge'`, or `'jitter'`
26
+
*`aggregate` Aggregation functions to apply per group:
27
+
*`null` apply no group aggregation (default).
28
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
26
29
27
30
## Data transformation
28
-
The point layer does not transform its data but passes it through unchanged
31
+
This layer supports aggregation through the `aggregate` setting. Aggregation groups are defined by `PARTITION BY` and all discrete mappings. Within each group, every numeric mapping is replaced in place by its aggregated value. Use a default like `'mean'` or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
29
32
30
33
## Orientation
31
34
The point layer has no orientation. The axes are treated symmetrically.
@@ -72,3 +75,13 @@ VISUALISE species AS x, bill_dep AS y FROM ggsql:penguins
72
75
DRAW point
73
76
SETTING position => 'jitter', distribution => 'density'
74
77
```
78
+
79
+
Use aggregation to show a single point per group
80
+
81
+
```{ggsql}
82
+
VISUALISE species AS x, island AS y, body_mass AS fill, body_mass AS size
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/range.qmd
+26-1Lines changed: 26 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -22,9 +22,12 @@ The following aesthetics are recognised by the range layer.
22
22
23
23
## Settings
24
24
*`width`: The width of the hinges in points (must be >= 0). Defaults to 10. Can be set to `null` to not display hinges.
25
+
*`aggregate` Aggregation functions to apply per group:
26
+
*`null` apply no group aggregation (default).
27
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
25
28
26
29
## Data transformation
27
-
The range layer does not transform its data but passes it through unchanged.
30
+
This layer supports aggregation through the `aggregate` setting. Within each group, defined by `PARTITION BY` and all discrete mappings, every numeric mapping is replaced in place by its aggregated value, producing one range per group. Range is a range layer with two defaults: the first applies to the start point (`xmin`/`ymin`) and the second applies to the end point (`xmax`/`ymax`). Use a single default like `'mean'` to apply the same function to all values, or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
28
31
29
32
## Orientation
30
33
The orientation of range layers is deduced directly from the mapping, because the interval is mapped to the secondary axis. To create a horizontal range layer, you map the independent variable to `y` instead of `x` and the interval to `xmin` and `xmax` (assuming a default Cartesian coordinate system).
@@ -108,3 +111,25 @@ DRAW range
108
111
MAPPING low AS ymin, high AS ymax
109
112
SETTING width => null
110
113
```
114
+
115
+
Rather than precomputing the values and plotting them, you can use the aggregate functionality to calculate the relevant statistics dynamically:
116
+
117
+
```{ggsql}
118
+
VISUALISE Date AS x, Temp AS ymin, Temp AS ymax, Temp AS color
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/ribbon.qmd
+12-1Lines changed: 12 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -23,9 +23,12 @@ The following aesthetics are recognised by the ribbon layer.
23
23
24
24
## Settings
25
25
*`position`: Position adjustment. One of `'identity'` (default), `'stack'`, `'dodge'`, or `'jitter'`
26
+
*`aggregate` Aggregation functions to apply per group:
27
+
*`null` apply no group aggregation (default).
28
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
26
29
27
30
## Data transformation
28
-
The ribbon layer sorts the data along its primary axis
31
+
This layer supports aggregation through the `aggregate` setting. Within each group, defined by `PARTITION BY` and all discrete mappings, every numeric mapping is replaced in place by its aggregated value, producing one ribbon per group. Ribon is a range layer with two defaults: the first applies to the start point (`xmin`/`ymin`) and the second applies to the end point (`xmax`/`ymax`). Use a single default like `'mean'` to apply the same function to all values, or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
29
32
30
33
## Orientation
31
34
Ribbon layers are sorted and connected along their primary axis. The orientation is deduced directly from the mapping, because the interval is mapped to the secondary axis. To create a vertical ribbon layer you map the independent variable to `y` instead of `x` and the interval to `xmin` and `xmax` (assuming a default Cartesian coordinate system).
@@ -59,3 +62,11 @@ DRAW ribbon
59
62
DRAW line
60
63
MAPPING MeanTemp AS y
61
64
```
65
+
66
+
Use aggregation to calculate bounds on the fly. The two untargeted aggregation functions target the `ymin` and `ymax` aesthetics automatically.
67
+
68
+
```{ggsql}
69
+
VISUALISE Day AS x, Temp AS ymin, Temp AS ymax FROM ggsql:airquality
Copy file name to clipboardExpand all lines: doc/syntax/layer/type/rule.qmd
+15-1Lines changed: 15 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -25,8 +25,12 @@ The following aesthetics are recognised by the rule layer.
25
25
26
26
## Settings
27
27
*`position`: Position adjustment. One of `'identity'` (default), `'stack'`, `'dodge'`, or `'jitter'`
28
+
*`aggregate` Aggregation functions to apply per group:
29
+
*`null` apply no group aggregation (default).
30
+
* A single string or an array of strings. See an overview of aggregation function in [the `DRAW` documentation](../../clause/draw.qmd#aggregate) and more information in the *Data transformation* section below.
28
31
29
32
## Data transformation
33
+
This layer supports aggregation through the `aggregate` setting. Aggregation groups are defined by `PARTITION BY` and all discrete mappings. Within each group, every numeric mapping is replaced in place by its aggregated value. Use a default like `'mean'` or target individual aesthetics with `'<aes>:<func>'`. See [the `DRAW` documentation](../../clause/draw.qmd#aggregate) for the full setting shape.
30
34
31
35
For diagonal lines, the position aesthetic determines the intercept:
32
36
@@ -110,4 +114,14 @@ VISUALISE FROM ggsql:penguins
0 commit comments