1 ## scales.R (2009-12-16)
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-2009 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, ...)
15 lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
16 direc <- lastPP$direction
17 if (is.null(length)) {
19 if (direc %in% c("rightwards", "leftwards")) diff(range(lastPP$xx))
20 else diff(range(lastPP$yy))
21 nb.digit <- ceiling(log10(nb.digit)) - 2
22 length <- eval(parse(text = paste("1e", nb.digit, sep = "")))
26 cat("\nClick where you want to draw the bar\n")
27 x <- unlist(locator(1))
30 } else if (missing(x) || missing(y)) {
31 if (lastPP$type %in% c("phylogram", "cladogram")) {
50 direc <- "rightwards" # just to be sure for below
58 segments(x, y, x + length, y)
59 text(x + length * 1.1, y, as.character(length), adj = c(0, 0.5), ...)
62 segments(x - length, y, x, y)
63 text(x - length * 1.1, y, as.character(length), adj = c(1, 0.5), ...)
66 segments(x, y, x, y + length)
67 text(x, y + length * 1.1, as.character(length), adj = c(0, 0.5), srt = 90, ...)
70 segments(x, y - length, x, y)
71 text(x, y - length * 1.1, as.character(length), adj = c(0, 0.5), srt = 270, ...)
75 axisPhylo <- function(side = 1, ...)
77 lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv)
78 if (lastPP$type %in% c("phylogram", "cladogram")) {
79 if (lastPP$direction %in% c("rightwards", "leftwards")) {
80 x <- pretty(lastPP$xx)
81 if (lastPP$direction == "rightwards") maxi <- max(lastPP$xx)
83 maxi <- min(lastPP$xx)
87 x <- pretty(lastPP$yy)
88 if (lastPP$direction == "upwards") maxi <- max(lastPP$yy)
90 maxi <- min(lastPP$yy)
95 axis(side = side, at = c(maxi - x), labels = abs(x), ...)