]> git.donarmstrong.com Git - ape.git/blob - R/scales.R
improved root()
[ape.git] / R / scales.R
1 ## scales.R (2008-02-08)
2
3 ##   Add a Scale Bar or Axis to a Phylogeny Plot
4
5 ## add.scale.bar: add a scale bar to a phylogeny plot
6 ## axisPhylo: add a scale axis on the side of a phylogeny plot
7
8 ## Copyright 2002-2008 Emmanuel Paradis
9
10 ## This file is part of the R-package `ape'.
11 ## See the file ../COPYING for licensing issues.
12
13 add.scale.bar <- function(x = 0, y = 1, length = NULL, ...)
14 {
15     if (is.null(length)) {
16         nb.digit <- ceiling(log10(mean(get("last_plot.phylo$xx",
17                                            envir = .PlotPhyloEnv)))) - 2
18         length <- eval(parse(text = paste("1e", nb.digit, sep = "")))
19     }
20     segments(x, y, x + length, y)
21     text(x + length * 1.1, y, as.character(length), adj = c(0, 0.5), ...)
22 }
23
24 axisPhylo <- function(side = 1, ...)
25 {
26     type <- get("last_plot.phylo$type", envir = .PlotPhyloEnv)
27     direction <- get("last_plot.phylo$direction", envir = .PlotPhyloEnv)
28     if (type %in% c("phylogram", "cladogram")) {
29         if (direction %in% c("rightwards", "leftwards")) {
30             xx <- get("last_plot.phylo$xx", envir = .PlotPhyloEnv)
31             x <- pretty(xx)
32             if (direction == "rightwards") maxi <- max(xx)
33             else {
34                 maxi <- min(xx)
35                 x <- -x
36             }
37         } else {
38             yy <- get("last_plot.phylo$yy", envir = .PlotPhyloEnv)
39             x <- pretty(yy)
40             if (direction == "upwards") maxi <- max(yy)
41             else {
42                 maxi <- min(yy)
43                 x <- -x
44             }
45         }
46     }
47     axis(side = side, at = c(maxi - x), labels = abs(x), ...)
48 }