Code
library(ggalign)
#> Loading required package: ggplot2
#>
#> Attaching package: 'ggalign'
#> The following object is masked from 'package:ggplot2':
#>
#> element_polygon
library(ggalign)
#> Loading required package: ggplot2
#>
#> Attaching package: 'ggalign'
#> The following object is masked from 'package:ggplot2':
#>
#> element_polygon
ggalign
provides a linking mechanism that allows you to select observations and either:
annotate them with additional plots, or
visually connect them using lines, polygons, or other elements.
The pair_links()
function is used to define pairs of connected observations. In the formula, the left side (before the ~
) represents hand1
—the observations on the left (for a horizontal stack layout) or top (for a vertical stack layout). The right side (after the ~
) represents hand2
—the observations on the right (for horizontal stack) or bottom (for vertical stack).
These defined pairs will either be linked together, or each group in the pair will be linked separately within the same plot area.
Both sides of the formula can be specified using integer or character indices of the original data (before reordering).
pair_links(1:2 ~ c("c", "d"), c("a", "b") ~ 3:4)
#> <ggalign_pair_links>
#> A total of 2 pairs of link groups
#>
#> hand1 ~ hand2
#> 1: 1:2 ~ c("c", "d")
#> 2: c("a", "b") ~ 3:4
#>
#> A total of 4 link groups
The print method showed various informations:
If only the left-hand side of the formula exists, you can input it directly.
pair_links(1:2, c("a", "b"))
#> <ggalign_pair_links>
#> A total of 2 pairs of link groups
#>
#> hand1 ~ hand2
#> 1: 1:2 ~
#> 2: c("a", "b") ~
#>
#> A total of 2 link groups
To specify links in the right only, you must use formula:
pair_links(~ 1:2, ~ c("a", "b"))
#> <ggalign_pair_links>
#> A total of 2 pairs of link groups
#>
#> hand1 ~ hand2
#> 1: ~ 1:2
#> 2: ~ c("a", "b")
#>
#> A total of 2 link groups
For integer indices, wrap them with I()
to follow ordering from the layout.
pair_links(I(1:2))
#> <ggalign_pair_links>
#> A total of 1 pair of link groups
#>
#> hand1 ~ hand2
#> 1: I(1:2) ~
#>
#> A total of 1 link group
You can wrap the whole formula.
pair_links(I(1:2 ~ 3:4))
#> <ggalign_pair_links>
#> A total of 1 pair of link groups
#>
#> hand1 ~ hand2
#> 1: I(1:2) ~ I(3:4)
#>
#> A total of 2 link groups
range_link()
function can be used to define a range of observations, which accepts two argument that specify the lower and upper bounds of the range. These bounds should be defined as a single integer or character.
pair_links(range_link(1, "a"))
#> <ggalign_pair_links>
#> A total of 1 pair of link groups
#>
#> hand1 ~ hand2
#> 1: range_link(1, "a") ~
#>
#> A total of 1 link group
In this case, the left-hand is defined as the range between the 1st
observation and the observation named "a"
.
You can also use waiver()
to inherit values from the opposite group.
pair_links(range_link(1, "a") ~ waiver())
#> <ggalign_pair_links>
#> A total of 1 pair of link groups
#>
#> hand1 ~ hand2
#> 1: range_link(1, "a") ~ waiver()
#>
#> A total of 2 link groups
pair_links(waiver() ~ range_link(1, "a"))
#> <ggalign_pair_links>
#> A total of 1 pair of link groups
#>
#> hand1 ~ hand2
#> 1: waiver() ~ range_link(1, "a")
#>
#> A total of 2 link groups
You can combine any of these into a list.
pair_links(list(range_link(1, "a"), waiver()) ~ list(4:5, c("b", "c")))
#> <ggalign_pair_links>
#> A total of 1 pair of link groups
#>
#> hand1 ~ hand2
#> 1: list(range_link(1, "a"), waiver()) ~ list(4:5, c("b", "c"))
#>
#> A total of 2 link groups
In this example, the left side combines the range between the 1st
observation and “a” with the observations 4 and 5, and the observations “b” and “c”.