Skip to content

Commit 09ab79a

Browse files
authored
Add bidirectionality to layers (#183)
1 parent 14f0c1e commit 09ab79a

46 files changed

Lines changed: 2188 additions & 303 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

doc/syntax/clause/draw.qmd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,6 @@ ORDER BY <column>, ...
9898
```
9999

100100
For some layers the order of records in the data is important, e.g. the path layer which connect records in the order they are provided. Since databases often doesn't guarantee a specific order of the data, the `ORDER BY` clause can be used to enforce such and order. Even for layers where the order doesn't immediately seem to matter it may have an effect, e.g. an overplottet scatterplot where the records in the end of the data are plottet on top of the one in the start.
101+
102+
## Layer orientation
103+
Some layer types treat the two axes differently. For instance, a boxplot has categories along a discrete axis and summary statistics along a continuous one. While we are used to seeing boxplots with categories along the x-axis, this is not a necessity. The orientation can be deduced directly from the mappings in the layer. So, if you map discrete data to the x axis and continuous data to the y axis you get a boxplot in the standard orientation, whereas if you switch the mapping the boxes will "lay down" instead. The vast majority of layers that have an orientation also have a unique mapping pattern that allows us to deduce the orientation directly from the mapping. The few layers where the mapping is ambiguous (e.g. `line`) have an `orientation` setting that allows you to set the orientation explicitly.

doc/syntax/coord/polar.qmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ The polar coordinate system interprets its primary aesthetic as the angular posi
77
## Default aesthetics
88
The polar coordinate system has the following default positional aesthetics which will be used if no others have been provided:
99

10-
* **Primary**: `theta` (angular position)
11-
* **Secondary**: `radius` (distance from center)
10+
* **Primary**: `radius` (distance from center)
11+
* **Secondary**: `theta` (angular position)
1212

1313
Users can provide their own aesthetic names if needed. For example, if using `x` and `y` aesthetics:
1414

1515
```ggsql
1616
PROJECT y, x TO polar
1717
```
1818

19-
This maps `y` to theta (angle) and `x` to radius. This is useful when converting from a cartesian coordinate system without editing all the mappings.
19+
This maps `y` to radius and `x` to theta (angle). This is useful when converting from a cartesian coordinate system without editing all the mappings.
2020

2121
## Settings
2222
* `clip`: Should data be removed if it appears outside the bounds of the coordinate system. Defaults to `true`

doc/syntax/layer/type/area.qmd

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ The area layer is used to display absolute amounts over a sorted x-axis. It can
1010
The following aesthetics are recognised by the area layer.
1111

1212
### Required
13-
* `x`: Position along the x-axis.
14-
* `y`: Position along the y-axis.
13+
* Primary axis (e.g. `x`): The value of the independent variable.
14+
* Secondary axis (e.g. `y`): The value of the dependent variable.
1515

1616
### Optional
1717
* `stroke`: The colour of the contour lines.
@@ -22,9 +22,15 @@ The following aesthetics are recognised by the area layer.
2222

2323
## Settings
2424
* `position`: Determines the position adjustment to use for the layer (default is `'stack'`)
25+
* `orientation`: The orientation of the layer, see the [Orientation section](#orientation). One of the following:
26+
* `'aligned'` to align the layer's primary axis with the coordinate system's first axis.
27+
* `'transposed'` to align the layer's primary axis with the coordinate system's second axis.
2528

2629
## Data transformation
27-
The area layer does not transform its data but passes it through unchanged.
30+
The area layer sorts the data along its primary axis
31+
32+
## Orientation
33+
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.
2834

2935
## Examples
3036

@@ -73,3 +79,11 @@ An alternative is to center the stacks to create a steamgraph
7379
VISUALISE Date AS x, Value AS y, Series AS colour FROM long_airquality
7480
DRAW area SETTING position => 'stack', center => true
7581
```
82+
83+
You can combine this with the `orientation` setting to make a vertical steamgraph
84+
85+
```{ggsql}
86+
VISUALISE Date AS y, Value AS x, Series AS colour FROM long_airquality
87+
DRAW area
88+
SETTING position => 'stack', center => true, orientation => 'transposed'
89+
```

doc/syntax/layer/type/bar.qmd

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ The following aesthetics are recognised by the bar layer.
1313
The bar layer has no required aesthetics
1414

1515
### Optional
16-
* `x`: Position on the x-axis. If missing all records will be shown in the same bar
17-
* `y`: The height of the plot. If missing, it will be calculated by the layer
16+
* Primary axis (e.g. `x`): The categories to create bars for. If missing all records will be shown in the same bar
17+
* Secondary axis (e.g. `y`): The height of the bars. If missing, it will be calculated by the layer
1818
* `colour`: The default colour of each bar
1919
* `stroke`: The colour of the stroke around each bar. Overrides `colour`
2020
* `fill`: The fill colour of each bar. Overrides `colour`
@@ -27,7 +27,7 @@ The bar layer has no required aesthetics
2727
* `width`: The width of the bars as a proportion of the available width
2828

2929
## Data transformation
30-
If `y` has not been mapped the layer will calculate it for you.
30+
If the secondary axis has not been mapped the layer will calculate counts for you and display these as the secondary axis.
3131

3232
### Properties
3333

@@ -40,7 +40,10 @@ If `y` has not been mapped the layer will calculate it for you.
4040

4141
### Default remappings
4242

43-
* `count AS y`: By default the barplot will show count as the height of the bars
43+
* `count AS <secondary axis>`: By default the barplot will show count as the height of the bars
44+
45+
## Orientation
46+
Bar plots have categories along their primary axis. The orientation is deduced directly from the mapping. To create a horizontal bar plot you map the categories to `y` instead of `x` (assuming a default Cartesian coordinate system).
4447

4548
## Examples
4649

@@ -97,6 +100,19 @@ SCALE BINNED x
97100
SETTING breaks => 10
98101
```
99102

103+
Create a horizontal bar plot by changing the mapping
104+
105+
```{ggsql}
106+
VISUALISE FROM ggsql:penguins
107+
DRAW bar
108+
MAPPING species AS y
109+
```
110+
100111
And use with a polar coordinate system to create a pie chart
101112

102-
**TBD**
113+
```{ggsql}
114+
VISUALISE FROM ggsql:penguins
115+
DRAW bar
116+
MAPPING species AS fill
117+
PROJECT TO polar
118+
```

doc/syntax/layer/type/boxplot.qmd

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Boxplots display a summary of a continuous distribution. In the style of Tukey,
99
The following aesthetics are recognised by the boxplot layer.
1010

1111
### Required
12-
* `x`: Position on the x-axis
13-
* `y`: Position on the y-axis
12+
* Primary axis (e.g. `x`): The categorical variable to group by
13+
* Secondary axis (e.g. `y`): The continuous variable to summarize
1414

1515
### Optional
1616
* `stroke`: The colour of the box contours, whiskers, median line and outliers.
@@ -29,11 +29,11 @@ The following aesthetics are recognised by the boxplot layer.
2929
* `width`: Relative width of the boxes. Defaults to `0.9`.
3030

3131
## Data transformation
32-
Per group, data will be divided into 4 quartiles and summary statistics will be derived from their extremes.
33-
Because number of observations per quartile may differ by one, the result of this approach may slightly differ from a pure quantile-based approach.
34-
The central line represents the median.
35-
The boxes are displayed from the 25th up to the 75th percentiles.
36-
The whiskers are calculated from the 25th/75th percentiles +/- the IQR times `coef`, but no more extreme than the data extrema.
32+
Per group, data will be divided into 4 quartiles and summary statistics will be derived from their extremes.
33+
Because number of observations per quartile may differ by one, the result of this approach may slightly differ from a pure quantile-based approach.
34+
The central line represents the median.
35+
The boxes are displayed from the 25th up to the 75th percentiles.
36+
The whiskers are calculated from the 25th/75th percentiles +/- the IQR times `coef`, but no more extreme than the data extrema.
3737
Observations are considered outliers when they are more extreme than the whiskers.
3838

3939
### Calculated statistics
@@ -43,7 +43,10 @@ Observations are considered outliers when they are more extreme than the whisker
4343

4444
### Default remapping
4545

46-
* `value AS y`: By default the values are displayed along the y-axis.
46+
* `value AS <secondary axis>`: By default the values are displayed along the secondary axis.
47+
48+
## Orientation
49+
The boxplot has its categorical groups along the primary axis and the continuous values along the secondary axis. The orientation can be deduced from the scale types or from the mapping. To create a horizontal boxplot, map the categorical variable to `y` and the continuous variable to `x` (assuming a default Cartesian coordinate system).
4750

4851
### Examples
4952

@@ -83,3 +86,11 @@ DRAW boxplot
8386
MAPPING species AS x, bill_len AS y
8487
SETTING coef => 0.1
8588
```
89+
90+
Create a horizontal boxplot by swapping `x` and `y`:
91+
92+
```{ggsql}
93+
VISUALISE FROM ggsql:penguins
94+
DRAW boxplot
95+
MAPPING species AS y, bill_len AS x
96+
```

doc/syntax/layer/type/density.qmd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Visualise the distribution of a single continuous variable by computing a kernel
1010
The following aesthetics are recognised by the density layer.
1111

1212
### Required
13-
* `x`: Position on the x-axis.
13+
* Primary axis (e.g. `x`): The continuous variable for which to estimate density.
1414

1515
### Optional
1616
* `stroke`: The colour of the contour lines.
@@ -62,7 +62,10 @@ $$
6262

6363
### Default remappings
6464

65-
* `density AS y`: By default the density layer will display the computed density along the y-axis.
65+
* `density AS <secondary axis>`: By default the density layer will display the computed density along the secondary axis.
66+
67+
## Orientation
68+
The density has its primary axis along the variable for which density is computed. The orientation can be deduced from the mapping. To create a horizontal density plot, map the variable to `y` instead of `x` (assuming a default Cartesian coordinate system).
6669

6770
## Examples
6871

doc/syntax/layer/type/errorbar.qmd

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ Errorbars are used to display paired metrics, typically some interval, for a var
1010
The following aesthetics are recognised by the errorbar layer.
1111

1212
### Required
13-
* `x` or `y`: Position on the x- or y-axis. These are mutually exclusive.
14-
* `xmin` or `ymin`: Position of one of the interval ends orthogonal to the main position. These are also mutually exclusive.
15-
* `xmax` or `ymax`: Position of the other interval end orthogonal to the main position. These are also mutually exclusive.
16-
17-
Note that the required aesthetics is either a set of {`x`, `ymin`, `ymax`} or {`y`, `xmin`, `xmax`} and *not* a combination of the two.
13+
* Primary axis (e.g. `x`): Position along the primary axis
14+
* Secondary axis minimum (e.g. `ymin`): Lower position along the secondary axis.
15+
* Secondary axis maximum (e.g. `ymax`): Upper position along the secondary axis.
1816

1917
### Optional
2018
* `stroke`/`colour`: The colour of the lines in the errorbar.
@@ -28,6 +26,9 @@ Note that the required aesthetics is either a set of {`x`, `ymin`, `ymax`} or {`
2826
## Data transformation
2927
The errorbar layer does not transform its data but passes it through unchanged.
3028

29+
## Orientation
30+
The orientation of errorbar layers is deduced directly from the mapping, because the interval is mapped to the secondary axis. To create a horizontal errorbar layer, you map the independent variable to `y` instead of `x` and the interval to `xmin` and `xmax` (assuming a default Cartesian coordinate system).
31+
3132
## Examples
3233

3334
```{ggsql}

doc/syntax/layer/type/histogram.qmd

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ title: "Histogram"
44

55
> Layers are declared with the [`DRAW` clause](../../clause/draw.qmd). Read the documentation for this clause for a thorough description of how to use it.
66
7-
Visualise the distribution of a single continuous variable by dividing the x axis into bins and counting the number of observations in each bin. If providing a weight then a weighted histogram is calculated instead.
7+
Visualise the distribution of a single continuous variable by dividing the primary axis into bins and counting the number of observations in each bin. If providing a weight then a weighted histogram is calculated instead.
88

99
## Aesthetics
1010
The following aesthetics are recognised by the bar layer.
1111

1212
### Required
13-
* `x`: Position on the x-axis
13+
* Primary axis (e.g. `x`): The continuous variable to bin
1414

1515
### Optional
1616
* `colour`: The default colour of each bar
@@ -27,7 +27,7 @@ The following aesthetics are recognised by the bar layer.
2727
* `closed`: Either `'left'` or `'right'` (default). Determines whether the bin intervals are closed to the left or right side
2828

2929
## Data transformation
30-
The histogram layer will bin the records in each group and count them. By default it will map the count to `y`.
30+
The histogram layer will bin the records in each group and count them. By default it will map the count to the secondary axis.
3131

3232
### Properties
3333

@@ -40,7 +40,10 @@ The histogram layer will bin the records in each group and count them. By defaul
4040

4141
### Default remappings
4242

43-
* `count AS y`: By default the histogram will show count as the height of the bars
43+
* `count AS <secondary axis>`: By default the histogram will show count as the height of the bars
44+
45+
## Orientation
46+
The histogram has its primary axis along the binned variable. The orientation is deduced directly from the mapping. To create a horizontal histogram, you map the variable to `y` instead of `x` (assuming a default Cartesian coordinate system).
4447

4548
## Examples
4649

@@ -86,3 +89,11 @@ DRAW histogram
8689
MAPPING body_mass AS x
8790
SETTING binwidth => 100
8891
```
92+
93+
Create a histogram along the y axis by changing the mapping
94+
95+
```{ggsql}
96+
VISUALISE FROM ggsql:penguins
97+
DRAW histogram
98+
MAPPING body_mass AS y
99+
```

doc/syntax/layer/type/line.qmd

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ The line layer is used to create lineplots. Lineplots always connects records al
1010
The following aesthetics are recognised by the line layer.
1111

1212
### Required
13-
* `x`: Position along the x-axis
14-
* `y`: Position along the y-axis
13+
* Primary axis (e.g. `x`): The value of the independent variable.
14+
* Secondary axis (e.g. `y`): The value of the dependent variable.
1515

1616
### Optional
1717
* `colour`/`stroke`: The colour of the line
@@ -21,9 +21,15 @@ The following aesthetics are recognised by the line layer.
2121

2222
## Settings
2323
* `position`: Determines the position adjustment to use for the layer (default is `'identity'`)
24+
* `orientation`: The orientation of the layer, see the [Orientation section](#orientation). One of the following:
25+
* `'aligned'` to align the layer's primary axis with the coordinate system's first axis.
26+
* `'transposed'` to align the layer's primary axis with the coordinate system's second axis.
2427

2528
## Data transformation
26-
The line layer does not transform its data but passes it through unchanged
29+
The line layer sorts the data along its primary axis.
30+
31+
## Orientation
32+
Line 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. If you wish to create a vertical line plot, you need to set `orientation => 'transposed'` to indicate that the primary layer axis follows the second axis of the coordinate system.
2733

2834
## Examples
2935

doc/syntax/layer/type/linear.qmd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ $$
1313

1414
Where $a$ is the `intercept` and $\beta$ is the `coef`.
1515

16+
> The linear layer doesn't have a natural extend along either axes and the scale ranges can thus not be deduced from it. If the linear layer is the only layer in the visualisation you must specify the position scale ranges manually.
17+
1618
## Aesthetics
1719
The following aesthetics are recognised by the abline layer.
1820

@@ -27,11 +29,16 @@ The following aesthetics are recognised by the abline layer.
2729
* `linetype`: The type of the line, i.e. the dashing pattern
2830

2931
## Settings
30-
The linear layer has no additional settings.
32+
* `orientation`: The orientation of the layer, see the [Orientation section](#orientation). One of the following:
33+
* `'aligned'` to align the layer's primary axis with the coordinate system's first axis.
34+
* `'transposed'` to align the layer's primary axis with the coordinate system's second axis.
3135

3236
## Data transformation
3337
The linear layer does not transform its data but passes it through unchanged.
3438

39+
## Orientation
40+
Linear layers use the predictor ($x$) for their primary axis and the response $y$ for its secondary axis. Since the primary axis cannot be deduced from the mapping it must be specified using the `orientation` setting. E.g. to create a vertical linear plot, you need to set `orientation => 'transposed'` to indicate that the primary layer axis follows the second axis of the coordinate system.
41+
3542
## Examples
3643

3744
Add a simple reference line to a scatterplot:

0 commit comments

Comments
 (0)