X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=R%2Fscales.R;h=7176d1f5c6e19b1ab60ec11ff2633eb1725ec097;hb=fb6a06e39b9c580b39c76fd95e950144e818f45d;hp=614db24540e46a4b0df02f7624e21abe165ec63d;hpb=6a2fafede27a5cf6a5aafef7c9b56e78635eaeb4;p=ape.git diff --git a/R/scales.R b/R/scales.R index 614db24..7176d1f 100644 --- a/R/scales.R +++ b/R/scales.R @@ -1,16 +1,17 @@ -## scales.R (2009-10-02) +## scales.R (2011-05-31) ## Add a Scale Bar or Axis to a Phylogeny Plot ## add.scale.bar: add a scale bar to a phylogeny plot ## axisPhylo: add a scale axis on the side of a phylogeny plot -## Copyright 2002-2009 Emmanuel Paradis +## Copyright 2002-2011 Emmanuel Paradis ## This file is part of the R-package `ape'. ## See the file ../COPYING for licensing issues. -add.scale.bar <- function(x, y, length = NULL, ...) +add.scale.bar <- function(x, y, length = NULL, ask = FALSE, + lwd = 1, lcol = "black", ...) { lastPP <- get("last_plot.phylo", envir = .PlotPhyloEnv) direc <- lastPP$direction @@ -22,40 +23,52 @@ add.scale.bar <- function(x, y, length = NULL, ...) length <- eval(parse(text = paste("1e", nb.digit, sep = ""))) } - if (missing(x) || missing(y)) - switch(direc, - "rightwards" = { - x <- 0 - y <- 1 - }, - "leftwards" = { - x <- max(lastPP$xx) - y <- 1 - }, - "upwards" = { - x <- max(lastPP$xx) - y <- 0 - }, - "downwards" = { - x <- 1 - y <- max(lastPP$yy) - }) + if (ask) { + cat("\nClick where you want to draw the bar\n") + x <- unlist(locator(1)) + y <- x[2] + x <- x[1] + } else if (missing(x) || missing(y)) { + if (lastPP$type %in% c("phylogram", "cladogram")) { + switch(direc, + "rightwards" = { + x <- 0 + y <- 1 + }, + "leftwards" = { + x <- max(lastPP$xx) + y <- 1 + }, + "upwards" = { + x <- max(lastPP$xx) + y <- 0 + }, + "downwards" = { + x <- 1 + y <- max(lastPP$yy) + }) + } else { + direc <- "rightwards" # just to be sure for below + x <- lastPP$x.lim[1] + y <- lastPP$y.lim[1] + } + } switch(direc, "rightwards" = { - segments(x, y, x + length, y) + segments(x, y, x + length, y, col = lcol, lwd = lwd) text(x + length * 1.1, y, as.character(length), adj = c(0, 0.5), ...) }, "leftwards" = { - segments(x - length, y, x, y) + segments(x - length, y, x, y, col = lcol, lwd = lwd) text(x - length * 1.1, y, as.character(length), adj = c(1, 0.5), ...) }, "upwards" = { - segments(x, y, x, y + length) + segments(x, y, x, y + length, col = lcol, lwd = lwd) text(x, y + length * 1.1, as.character(length), adj = c(0, 0.5), srt = 90, ...) }, "downwards" = { - segments(x, y - length, x, y) + segments(x, y - length, x, y, col = lcol, lwd = lwd) text(x, y - length * 1.1, as.character(length), adj = c(0, 0.5), srt = 270, ...) }) }