1 ## scales.R (2011-05-31)
3 ## Add a Scale Bar or Axis to a Phylogeny Plot
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
8 ## Copyright 2002-2011 Emmanuel Paradis
10 ## This file is part of the R-package `ape'.
11 ## See the file ../COPYING for licensing issues.
13 add.scale.bar <- function(x, y, length = NULL, ask = FALSE,
14 lwd = 1, lcol = "black", ...)
16 lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
17 direc <- lastPP$direction
18 if (is.null(length)) {
20 if (direc %in% c("rightwards", "leftwards")) diff(range(lastPP$xx))
21 else diff(range(lastPP$yy))
22 nb.digit <- ceiling(log10(nb.digit)) - 2
23 length <- eval(parse(text = paste("1e", nb.digit, sep = "")))
27 cat("\nClick where you want to draw the bar\n")
28 x <- unlist(locator(1))
31 } else if (missing(x) || missing(y)) {
32 if (lastPP$type %in% c("phylogram", "cladogram")) {
51 direc <- "rightwards" # just to be sure for below
59 segments(x, y, x + length, y, col = lcol, lwd = lwd)
60 text(x + length * 1.1, y, as.character(length), adj = c(0, 0.5), ...)
63 segments(x - length, y, x, y, col = lcol, lwd = lwd)
64 text(x - length * 1.1, y, as.character(length), adj = c(1, 0.5), ...)
67 segments(x, y, x, y + length, col = lcol, lwd = lwd)
68 text(x, y + length * 1.1, as.character(length), adj = c(0, 0.5), srt = 90, ...)
71 segments(x, y - length, x, y, col = lcol, lwd = lwd)
72 text(x, y - length * 1.1, as.character(length), adj = c(0, 0.5), srt = 270, ...)
76 axisPhylo <- function(side = 1, ...)
78 lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
79 if (lastPP$type %in% c("phylogram", "cladogram")) {
80 if (lastPP$direction %in% c("rightwards", "leftwards")) {
81 x <- pretty(lastPP$xx)
82 if (lastPP$direction == "rightwards") maxi <- max(lastPP$xx)
84 maxi <- min(lastPP$xx)
88 x <- pretty(lastPP$yy)
89 if (lastPP$direction == "upwards") maxi <- max(lastPP$yy)
91 maxi <- min(lastPP$yy)
96 axis(side = side, at = c(maxi - x), labels = abs(x), ...)