Skip to content

Commit 6363e0f

Browse files
willgeartyms609
andauthored
Fix add_phylopic_tree and use it in the base R vignette (#121)
* Make add_phylopic_tree work for fossil tips * Use add_phylopic_tree in base R vignette * Clean up add_phylopic_tree docs * Fix x calculation * Update tests and snapshots * Update docs to match new behavior * Allow for aligning to the plot edge * Apply suggestions from code review Co-authored-by: Martin R. Smith <[email protected]> * Update docs for previous commit * Switch hjust depending on alignment * switch padding direction if aligned to plot edge; add to test * Whoops, add test changes * Note assumptions * Update vignette --------- Co-authored-by: Martin R. Smith <[email protected]>
1 parent 10be6cc commit 6363e0f

16 files changed

+139
-93
lines changed

R/add_phylopic_tree.R

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,39 @@
33
#' Specify existing images, taxonomic names, or PhyloPic uuids to add PhyloPic
44
#' silhouettes alongside the associated leaves of a phylogenetic tree that has
55
#' been plotted in the active graphics device using the base R graphics
6-
#' functions.
6+
#' functions. The current functionality assumes that the tree is not in a
7+
#' circular configuration and has a "rightwards" direction.
78
#'
89
#' @inheritParams add_phylopic_base
9-
#' @param tree The phylogenetic tree object of class `phylo` on which to add
10-
#' the silhouette.
11-
#' @param tip The tip labels against which to add the silhouettes.
12-
#' If not specified, the names of the `img`, `uuid` or `name` vector are used.
13-
#' @param relWidth The width of each silhouette relative to the plotting area.
14-
#' @param padding,relPadding Distance to inset each silhouette from the right
15-
#' edge of the plotting area,
16-
#' in the plot coordinate system (`padding`) or
17-
#' relative to the size of the plotting area (`relPadding`).
18-
#' Negative values offset to the right.
19-
#' @param \dots Further arguments to pass to `add_phylopic_base()`.
20-
#' @author [Martin R. Smith](https://orcid.org/0000-0001-5660-1727)
21-
22-
#' @seealso
23-
#' For trees plotted using \pkg{ggtree}, see [`geom_phylopic()`].
10+
#' @param tree `phylo`. The phylogenetic tree object on which to add the
11+
#' silhouette.
12+
#' @param tip `character`. The tip labels against which to add the silhouettes. If not
13+
#' specified, the names of the `img`, `uuid` or `name` vector are used.
14+
#' @param align \code{character}. Should each silhouette be aligned to its
15+
#' respective tip (`"tip"`, the default) or to the right-hand side of the
16+
#' plotting area (`"plot"`)? If `"tip"` is specified, the silhouette is placed
17+
#' at the x coordinate of the respective tip, plus any horizontal padding
18+
#' specified by `padding` or `relPadding`. If `"plot"` is specified, the
19+
#' silhouette is placed at the right-hand side of the plotting area,
20+
#' determined by `par("usr")`, plus any horizontal padding specified by
21+
#' `padding` or `relPadding`.
22+
#' @param width,relWidth `numeric`. The width of each silhouette, in the plot coordinate
23+
#' system (`width`) or relative to the size of the plotting area (`relWidth`).
24+
#' If "NULL" and `height` is specified, the width is determined by the aspect
25+
#' ratio of the original image. One of height and width must be "NULL".
26+
#' @param padding,relPadding `numeric`. Horizontal padding for each silhouette from its
27+
#' respective x value, in the plot coordinate system (`padding`) or relative
28+
#' to the size of the plotting area (`relPadding`). Negative values offset to
29+
#' the left.
30+
#' @param \dots Further arguments to pass to [add_phylopic_base()].
31+
#' @author [Martin R. Smith](https://orcid.org/0000-0001-5660-1727)
32+
33+
#' @seealso For trees plotted using \pkg{ggtree}, see [`geom_phylopic()`].
2434
#' @importFrom ape plot.phylo .PlotPhyloEnv
2535
#' @importFrom grDevices dev.cur
2636
#' @export
2737
#' @examples \dontrun{
28-
#' # Load the ape library to work with phylogenetic trees
38+
#' # Load the ape library to work with phylogenetic trees
2939
#' library("ape")
3040
#'
3141
#' # Read a phylogenetic tree
@@ -50,11 +60,14 @@ add_phylopic_tree <- function(tree, tip = names(img) %||% names(uuid) %||%
5060
names(name) %||% name,
5161
img = NULL,
5262
name = if (is.null(img) && is.null(uuid)) tip
53-
else NULL,
54-
uuid = NULL, width, padding = NULL,
55-
relWidth = 0.06, relPadding = 1/200,
56-
hjust = 0,
63+
else NULL,
64+
uuid = NULL, align = "tip",
65+
width, padding = NULL,
66+
relWidth = 0.06,
67+
relPadding = if(align == "tip") 1/200 else -1/200,
68+
hjust = if(align == "tip") 0 else 1,
5769
...) {
70+
align <- match.arg(align, c("tip", "plot"))
5871
if (dev.cur() < 2) {
5972
# It would be nice to check whether the plotting device that contains
6073
# last_plot.phylo is still the active device, but this is not possible - so
@@ -90,7 +103,9 @@ add_phylopic_tree <- function(tree, tip = names(img) %||% names(uuid) %||%
90103
img = img,
91104
name = name,
92105
uuid = uuid,
93-
x = rightEdge - width - padX,
106+
x = switch(align,
107+
tip = coords[["xx"]][leafIndex],
108+
plot = rightEdge) + padX,
94109
y = coords[["yy"]][leafIndex],
95110
hjust = hjust,
96111
width = width,

man/add_phylopic_tree.Rd

Lines changed: 27 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/add_phylopic_tree/add-individual-silhouettes-to-tree.svg

Lines changed: 29 additions & 17 deletions
Loading

tests/testthat/_snaps/add_phylopic_tree/add-vector-of-silhouettes-to-tree.svg

Lines changed: 17 additions & 17 deletions
Loading

tests/testthat/test-add_phylopic_tree.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ test_that("add_phylopic_tree works", {
4040
"mouse",
4141
name = "mus",
4242
relWidth = 0.2,
43-
relPadding = 0.8, # Should appear flush with left margin
43+
relPadding = -0.85, # Should appear flush with left margin
4444
fill = "lightblue",
4545
vjust = 1
4646
),
@@ -91,7 +91,7 @@ test_that("add_phylopic_tree works", {
9191
"mouse",
9292
uuid = "dd0a795e-4be3-4f99-a084-2427c1319d31",
9393
relWidth = 0.2,
94-
relPadding = 0.8, # Should appear flush with left margin
94+
relPadding = -0.85, # Should appear flush with left margin
9595
fill = "lightblue",
9696
vjust = 1
9797
)
@@ -100,9 +100,17 @@ test_that("add_phylopic_tree works", {
100100
"dog",
101101
uuid = "6f3ebbc6-be53-4216-b45b-946f7984669b",
102102
relWidth = 0.16,
103-
padding = -0.08, # Half off the page
103+
padding = 0.08, # Half off the page
104104
fill = "#665566"
105105
)
106+
add_phylopic_tree(
107+
tree,
108+
"mouse",
109+
uuid = "dd0a795e-4be3-4f99-a084-2427c1319d31",
110+
align = "plot", # Aligned with plot edge
111+
relWidth = 0.16,
112+
fill = "purple"
113+
)
106114
})
107115
})
108116

@@ -125,7 +133,7 @@ test_that("add_phylopic_tree works with vectors", {
125133
"dd0a795e-4be3-4f99-a084-2427c1319d31"
126134
),
127135
relWidth = c(0.1, 0.2, 0.16, 9, 9),
128-
padding = c(1/200, 0.8, -0.08, 0, 0),
136+
padding = c(-1/200, -0.85, 0.08, 0, 0),
129137
vjust = c(0.2, 1, 1, 0.5, 0.5),
130138
fill = c("brown", "lightblue", "#665566", "red", "red"),
131139
),
@@ -145,7 +153,7 @@ test_that("add_phylopic_tree works with vectors", {
145153
mouse = "dd0a795e-4be3-4f99-a084-2427c1319d31",
146154
dog = "6f3ebbc6-be53-4216-b45b-946f7984669b"),
147155
relWidth = c(0.1, 0.2, 0.16),
148-
padding = c(1/200, 0.8, -0.08),
156+
padding = c(-1/200, -0.85, 0.08),
149157
vjust = c(0.2, 1, 1),
150158
fill = c("brown", "lightblue", "#665566"),
151159
)
-1 Bytes
Loading
-2 Bytes
Loading
-2 Bytes
Loading
-2 Bytes
Loading
4 Bytes
Loading

0 commit comments

Comments
 (0)