-
Notifications
You must be signed in to change notification settings - Fork 4
Lines and Segments #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
teunbrand
wants to merge
13
commits into
posit-dev:main
Choose a base branch
from
teunbrand:segments
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lines and Segments #159
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4331aa3
Implement segments
teunbrand db978b9
docs for segment
teunbrand cb814a0
enable hline/vline
teunbrand 34d2bbf
Add RenderContext to provide renderers with read-access to scales
teunbrand 026e5c8
Implement ABLineRenderer for slope-intercept lines
teunbrand d0bb7e5
update test expectations
teunbrand 79a751b
add docs
teunbrand 757a69e
fix clippy warning
teunbrand 02eb205
implement errorbar
teunbrand 3a3885f
add docs for errorbar
teunbrand cafd6c2
unite hline/vline into rule
teunbrand e7cd445
Merge branch 'main' into segments
teunbrand cdac6a1
rename abline/slope to linear/coef
teunbrand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| --- | ||
| title: "Errorbar" | ||
| --- | ||
|
|
||
| > 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. | ||
|
|
||
| Errorbars are used to display paired metrics, typically some interval, for a variable. It is displayed as a line between the two values, often with hinges at the ends. | ||
|
|
||
| ## Aesthetics | ||
| The following aesthetics are recognised by the errorbar layer. | ||
|
|
||
| ### Required | ||
| * `x` or `y`: Position on the x- or y-axis. These are mutually exclusive. | ||
| * `xmin` or `ymin`: Position of one of the interval ends orthogonal to the main position. These are also mutually exclusive. | ||
| * `xmax` or `ymax`: Position of the other interval end orthogonal to the main position. These are also mutually exclusive. | ||
|
|
||
| Note that the required aesthetics is either a set of {`x`, `ymin`, `ymax`} or {`y`, `xmin`, `xmax`} and *not* a combination of the two. | ||
|
|
||
| ### Optional | ||
| * `stroke`/`colour`: The colour of the lines in the errorbar. | ||
| * `opacity`: The opacity of the colour. | ||
| * `linewidth`: The width of the lines in the errorbar. | ||
| * `linetype`: The dash pattern of the lines in the errorbar. | ||
|
|
||
| ## Settings | ||
| * `width`: The width of the hinges in points. Can be set to `null` to not display hinges. | ||
|
|
||
| ## Data transformation | ||
| The errorbar layer does not transform its data but passes it through unchanged. | ||
|
|
||
| ## Examples | ||
|
|
||
| ```{ggsql} | ||
| #| code-fold: true | ||
| #| code-summary: "Create example data" | ||
| CREATE TABLE penguin_summary AS | ||
| SELECT | ||
| species, | ||
| MEAN(bill_dep) - STDDEV(bill_dep) AS low, | ||
| MEAN(bill_dep) AS mean, | ||
| MEAN(bill_dep) + STDDEV(bill_dep) AS high | ||
| FROM ggsql:penguins | ||
| GROUP BY species | ||
| ``` | ||
|
|
||
| Classic errorbar with point at centre. | ||
|
|
||
| ```{ggsql} | ||
| VISUALISE species AS x FROM penguin_summary | ||
| DRAW errorbar MAPPING low AS ymax, high AS ymin | ||
| DRAW point MAPPING mean AS y | ||
| ``` | ||
|
|
||
| Dynamite plot using bars instead of points, using extra wide hinges. | ||
|
|
||
| ```{ggsql} | ||
| VISUALISE species AS x FROM penguin_summary | ||
| DRAW errorbar | ||
| MAPPING low AS ymax, high AS ymin | ||
| SETTING width => 40 | ||
| DRAW bar MAPPING mean AS y | ||
| ``` | ||
|
|
||
| The hinges can be omitted by setting `null` as width. | ||
|
|
||
| ```{ggsql} | ||
| VISUALISE species AS x FROM penguin_summary | ||
| DRAW errorbar | ||
| MAPPING low AS ymax, high AS ymin | ||
| SETTING width => null | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| --- | ||
| title: "Linear line" | ||
| --- | ||
|
|
||
| > 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. | ||
|
|
||
| The linear layer is used to draw diagonal reference lines based on a coefficient and intercept. This is useful for adding regression lines, diagonal guides, or mathematical functions to plots. The lines extend across the full extent of the x-axis, regardless of the data range. | ||
| The layer is named for the following formula: | ||
|
|
||
| $$ | ||
| y = a + \beta x | ||
| $$ | ||
|
|
||
| Where $a$ is the `intercept` and $\beta$ is the `coef`. | ||
|
|
||
| ## Aesthetics | ||
| The following aesthetics are recognised by the abline layer. | ||
|
|
||
| ### Required | ||
| * `coef`: The coefficient/slope of the line i.e. the amount $y$ increases for every unit of $x$. | ||
| * `intercept`: The intercept where the line crosses the y-axis at $x = 0$. | ||
|
|
||
| ### Optional | ||
| * `colour`/`stroke`: The colour of the line | ||
| * `opacity`: The opacity of the line | ||
| * `linewidth`: The width of the line | ||
| * `linetype`: The type of the line, i.e. the dashing pattern | ||
|
|
||
| ## Settings | ||
| The linear layer has no additional settings. | ||
|
|
||
| ## Data transformation | ||
| The linear layer does not transform its data but passes it through unchanged. | ||
|
|
||
| ## Examples | ||
|
|
||
| Add a simple reference line to a scatterplot: | ||
|
|
||
| ```{ggsql} | ||
| VISUALISE FROM ggsql:penguins | ||
| DRAW point MAPPING bill_len AS x, bill_dep AS y | ||
| DRAW linear MAPPING 0.4 AS coef, -1 AS intercept | ||
| ``` | ||
|
|
||
| Add multiple reference lines with different colors from a separate dataset: | ||
|
|
||
| ```{ggsql} | ||
| WITH lines AS ( | ||
| SELECT * FROM (VALUES | ||
| (0.4, -1, 'Line A'), | ||
| (0.2, 8, 'Line B'), | ||
| (0.8, -19, 'Line C') | ||
| ) AS t(coef, intercept, label) | ||
| ) | ||
| VISUALISE FROM ggsql:penguins | ||
| DRAW point MAPPING bill_len AS x, bill_dep AS y | ||
| DRAW linear | ||
| MAPPING | ||
| coef AS coef, | ||
| intercept AS intercept, | ||
| label AS colour | ||
| FROM lines | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| --- | ||
| title: "H Line" | ||
| --- | ||
teunbrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| > 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. | ||
|
|
||
| The rule layer is used to draw horizontal or vertical reference lines at specified values. This is useful for adding thresholds, means, medians, avent markers, cutoff dates or other guides to the plot. The lines span the full width or height of the panels. | ||
|
|
||
| ## Aesthetics | ||
| The following aesthetics are recognised by the hline layer. | ||
|
|
||
| ### Required | ||
| * `x`\*: The x-coordinate for the vertical line. | ||
| * `y`\*: The y-coordinate for the horizontal line | ||
|
|
||
| \* Exactly one of `x` or `y` is required, not both. | ||
|
|
||
| ### Optional | ||
| * `colour`/`stroke`: The colour of the line | ||
| * `opacity`: The opacity of the line | ||
| * `linewidth`: The width of the line | ||
| * `linetype`: The type of the line, i.e. the dashing pattern | ||
|
|
||
| ## Settings | ||
| The rule layer has no additional settings. | ||
|
|
||
| ## Data transformation | ||
| The rule layer does not transform its data but passes it through unchanged. | ||
|
|
||
| ## Examples | ||
|
|
||
| Add a horizontal threshold line to a time series plot: | ||
|
|
||
| ```{ggsql} | ||
| SELECT Date AS date, temp AS temperature | ||
| FROM ggsql:airquality | ||
| WHERE Month = 5 | ||
|
|
||
| VISUALISE | ||
| DRAW line MAPPING date AS x, temperature AS y | ||
| DRAW rule MAPPING 70 AS y | ||
| ``` | ||
|
|
||
| Add a vertical line to mark a specific value: | ||
|
|
||
| ```{ggsql} | ||
| VISUALISE FROM ggsql:penguins | ||
| DRAW point MAPPING bill_len AS x, bill_dep AS y | ||
| DRAW rule MAPPING 45 AS x | ||
| ``` | ||
|
|
||
| Add multiple threshold lines with different colors: | ||
|
|
||
| ```{ggsql} | ||
| WITH thresholds AS ( | ||
| SELECT * FROM (VALUES | ||
| (70, 'Target'), | ||
| (80, 'Warning'), | ||
| (90, 'Critical') | ||
| ) AS t(value, label) | ||
| ) | ||
| SELECT Date AS date, temp AS temperature | ||
| FROM ggsql:airquality | ||
| WHERE Month = 5 | ||
|
|
||
| VISUALISE | ||
| DRAW line MAPPING date AS x, temperature AS y | ||
| DRAW rule MAPPING value AS y, label AS colour FROM thresholds | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| --- | ||
| title: "Segment" | ||
| --- | ||
|
|
||
| > 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. | ||
|
|
||
| The segment layer is used to create line segments between two endpoints. If differs from [lines](line.qmd) and [paths](path.qmd) in that it connects just two points rather than many. Data is expected to be in a different shape, with 4 coordinates for the start (x, y) and end (xend, yend) points on a single row. | ||
|
|
||
| ## Aesthetics | ||
| The following aesthetics are recognised by the segment layer. | ||
|
|
||
| ### Required | ||
| * `x`: Position along the x-axis of the start point. | ||
| * `y`: Position along the y-axis of the end point. | ||
| * `xend`\*: Position along the x-axis of the end point. | ||
| * `yend`\*: Position along the y-axis of the end point. | ||
|
|
||
| \* Only one of `xend` and `yend` is required. | ||
| If one is missing, it takes on the value of the start point. | ||
|
|
||
| ### Optional | ||
| * `colour`/`stroke`: The colour of the line. | ||
| * `opacity`: The opacity of the line. | ||
| * `linewidth`: The width of the line. | ||
| * `linetype`: The type of the line, i.e. the dashing pattern. | ||
|
|
||
| ## Settings | ||
| The segment layer has no additional settings. | ||
|
|
||
| ## Data transformation | ||
| The segment layer does not transform its data but passes it through unchanged. | ||
|
|
||
| ## Data transformation | ||
|
|
||
| ## Examples | ||
|
|
||
| Segments are useful when you have known start and end points of the data. For example in a graph | ||
|
|
||
| ```{ggsql} | ||
| WITH edges AS ( | ||
| SELECT * FROM (VALUES | ||
| (0, 0, 1, 1, 'A'), | ||
| (1, 1, 2, 1, 'A'), | ||
| (2, 1, 3, 0, 'A'), | ||
| (0, 3, 1, 2, 'B'), | ||
| (1, 2, 2, 2, 'B'), | ||
| (2, 2, 3, 3, 'B'), | ||
| (1, 1, 1, 2, 'C'), | ||
| (2, 2, 2, 1, 'C') | ||
| ) AS t(x, y, xend, yend, type) | ||
| ) | ||
| VISUALISE x, y, xend, yend FROM edges | ||
| DRAW segment MAPPING type AS stroke | ||
| ``` | ||
|
|
||
| You can use segments as part of a lollipop chart by anchoring one of the ends to 0. | ||
| Note that `xend` is missing and has taken up the value of `x`. | ||
|
|
||
| ```{ggsql} | ||
| SELECT ROUND(bill_dep) AS bill_dep, COUNT(*) AS n | ||
| FROM ggsql:penguins | ||
| GROUP BY ROUND(bill_dep) | ||
|
|
||
| VISUALISE bill_dep AS x, n AS y | ||
| DRAW segment MAPPING 0 AS yend | ||
| DRAW point | ||
| ``` | ||
|
|
||
| By overlaying a thick line on a thin line, you can create a candlestick chart. | ||
|
|
||
| ```{ggsql} | ||
| SELECT | ||
| FIRST(Date) AS date, | ||
| FIRST(temp) AS open, | ||
| LAST(temp) AS close, | ||
| MAX(temp) AS high, | ||
| MIN(temp) AS low, | ||
| CASE | ||
| WHEN FIRST(temp) > LAST(temp) THEN 'colder' | ||
| ELSE 'warmer' | ||
| END AS trend | ||
| FROM ggsql:airquality | ||
| GROUP BY WEEKOFYEAR(Date) | ||
|
|
||
| VISUALISE date AS x, trend AS colour | ||
| DRAW segment | ||
| MAPPING open AS y, close AS yend | ||
| SETTING linewidth => 5 | ||
| DRAW segment | ||
| MAPPING low AS y, high AS yend | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.