]> git.donarmstrong.com Git - ape.git/blob - R/scales.R
c8da7e79ec54692c272fcd7a1851ca5293cf1bb6
[ape.git] / R / scales.R
1 ## scales.R (2008-02-28)
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     lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
16     if (is.null(length)) {
17         nb.digit <- ceiling(log10(mean(lastPP$xx))) - 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     lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
27     if (lastPP$type %in% c("phylogram", "cladogram")) {
28         if (lastPP$direction %in% c("rightwards", "leftwards")) {
29             x <- pretty(lastPP$xx)
30             if (lastPP$direction == "rightwards") maxi <- max(lastPP$xx)
31             else {
32                 maxi <- min(lastPP$xx)
33                 x <- -x
34             }
35         } else {
36             x <- pretty(lastPP$yy)
37             if (lastPP$direction == "upwards") maxi <- max(lastPP$yy)
38             else {
39                 maxi <- min(lastPP$yy)
40                 x <- -x
41             }
42         }
43     }
44     axis(side = side, at = c(maxi - x), labels = abs(x), ...)
45 }