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 ,
0 commit comments