]> git.donarmstrong.com Git - ape.git/blob - R/plot.phylo.R
fe4543295491c5723a800fe43e77b86f2783c449
[ape.git] / R / plot.phylo.R
1 ## plot.phylo.R (2009-03-27)
2
3 ##   Plot Phylogenies
4
5 ## Copyright 2002-2009 Emmanuel Paradis
6
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
9
10 plot.phylo <- function(x, type = "phylogram", use.edge.length = TRUE,
11                        node.pos = NULL, show.tip.label = TRUE,
12                        show.node.label = FALSE, edge.color = "black",
13                        edge.width = 1, edge.lty = 1, font = 3, cex = par("cex"),
14                        adj = NULL, srt = 0, no.margin = FALSE,
15                        root.edge = FALSE, label.offset = 0, underscore = FALSE,
16                        x.lim = NULL, y.lim = NULL, direction = "rightwards",
17                        lab4ut = "horizontal", tip.color = "black", ...)
18 {
19     Ntip <- length(x$tip.label)
20     if (Ntip == 1) stop("found only one tip in the tree!")
21     Nedge <- dim(x$edge)[1]
22     if (any(tabulate(x$edge[, 1]) == 1))
23       stop("there are single (non-splitting) nodes in your tree; you may need to use collapse.singles().")
24     Nnode <- x$Nnode
25     ROOT <- Ntip + 1
26     type <- match.arg(type, c("phylogram", "cladogram", "fan",
27                               "unrooted", "radial"))
28     direction <- match.arg(direction, c("rightwards", "leftwards",
29                                         "upwards", "downwards"))
30     if (is.null(x$edge.length)) use.edge.length <- FALSE
31     if (type == "unrooted" || !use.edge.length) root.edge <- FALSE
32     phyloORclado <- type %in% c("phylogram", "cladogram")
33     horizontal <- direction %in% c("rightwards", "leftwards")
34     if (phyloORclado) {
35         ## we first compute the y-coordinates of the tips.
36         ## Fix from Klaus Schliep (2007-06-16):
37         if (!is.null(attr(x, "order")))
38           if (attr(x, "order") == "pruningwise")
39             x <- reorder(x)
40         ## End of fix
41         yy <- numeric(Ntip + Nnode)
42         TIPS <- x$edge[x$edge[, 2] <= Ntip, 2]
43         yy[TIPS] <- 1:Ntip
44     }
45     edge.color <- rep(edge.color, length.out = Nedge)
46     edge.width <- rep(edge.width, length.out = Nedge)
47     edge.lty <- rep(edge.lty, length.out = Nedge)
48     ## fix from Li-San Wang (2007-01-23):
49     xe <- x$edge
50     x <- reorder(x, order = "pruningwise")
51     ereorder <- match(x$edge[, 2], xe[, 2])
52     edge.color <- edge.color[ereorder]
53     edge.width <- edge.width[ereorder]
54     ## End of fix
55     if (phyloORclado) {
56         if (is.null(node.pos)) {
57             node.pos <- 1
58             if (type == "cladogram" && !use.edge.length) node.pos <- 2
59         }
60         if (node.pos == 1)
61           yy <- .C("node_height", as.integer(Ntip), as.integer(Nnode),
62                    as.integer(x$edge[, 1]), as.integer(x$edge[, 2]),
63                    as.integer(Nedge), as.double(yy),
64                    DUP = FALSE, PACKAGE = "ape")[[6]]
65         else {
66           ## node_height_clado requires the number of descendants
67           ## for each node, so we compute `xx' at the same time
68           ans <- .C("node_height_clado", as.integer(Ntip),
69                     as.integer(Nnode), as.integer(x$edge[, 1]),
70                     as.integer(x$edge[, 2]), as.integer(Nedge),
71                     double(Ntip + Nnode), as.double(yy),
72                     DUP = FALSE, PACKAGE = "ape")
73           xx <- ans[[6]] - 1
74           yy <- ans[[7]]
75         }
76         if (!use.edge.length) {
77             if(node.pos != 2)
78               xx <- .C("node_depth", as.integer(Ntip), as.integer(Nnode),
79                        as.integer(x$edge[, 1]), as.integer(x$edge[, 2]),
80                        as.integer(Nedge), double(Ntip + Nnode),
81                        DUP = FALSE, PACKAGE = "ape")[[6]] - 1
82             xx <- max(xx) - xx
83         } else  {
84               xx <- .C("node_depth_edgelength", as.integer(Ntip),
85                        as.integer(Nnode), as.integer(x$edge[, 1]),
86                        as.integer(x$edge[, 2]), as.integer(Nedge),
87                        as.double(x$edge.length), double(Ntip + Nnode),
88                        DUP = FALSE, PACKAGE = "ape")[[7]]
89         }
90     }
91     if (type == "fan") {
92         ## if the tips are not in the same order in tip.label
93         ## and in edge[, 2], we must reorder the angles: we
94         ## use `xx' to store temporarily the angles
95         TIPS <- xe[which(xe[, 2] <= Ntip), 2]
96         xx <- seq(0, 2*pi*(1 - 1/Ntip), 2*pi/Ntip)
97         theta <- double(Ntip)
98         theta[TIPS] <- xx
99         theta <- c(theta, numeric(Nnode))
100         theta <- .C("node_height", as.integer(Ntip), as.integer(Nnode),
101                   as.integer(x$edge[, 1]), as.integer(x$edge[, 2]),
102                   as.integer(Nedge), theta, DUP = FALSE,
103                   PACKAGE = "ape")[[6]]
104         if (use.edge.length) {
105             r <- .C("node_depth_edgelength", as.integer(Ntip),
106                     as.integer(Nnode), as.integer(x$edge[, 1]),
107                     as.integer(x$edge[, 2]), as.integer(Nedge),
108                     as.double(x$edge.length), double(Ntip + Nnode),
109                     DUP = FALSE, PACKAGE = "ape")[[7]]
110         } else {
111             r <- .C("node_depth", as.integer(Ntip), as.integer(Nnode),
112                     as.integer(x$edge[, 1]), as.integer(x$edge[, 2]),
113                     as.integer(Nedge), double(Ntip + Nnode),
114                     DUP = FALSE, PACKAGE = "ape")[[6]]
115             r <- 1/r
116         }
117         xx <- r*cos(theta)
118         yy <- r*sin(theta)
119
120     }
121     if (type == "unrooted") {
122         XY <- if (use.edge.length)
123           unrooted.xy(Ntip, Nnode, x$edge, x$edge.length)
124         else
125           unrooted.xy(Ntip, Nnode, x$edge, rep(1, Nedge))
126         ## rescale so that we have only positive values
127         xx <- XY$M[, 1] - min(XY$M[, 1])
128         yy <- XY$M[, 2] - min(XY$M[, 2])
129     }
130     if (type == "radial") {
131         X <- .C("node_depth", as.integer(Ntip), as.integer(Nnode),
132                 as.integer(x$edge[, 1]), as.integer(x$edge[, 2]),
133                 as.integer(Nedge), double(Ntip + Nnode),
134                 DUP = FALSE, PACKAGE = "ape")[[6]]
135         X[X == 1] <- 0
136         ## radius:
137         X <- 1 - X/Ntip
138         ## angle (1st compute the angles for the tips):
139         yy <- c((1:Ntip)*2*pi/Ntip, rep(0, Nnode))
140         Y <- .C("node_height", as.integer(Ntip), as.integer(Nnode),
141                 as.integer(x$edge[, 1]), as.integer(x$edge[, 2]),
142                 as.integer(Nedge), as.double(yy),
143                 DUP = FALSE, PACKAGE = "ape")[[6]]
144         xx <- X * cos(Y)
145         yy <- X * sin(Y)
146     }
147     if (phyloORclado && direction != "rightwards") {
148         if (direction == "leftwards") {
149             xx <- -xx
150             xx <- xx - min(xx)
151         }
152         if (!horizontal) {
153             tmp <- yy
154             yy <- xx
155             xx <- tmp - min(tmp) + 1
156             if (direction == "downwards") {
157                 yy <- -yy
158                 yy <- yy - min(yy)
159             }
160         }
161     }
162     if (phyloORclado && root.edge) {
163         if (direction == "rightwards") xx <- xx + x$root.edge
164         if (direction == "upwards") yy <- yy + x$root.edge
165     }
166     if (no.margin) par(mai = rep(0, 4))
167     if (is.null(x.lim)) {
168         if (phyloORclado) {
169             if (horizontal) {
170                 x.lim <- c(0, NA)
171                 tmp <-
172                   if (show.tip.label) nchar(x$tip.label) * 0.018 * max(xx) * cex
173                   else 0
174                 x.lim[2] <-
175                   if (direction == "leftwards") max(xx[ROOT] + tmp)
176                   else max(xx[1:Ntip] + tmp)
177             } else x.lim <- c(1, Ntip)
178         }
179         if (type == "fan") {
180             if (show.tip.label) {
181                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
182                 x.lim <- c(min(xx) - offset, max(xx) + offset)
183             } else x.lim <- c(min(xx), max(xx))
184         }
185         if (type == "unrooted") {
186             if (show.tip.label) {
187                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
188                 x.lim <- c(0 - offset, max(xx) + offset)
189             } else x.lim <- c(0, max(xx))
190         }
191         if (type == "radial") {
192             if (show.tip.label) {
193                 offset <- max(nchar(x$tip.label) * 0.03 * cex)
194                 x.lim <- c(-1 - offset, 1 + offset)
195             } else x.lim <- c(-1, 1)
196         }
197     } else if (length(x.lim) == 1) {
198         x.lim <- c(0, x.lim)
199         if (phyloORclado && !horizontal) x.lim[1] <- 1
200         if (type %in% c("fan", "unrooted") && show.tip.label)
201           x.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
202         if (type == "radial")
203           x.lim[1] <-
204             if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.03 * cex)
205             else -1
206     }
207     if (is.null(y.lim)) {
208         if (phyloORclado) {
209             if (horizontal) y.lim <- c(1, Ntip) else {
210                 y.lim <- c(0, NA)
211                 tmp <-
212                   if (show.tip.label) nchar(x$tip.label) * 0.018 * max(yy) * cex
213                   else 0
214                 y.lim[2] <-
215                   if (direction == "downwards") max(yy[ROOT] + tmp)
216                   else max(yy[1:Ntip] + tmp)
217             }
218         }
219         if (type == "fan") {
220             if (show.tip.label) {
221                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
222                 y.lim <- c(min(yy) - offset, max(yy) + offset)
223             } else y.lim <- c(min(yy), max(yy))
224         }
225         if (type == "unrooted") {
226             if (show.tip.label) {
227                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
228                 y.lim <- c(0 - offset, max(yy) + offset)
229             } else y.lim <- c(0, max(yy))
230         }
231         if (type == "radial") {
232             if (show.tip.label) {
233                 offset <- max(nchar(x$tip.label) * 0.03 * cex)
234                 y.lim <- c(-1 - offset, 1 + offset)
235             } else y.lim <- c(-1, 1)
236         }
237     } else if (length(y.lim) == 1) {
238         y.lim <- c(0, y.lim)
239         if (phyloORclado && horizontal) y.lim[1] <- 1
240         if (type %in% c("fan", "unrooted") && show.tip.label)
241           y.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
242         if (type == "radial")
243           y.lim[1] <- if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.018 * max(yy) * cex) else -1
244     }
245     if (phyloORclado && root.edge) {
246         if (direction == "leftwards") x.lim[2] <- x.lim[2] + x$root.edge
247         if (direction == "downwards") y.lim[2] <- y.lim[2] + x$root.edge
248     }
249     ## fix by Klaus Schliep (2008-03-28):
250     asp <- if (type %in% c("fan", "radial")) 1 else NA
251     plot(0, type = "n", xlim = x.lim, ylim = y.lim, xlab = "",
252          ylab = "", xaxt = "n", yaxt = "n", bty = "n", asp = asp, ...)
253     if (is.null(adj))
254       adj <- if (phyloORclado && direction == "leftwards") 1 else 0
255     if (phyloORclado) {
256         MAXSTRING <- max(strwidth(x$tip.label, cex = cex))
257         if (direction == "rightwards") {
258             lox <- label.offset + MAXSTRING * 1.05 * adj
259             loy <- 0
260         }
261         if (direction == "leftwards") {
262             lox <- -label.offset - MAXSTRING * 1.05 * (1 - adj)
263             loy <- 0
264             xx <- xx + MAXSTRING
265         }
266         if (!horizontal) {
267             psr <- par("usr")
268             MAXSTRING <- MAXSTRING * 1.09 * (psr[4] - psr[3]) / (psr[2] - psr[1])
269             loy <- label.offset + MAXSTRING * 1.05 * adj
270             lox <- 0
271             srt <- 90 + srt
272             if (direction == "downwards") {
273                 loy <- -loy
274                 yy <- yy + MAXSTRING
275                 srt <- 180 + srt
276             }
277         }
278     }
279     if (type == "phylogram") {
280         phylogram.plot(x$edge, Ntip, Nnode, xx, yy,
281                        horizontal, edge.color, edge.width, edge.lty)
282     } else {
283       if (type == "fan")
284         circular.plot(x$edge, Ntip, Nnode, xx, yy, theta,
285                       r, edge.color, edge.width, edge.lty)
286       else
287         cladogram.plot(x$edge, xx, yy, edge.color, edge.width, edge.lty)
288     }
289     if (root.edge)
290       switch(direction,
291              "rightwards" = segments(0, yy[ROOT], x$root.edge, yy[ROOT]),
292              "leftwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT] + x$root.edge, yy[ROOT]),
293              "upwards" = segments(xx[ROOT], 0, xx[ROOT], x$root.edge),
294              "downwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT], yy[ROOT] + x$root.edge))
295     if (show.tip.label) {
296         if (!underscore) x$tip.label <- gsub("_", " ", x$tip.label)
297         if (phyloORclado) {
298             text(xx[1:Ntip] + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj,
299                  font = font, srt = srt, cex = cex, col = tip.color)
300         }
301         if (type == "unrooted") {
302             if (lab4ut == "horizontal") {
303                 y.adj <- x.adj <- numeric(Ntip)
304                 sel <- abs(XY$axe) > 0.75 * pi
305                 x.adj[sel] <- -strwidth(x$tip.label)[sel] * 1.05
306                 sel <- abs(XY$axe) > pi/4 & abs(XY$axe) < 0.75 * pi
307                 x.adj[sel] <- -strwidth(x$tip.label)[sel] * (2 * abs(XY$axe)[sel] / pi - 0.5)
308                 sel <- XY$axe > pi / 4 & XY$axe < 0.75 * pi
309                 y.adj[sel] <- strheight(x$tip.label)[sel] / 2
310                 sel <- XY$axe < -pi / 4 & XY$axe > -0.75 * pi
311                 y.adj[sel] <- -strheight(x$tip.label)[sel] * 0.75
312                 text(xx[1:Ntip] + x.adj*cex, yy[1:Ntip] + y.adj*cex,
313                      x$tip.label, adj = c(adj, 0), font = font,
314                      srt = srt, cex = cex, col = tip.color)
315             } else { # if lab4ut == "axial"
316                 adj <- as.numeric(abs(XY$axe) > pi/2)
317                 srt <- 180*XY$axe/pi
318                 srt[as.logical(adj)] <- srt[as.logical(adj)] - 180
319                 ## <FIXME> temporary check of the values of `srt':
320                 ## set to 0 if "-0.000001 < srt < 0"
321                 sel <- srt > -1e-6 & srt < 0
322                 if (any(sel)) srt[sel] <- 0
323                 ## </FIXME>
324                 ## `srt' takes only a single value, so we cannot vectorize this:
325                 for (i in 1:Ntip)
326                   text(xx[i], yy[i], cex = cex, x$tip.label[i], adj = adj[i],
327                        font = font, srt = srt[i], col = tip.color[i])
328             }
329         }
330         if (type %in% c("fan", "radial")) {
331             xx.scaled <- xx[1:Ntip]
332             if (type == "fan") { # no need if type == "radial"
333                 maxx <- max(abs(xx.scaled))
334                 if (maxx > 1) xx.scaled <- xx.scaled/maxx
335             }
336             angle <- acos(xx.scaled)*180/pi
337             s1 <- angle > 90 & yy[1:Ntip] > 0
338             s2 <- angle < 90 & yy[1:Ntip] < 0
339             s3 <- angle > 90 & yy[1:Ntip] < 0
340             angle[s1] <- angle[s1] + 180
341             angle[s2] <- -angle[s2]
342             angle[s3] <- 180 - angle[s3]
343             adj <- numeric(Ntip)
344             adj[xx[1:Ntip] < 0] <- 1
345             ## `srt' takes only a single value, so we cannot vectorize this:
346             for (i in 1:Ntip)
347               text(xx[i], yy[i], x$tip.label[i], font = font, cex = cex,
348                    srt = angle[i], adj = adj[i], col = tip.color[i])
349         }
350     }
351     if (show.node.label)
352       text(xx[ROOT:length(xx)] + label.offset, yy[ROOT:length(yy)],
353            x$node.label, adj = adj, font = font, srt = srt, cex = cex)
354     L <- list(type = type, use.edge.length = use.edge.length,
355               node.pos = node.pos, show.tip.label = show.tip.label,
356               show.node.label = show.node.label, font = font,
357               cex = cex, adj = adj, srt = srt, no.margin = no.margin,
358               label.offset = label.offset, x.lim = x.lim, y.lim = y.lim,
359               direction = direction, tip.color = tip.color,
360               Ntip = Ntip, Nnode = Nnode)
361     assign("last_plot.phylo", c(L, list(edge = xe, xx = xx, yy = yy)),
362            envir = .PlotPhyloEnv)
363     invisible(L)
364 }
365
366 phylogram.plot <- function(edge, Ntip, Nnode, xx, yy, horizontal,
367                            edge.color, edge.width, edge.lty)
368 {
369     nodes <- (Ntip + 1):(Ntip + Nnode)
370     if (!horizontal) {
371         tmp <- yy
372         yy <- xx
373         xx <- tmp
374     }
375     ## un trait vertical à chaque noeud...
376     x0v <- xx[nodes]
377     y0v <- y1v <- numeric(Nnode)
378     for (i in nodes) {
379         j <- edge[which(edge[, 1] == i), 2]
380         y0v[i - Ntip] <- min(yy[j])
381         y1v[i - Ntip] <- max(yy[j])
382     }
383     ## ... et un trait horizontal partant de chaque tip et chaque noeud
384     ##  vers la racine
385     sq <- if (Nnode == 1) 1:Ntip else c(1:Ntip, nodes[-1])
386     y0h <- yy[sq]
387     x1h <- xx[sq]
388     ## match() is very useful here becoz each element in edge[, 2] is
389     ## unique (not sure this is so useful in edge[, 1]; needs to be checked)
390     ## `pos' gives for each element in `sq' its index in edge[, 2]
391     pos <- match(sq, edge[, 2])
392     x0h <- xx[edge[pos, 1]]
393
394     ## function dispatching the features to the vertical edges
395     foo <- function(edge.feat, default) {
396         e <- unique(edge.feat)
397         if (length(e) == 1) return(rep(e, Nnode)) else {
398             feat.v <- rep(default, Nnode)
399             for (i in 1:Nnode) {
400                 br <- which(edge[, 1] == i + Ntip)
401                 x <- unique(edge.feat[br])
402                 if (length(x) == 1) feat.v[i] <- x
403             }
404         }
405         feat.v
406     }
407     color.v <- foo(edge.color, "black")
408     width.v <- foo(edge.width, 1)
409     lty.v <- foo(edge.lty, 1)
410
411     ## we need to reorder:
412     edge.width <- edge.width[pos]
413     edge.color <- edge.color[pos]
414     edge.lty <- edge.lty[pos]
415     if (horizontal) {
416         segments(x0v, y0v, x0v, y1v, col = color.v, lwd = width.v, lty = lty.v) # draws vertical lines
417         segments(x0h, y0h, x1h, y0h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws horizontal lines
418     } else {
419         segments(y0v, x0v, y1v, x0v, col = color.v, lwd = width.v, lty = lty.v) # draws horizontal lines
420         segments(y0h, x0h, y0h, x1h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws vertical lines
421     }
422 }
423
424 cladogram.plot <- function(edge, xx, yy, edge.color, edge.width, edge.lty)
425   segments(xx[edge[, 1]], yy[edge[, 1]], xx[edge[, 2]], yy[edge[, 2]],
426            col = edge.color, lwd = edge.width, lty = edge.lty)
427
428 circular.plot <- function(edge, Ntip, Nnode, xx, yy, theta,
429                           r, edge.color, edge.width, edge.lty)
430 {
431     r0 <- r[edge[, 1]]
432     r1 <- r[edge[, 2]]
433     theta0 <- theta[edge[, 2]]
434
435     x0 <- r0*cos(theta0)
436     y0 <- r0*sin(theta0)
437     x1 <- r1*cos(theta0)
438     y1 <- r1*sin(theta0)
439
440     segments(x0, y0, x1, y1, col = edge.color, lwd = edge.width, lty = edge.lty)
441
442     tmp <- which(diff(edge[, 1]) != 0)
443     start <- c(1, tmp + 1)
444     end <- c(tmp, dim(edge)[1])
445
446     for (k in 1:Nnode) {
447         i <- start[k]
448         j <- end[k]
449         X <- rep(r[edge[i, 1]], 100)
450         Y <- seq(theta[edge[i, 2]], theta[edge[j, 2]], length.out = 100)
451         co <- if (edge.color[i] == edge.color[j]) edge.color[i] else "black"
452         lw <- if (edge.width[i] == edge.width[j]) edge.width[i] else 1
453         ly <- if (edge.lty[i] == edge.lty[j]) edge.lty[i] else 1
454         lines(X*cos(Y), X*sin(Y), col = co, lwd = lw, lty = ly)
455     }
456 }
457
458 unrooted.xy <- function(Ntip, Nnode, edge, edge.length)
459 {
460     foo <- function(node, ANGLE, AXIS) {
461         ind <- which(edge[, 1] == node)
462         sons <- edge[ind, 2]
463         start <- AXIS - ANGLE/2
464         for (i in 1:length(sons)) {
465             h <- edge.length[ind[i]]
466             angle[sons[i]] <<- alpha <- ANGLE*nb.sp[sons[i]]/nb.sp[node]
467             axis[sons[i]] <<- beta <- start + alpha/2
468             start <- start + alpha
469             xx[sons[i]] <<- h*cos(beta) + xx[node]
470             yy[sons[i]] <<- h*sin(beta) + yy[node]
471         }
472         for (i in sons)
473           if (i > Ntip) foo(i, angle[i], axis[i])
474     }
475     root <- Ntip + 1
476     Nedge <- dim(edge)[1]
477     yy <- xx <- numeric(Ntip + Nnode)
478     nb.sp <- .C("node_depth", as.integer(Ntip), as.integer(Nnode),
479                 as.integer(edge[, 1]), as.integer(edge[, 2]),
480                 as.integer(Nedge), double(Ntip + Nnode),
481                 DUP = FALSE, PACKAGE = "ape")[[6]]
482     ## `angle': the angle allocated to each node wrt their nb of tips
483     ## `axis': the axis of each branch
484     axis <- angle <- numeric(Ntip + Nnode)
485     ## start with the root...
486     ## xx[root] <- yy[root] <- 0 # already set!
487     foo(root, 2*pi, 0)
488
489     M <- cbind(xx, yy)
490     axe <- axis[1:Ntip] # the axis of the terminal branches (for export)
491     axeGTpi <- axe > pi
492     ## insures that returned angles are in [-PI, +PI]:
493     axe[axeGTpi] <- axe[axeGTpi] - 2*pi
494     list(M = M, axe = axe)
495 }
496
497 node.depth <- function(phy)
498 {
499     n <- length(phy$tip.label)
500     m <- phy$Nnode
501     N <- dim(phy$edge)[1]
502     phy <- reorder(phy, order = "pruningwise")
503     .C("node_depth", as.integer(n), as.integer(m),
504        as.integer(phy$edge[, 1]), as.integer(phy$edge[, 2]),
505        as.integer(N), double(n + m), DUP = FALSE, PACKAGE = "ape")[[6]]
506 }
507
508 plot.multiPhylo <- function(x, layout = 1, ...)
509 {
510     if (layout > 1)
511       layout(matrix(1:layout, ceiling(sqrt(layout)), byrow = TRUE))
512     else layout(matrix(1))
513     if (!par("ask")) {
514         par(ask = TRUE)
515         on.exit(par(ask = FALSE))
516     }
517     for (i in 1:length(x)) plot(x[[i]], ...)
518 }