]> git.donarmstrong.com Git - ape.git/blob - R/plot.phylo.R
some big fixes for ape 2.7-1
[ape.git] / R / plot.phylo.R
1 ## plot.phylo.R (2011-02-17)
2
3 ##   Plot Phylogenies
4
5 ## Copyright 2002-2011 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) {
21         warning("found only one tip in the tree")
22         return(NULL)
23     }
24     if (any(tabulate(x$edge[, 1]) == 1))
25       stop("there are single (non-splitting) nodes in your tree; you may need to use collapse.singles()")
26
27     .nodeHeight <- function(Ntip, Nnode, edge, Nedge, yy)
28         .C("node_height", as.integer(Ntip), as.integer(Nnode),
29            as.integer(edge[, 1]), as.integer(edge[, 2]),
30            as.integer(Nedge), as.double(yy),
31            DUP = FALSE, PACKAGE = "ape")[[6]]
32
33     .nodeDepth <- function(Ntip, Nnode, edge, Nedge)
34         .C("node_depth", as.integer(Ntip), as.integer(Nnode),
35            as.integer(edge[, 1]), as.integer(edge[, 2]),
36            as.integer(Nedge), double(Ntip + Nnode),
37            DUP = FALSE, PACKAGE = "ape")[[6]]
38
39     .nodeDepthEdgelength <- function(Ntip, Nnode, edge, Nedge, edge.length)
40         .C("node_depth_edgelength", as.integer(Ntip),
41            as.integer(Nnode), as.integer(edge[, 1]),
42            as.integer(edge[, 2]), as.integer(Nedge),
43            as.double(edge.length), double(Ntip + Nnode),
44            DUP = FALSE, PACKAGE = "ape")[[7]]
45
46     Nedge <- dim(x$edge)[1]
47     Nnode <- x$Nnode
48     ROOT <- Ntip + 1
49     type <- match.arg(type, c("phylogram", "cladogram", "fan",
50                               "unrooted", "radial"))
51     direction <- match.arg(direction, c("rightwards", "leftwards",
52                                         "upwards", "downwards"))
53     if (is.null(x$edge.length)) use.edge.length <- FALSE
54
55     ## the order of the last two conditions is important:
56     if (type %in% c("unrooted", "radial") || !use.edge.length ||
57         is.null(x$root.edge) || !x$root.edge) root.edge <- FALSE
58     if (type == "fan" && root.edge) {
59         warning("drawing root edge with type = 'fan' is not yet supported")
60         root.edge <- FALSE
61     }
62
63     phyloORclado <- type %in% c("phylogram", "cladogram")
64     horizontal <- direction %in% c("rightwards", "leftwards")
65     xe <- x$edge # to save
66     if (phyloORclado) {
67         ## we first compute the y-coordinates of the tips.
68         phyOrder <- attr(x, "order")
69         ## make sure the tree is in cladewise order:
70         if (is.null(phyOrder) || phyOrder != "cladewise") {
71             x <- reorder(x) # fix from Klaus Schliep (2007-06-16)
72             if (!identical(x$edge, xe)) {
73                 ## modified from Li-San Wang's fix (2007-01-23):
74                 ereorder <- match(x$edge[, 2], xe[, 2])
75                 if (length(edge.color) > 1) {
76                     edge.color <- rep(edge.color, length.out = Nedge)
77                     edge.color <- edge.color[ereorder]
78                 }
79                 if (length(edge.width) > 1) {
80                     edge.width <- rep(edge.width, length.out = Nedge)
81                     edge.width <- edge.width[ereorder]
82                 }
83                 if (length(edge.lty) > 1) {
84                     edge.lty <- rep(edge.lty, length.out = Nedge)
85                     edge.lty <- edge.lty[ereorder]
86                 }
87             }
88         }
89 ### By contrats to ape (< 2.4), the arguments edge.color, etc., are
90 ### not elongated before being passed to segments(), except if needed
91 ### to be reordered
92         yy <- numeric(Ntip + Nnode)
93         TIPS <- x$edge[x$edge[, 2] <= Ntip, 2]
94         yy[TIPS] <- 1:Ntip
95     }
96     ## 'z' is the tree in pruningwise order used in calls to .C
97     z <- reorder(x, order = "pruningwise")
98 ###    edge.color <- rep(edge.color, length.out = Nedge)
99 ###    edge.width <- rep(edge.width, length.out = Nedge)
100 ###    edge.lty <- rep(edge.lty, length.out = Nedge)
101 ###    ## fix from Li-San Wang (2007-01-23):
102 ###    xe <- x$edge
103 ###    x <- reorder(x, order = "pruningwise")
104 ###    ereorder <- match(x$edge[, 2], xe[, 2])
105 ###    edge.color <- edge.color[ereorder]
106 ###    edge.width <- edge.width[ereorder]
107 ###    edge.lty <- edge.lty[ereorder]
108 ###    ## end of fix
109     if (phyloORclado) {
110         if (is.null(node.pos)) {
111             node.pos <- 1
112             if (type == "cladogram" && !use.edge.length) node.pos <- 2
113         }
114         if (node.pos == 1)
115             yy <- .nodeHeight(Ntip, Nnode, z$edge, Nedge, yy)
116         else {
117           ## node_height_clado requires the number of descendants
118           ## for each node, so we compute `xx' at the same time
119           ans <- .C("node_height_clado", as.integer(Ntip),
120                     as.integer(Nnode), as.integer(z$edge[, 1]),
121                     as.integer(z$edge[, 2]), as.integer(Nedge),
122                     double(Ntip + Nnode), as.double(yy),
123                     DUP = FALSE, PACKAGE = "ape")
124           xx <- ans[[6]] - 1
125           yy <- ans[[7]]
126         }
127         if (!use.edge.length) {
128             if (node.pos != 2) xx <- .nodeDepth(Ntip, Nnode, z$edge, Nedge) - 1
129             xx <- max(xx) - xx
130         } else  {
131             xx <- .nodeDepthEdgelength(Ntip, Nnode, z$edge, Nedge, z$edge.length)
132         }
133     } else switch(type, "fan" = {
134         ## if the tips are not in the same order in tip.label
135         ## and in edge[, 2], we must reorder the angles: we
136         ## use `xx' to store temporarily the angles
137         TIPS <- x$edge[which(x$edge[, 2] <= Ntip), 2]
138         xx <- seq(0, 2*pi*(1 - 1/Ntip), 2*pi/Ntip)
139         theta <- double(Ntip)
140         theta[TIPS] <- xx
141         theta <- c(theta, numeric(Nnode))
142         theta <- .nodeHeight(Ntip, Nnode, z$edge, Nedge, theta)
143         if (use.edge.length) {
144             r <- .nodeDepthEdgelength(Ntip, Nnode, z$edge, Nedge, z$edge.length)
145         } else {
146             r <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
147             r <- 1/r
148         }
149         xx <- r*cos(theta)
150         yy <- r*sin(theta)
151     }, "unrooted" = {
152         nb.sp <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
153         XY <- if (use.edge.length)
154             unrooted.xy(Ntip, Nnode, z$edge, z$edge.length, nb.sp)
155         else
156             unrooted.xy(Ntip, Nnode, z$edge, rep(1, Nedge), nb.sp)
157         ## rescale so that we have only positive values
158         xx <- XY$M[, 1] - min(XY$M[, 1])
159         yy <- XY$M[, 2] - min(XY$M[, 2])
160     }, "radial" = {
161         X <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
162         X[X == 1] <- 0
163         ## radius:
164         X <- 1 - X/Ntip
165         ## angle (1st compute the angles for the tips):
166         yy <- c((1:Ntip)*2*pi/Ntip, rep(0, Nnode))
167         Y <- .nodeHeight(Ntip, Nnode, z$edge, Nedge, yy)
168         xx <- X * cos(Y)
169         yy <- X * sin(Y)
170     })
171     if (phyloORclado) {
172         if (!horizontal) {
173             tmp <- yy
174             yy <- xx
175             xx <- tmp - min(tmp) + 1
176         }
177         if (root.edge) {
178             if (direction == "rightwards") xx <- xx + x$root.edge
179             if (direction == "upwards") yy <- yy + x$root.edge
180         }
181     }
182     if (no.margin) par(mai = rep(0, 4))
183     if (is.null(x.lim)) {
184         if (phyloORclado) {
185             if (horizontal) {
186                 x.lim <- c(0, NA)
187                 pin1 <- par("pin")[1] # width of the device in inches
188                 strWi <- strwidth(x$tip.label, "inches") # id. for the tip labels
189                 ## 1.04 comes from that we are using a regular axis system
190                 ## with 4% on both sides of the range of x:
191                 xx.tips <- xx[1:Ntip] * 1.04
192                 ## 'alp' is the conversion coefficient from
193                 ## user coordinates to inches:
194                 alp <- try(uniroot(function(a) max(a*xx.tips + strWi) - pin1,
195                                    c(0, 1e6))$root, silent = TRUE)
196                 ## if the above fails, give 1/3 of the device for the tip labels:
197                 if (is.character(alp)) tmp <- max(xx.tips)*1.5 else {
198                     tmp <- if (show.tip.label) max(xx.tips + strWi/alp) else max(xx.tips)
199                 }
200                 x.lim[2] <- tmp
201             } else x.lim <- c(1, Ntip)
202         } else switch(type, "fan" = {
203             if (show.tip.label) {
204                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
205                 x.lim <- c(min(xx) - offset, max(xx) + offset)
206             } else x.lim <- c(min(xx), max(xx))
207         }, "unrooted" = {
208             if (show.tip.label) {
209                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
210                 x.lim <- c(0 - offset, max(xx) + offset)
211             } else x.lim <- c(0, max(xx))
212         }, "radial" = {
213             if (show.tip.label) {
214                 offset <- max(nchar(x$tip.label) * 0.03 * cex)
215                 x.lim <- c(-1 - offset, 1 + offset)
216             } else x.lim <- c(-1, 1)
217         })
218     } else if (length(x.lim) == 1) {
219         x.lim <- c(0, x.lim)
220         if (phyloORclado && !horizontal) x.lim[1] <- 1
221         if (type %in% c("fan", "unrooted") && show.tip.label)
222           x.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
223         if (type == "radial")
224           x.lim[1] <-
225             if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.03 * cex)
226             else -1
227     }
228     ## mirror the xx:
229     if (phyloORclado && direction == "leftwards") xx <- x.lim[2] - xx
230     if (is.null(y.lim)) {
231         if (phyloORclado) {
232             if (horizontal) y.lim <- c(1, Ntip) else {
233                 y.lim <- c(0, NA)
234                 pin2 <- par("pin")[2] # height of the device in inches
235                 strWi <- strwidth(x$tip.label, "inches")
236                 ## 1.04 comes from that we are using a regular axis system
237                 ## with 4% on both sides of the range of x:
238                 yy.tips <- yy[1:Ntip] * 1.04
239                 ## 'alp' is the conversion coefficient from
240                 ## user coordinates to inches:
241                 alp <- try(uniroot(function(a) max(a*yy.tips + strWi) - pin2,
242                                    c(0, 1e6))$root, silent = TRUE)
243                 ## if the above fails, give 1/3 of the device for the tip labels:
244                 if (is.character(alp)) tmp <- max(yy.tips)*1.5 else {
245                     tmp <- if (show.tip.label) max(yy.tips + strWi/alp) else max(yy.tips)
246                 }
247                 y.lim[2] <- tmp
248             }
249         } else switch(type, "fan" = {
250             if (show.tip.label) {
251                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
252                 y.lim <- c(min(yy) - offset, max(yy) + offset)
253             } else y.lim <- c(min(yy), max(yy))
254         }, "unrooted" = {
255             if (show.tip.label) {
256                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
257                 y.lim <- c(0 - offset, max(yy) + offset)
258             } else y.lim <- c(0, max(yy))
259         }, "radial" = {
260             if (show.tip.label) {
261                 offset <- max(nchar(x$tip.label) * 0.03 * cex)
262                 y.lim <- c(-1 - offset, 1 + offset)
263             } else y.lim <- c(-1, 1)
264         })
265     } else if (length(y.lim) == 1) {
266         y.lim <- c(0, y.lim)
267         if (phyloORclado && horizontal) y.lim[1] <- 1
268         if (type %in% c("fan", "unrooted") && show.tip.label)
269           y.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
270         if (type == "radial")
271           y.lim[1] <- if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.018 * max(yy) * cex) else -1
272     }
273     ## mirror the yy:
274     if (phyloORclado && direction == "downwards") yy <- y.lim[2] - yy
275     if (phyloORclado && root.edge) {
276         if (direction == "leftwards") x.lim[2] <- x.lim[2] + x$root.edge
277         if (direction == "downwards") y.lim[2] <- y.lim[2] + x$root.edge
278     }
279     asp <- if (type %in% c("fan", "radial", "unrooted")) 1 else NA # fixes by Klaus Schliep (2008-03-28 and 2010-08-12)
280     plot(0, type = "n", xlim = x.lim, ylim = y.lim, ann = FALSE, axes = FALSE, asp = asp, ...)
281     if (is.null(adj))
282         adj <- if (phyloORclado && direction == "leftwards") 1 else 0
283     if (phyloORclado && show.tip.label) {
284         MAXSTRING <- max(strwidth(x$tip.label, cex = cex))
285         loy <- 0
286         if (direction == "rightwards") {
287             lox <- label.offset + MAXSTRING * 1.05 * adj
288         }
289         if (direction == "leftwards") {
290             lox <- -label.offset - MAXSTRING * 1.05 * (1 - adj)
291             #xx <- xx + MAXSTRING
292         }
293         if (!horizontal) {
294             psr <- par("usr")
295             MAXSTRING <- MAXSTRING * 1.09 * (psr[4] - psr[3])/(psr[2] - psr[1])
296             loy <- label.offset + MAXSTRING * 1.05 * adj
297             lox <- 0
298             srt <- 90 + srt
299             if (direction == "downwards") {
300                 loy <- -loy
301                 ##yy <- yy + MAXSTRING
302                 srt <- 180 + srt
303             }
304         }
305     }
306     if (type == "phylogram") {
307         phylogram.plot(x$edge, Ntip, Nnode, xx, yy,
308                        horizontal, edge.color, edge.width, edge.lty)
309     } else {
310         if (type == "fan") {
311             ereorder <- match(z$edge[, 2], x$edge[, 2])
312             if (length(edge.color) > 1) {
313                 edge.color <- rep(edge.color, length.out = Nedge)
314                 edge.color <- edge.color[ereorder]
315             }
316             if (length(edge.width) > 1) {
317                 edge.width <- rep(edge.width, length.out = Nedge)
318                 edge.width <- edge.width[ereorder]
319             }
320             if (length(edge.lty) > 1) {
321                 edge.lty <- rep(edge.lty, length.out = Nedge)
322                 edge.lty <- edge.lty[ereorder]
323             }
324             circular.plot(z$edge, Ntip, Nnode, xx, yy, theta,
325                           r, edge.color, edge.width, edge.lty)
326         } else
327         cladogram.plot(x$edge, xx, yy, edge.color, edge.width, edge.lty)
328     }
329     if (root.edge)
330       switch(direction,
331              "rightwards" = segments(0, yy[ROOT], x$root.edge, yy[ROOT]),
332              "leftwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT] + x$root.edge, yy[ROOT]),
333              "upwards" = segments(xx[ROOT], 0, xx[ROOT], x$root.edge),
334              "downwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT], yy[ROOT] + x$root.edge))
335     if (show.tip.label) {
336         if (is.expression(x$tip.label)) underscore <- TRUE
337         if (!underscore) x$tip.label <- gsub("_", " ", x$tip.label)
338
339         if (phyloORclado)
340             text(xx[1:Ntip] + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj,
341                  font = font, srt = srt, cex = cex, col = tip.color)
342
343         if (type == "unrooted") {
344             if (lab4ut == "horizontal") {
345                 y.adj <- x.adj <- numeric(Ntip)
346                 sel <- abs(XY$axe) > 0.75 * pi
347                 x.adj[sel] <- -strwidth(x$tip.label)[sel] * 1.05
348                 sel <- abs(XY$axe) > pi/4 & abs(XY$axe) < 0.75 * pi
349                 x.adj[sel] <- -strwidth(x$tip.label)[sel] * (2 * abs(XY$axe)[sel] / pi - 0.5)
350                 sel <- XY$axe > pi / 4 & XY$axe < 0.75 * pi
351                 y.adj[sel] <- strheight(x$tip.label)[sel] / 2
352                 sel <- XY$axe < -pi / 4 & XY$axe > -0.75 * pi
353                 y.adj[sel] <- -strheight(x$tip.label)[sel] * 0.75
354                 text(xx[1:Ntip] + x.adj * cex, yy[1:Ntip] + y.adj * cex,
355                      x$tip.label, adj = c(adj, 0), font = font,
356                      srt = srt, cex = cex, col = tip.color)
357             } else { # if lab4ut == "axial"
358                 adj <- abs(XY$axe) > pi/2
359                 srt <- 180 * XY$axe / pi
360                 srt[adj] <- srt[adj] - 180
361                 adj <- as.numeric(adj)
362                 xx.tips <- xx[1:Ntip]
363                 yy.tips <- yy[1:Ntip]
364                 if (label.offset) {
365                     xx.tips <- xx.tips + label.offset * cos(XY$axe)
366                     yy.tips <- yy.tips + label.offset * sin(XY$axe)
367                 }
368                 ## `srt' takes only a single value, so can't vectorize this:
369                 ## (and need to 'elongate' these vectors:)
370                 font <- rep(font, length.out = Ntip)
371                 tip.color <- rep(tip.color, length.out = Ntip)
372                 cex <- rep(cex, length.out = Ntip)
373                 for (i in 1:Ntip)
374                     text(xx.tips[i], yy.tips[i], cex = cex[i],
375                          x$tip.label[i], adj = adj[i], font = font[i],
376                          srt = srt[i], col = tip.color[i])
377             }
378         }
379         if (type %in% c("fan", "radial")) {
380             xx.tips <- xx[1:Ntip]
381             yy.tips <- yy[1:Ntip]
382             ## using atan2 considerably facilitates things compared to acos...
383             angle <- atan2(yy.tips, xx.tips) # in radians
384             if (label.offset) {
385                 xx.tips <- xx.tips + label.offset * cos(angle)
386                 yy.tips <- yy.tips + label.offset * sin(angle)
387             }
388             s <- xx.tips < 0
389             angle <- angle * 180/pi # switch to degrees
390             angle[s] <- angle[s] + 180
391             adj <- as.numeric(s)
392             ## `srt' takes only a single value, so can't vectorize this:
393             ## (and need to 'elongate' these vectors:)
394             font <- rep(font, length.out = Ntip)
395             tip.color <- rep(tip.color, length.out = Ntip)
396             cex <- rep(cex, length.out = Ntip)
397             for (i in 1:Ntip)
398                 text(xx.tips[i], yy.tips[i], x$tip.label[i], font = font[i],
399                      cex = cex[i], srt = angle[i], adj = adj[i],
400                      col = tip.color[i])
401         }
402     }
403     if (show.node.label)
404         text(xx[ROOT:length(xx)] + label.offset, yy[ROOT:length(yy)],
405              x$node.label, adj = adj, font = font, srt = srt, cex = cex)
406     L <- list(type = type, use.edge.length = use.edge.length,
407               node.pos = node.pos, show.tip.label = show.tip.label,
408               show.node.label = show.node.label, font = font,
409               cex = cex, adj = adj, srt = srt, no.margin = no.margin,
410               label.offset = label.offset, x.lim = x.lim, y.lim = y.lim,
411               direction = direction, tip.color = tip.color,
412               Ntip = Ntip, Nnode = Nnode)
413     assign("last_plot.phylo", c(L, list(edge = xe, xx = xx, yy = yy)),
414            envir = .PlotPhyloEnv)
415     invisible(L)
416 }
417
418 phylogram.plot <- function(edge, Ntip, Nnode, xx, yy, horizontal,
419                            edge.color, edge.width, edge.lty)
420 {
421     nodes <- (Ntip + 1):(Ntip + Nnode)
422     if (!horizontal) {
423         tmp <- yy
424         yy <- xx
425         xx <- tmp
426     }
427     ## un trait vertical à chaque noeud...
428     x0v <- xx[nodes]
429     y0v <- y1v <- numeric(Nnode)
430     ## store the index of each node in the 1st column of edge:
431     NodeInEdge1 <- vector("list", Nnode)
432     for (i in nodes) {
433         ii <- i - Ntip
434         j <- NodeInEdge1[[ii]] <- which(edge[, 1] == i)
435         tmp <- range(yy[edge[j, 2]])
436         y0v[ii] <- tmp[1]
437         y1v[ii] <- tmp[2]
438     }
439     ## ... et un trait horizontal partant de chaque tip et chaque noeud
440     ##  vers la racine
441     x0h <- xx[edge[, 1]]
442     x1h <- xx[edge[, 2]]
443     y0h <- yy[edge[, 2]]
444
445     nc <- length(edge.color)
446     nw <- length(edge.width)
447     nl <- length(edge.lty)
448
449     if (nc + nw + nl == 3) {
450         color.v <- edge.color
451         width.v <- edge.width
452         lty.v <- edge.lty
453     } else {
454         Nedge <- dim(edge)[1]
455         edge.color <- rep(edge.color, length.out = Nedge)
456         edge.width <- rep(edge.width, length.out = Nedge)
457         edge.lty <- rep(edge.lty, length.out = Nedge)
458         DF <- data.frame(edge.color, edge.width, edge.lty, stringsAsFactors = FALSE)
459         color.v <- rep("black", Nnode)
460         width.v <- rep(1, Nnode)
461         lty.v <- rep(1, Nnode)
462         for (i in 1:Nnode) {
463             br <- NodeInEdge1[[i]]
464             if (length(br) > 2) {
465                 x <- unique(DF[br, 1])
466                 if (length(x) == 1) color.v[i] <- x
467                 x <- unique(DF[br, 2])
468                 if (length(x) == 1) width.v[i] <- x
469                 x <- unique(DF[br, 3])
470                 if (length(x) == 1) lty.v[i] <- x
471             } else {
472                 A <- br[1]
473                 B <- br[2]
474                 if (any(DF[A, ] != DF[B, ])) {
475                     color.v[i] <- edge.color[B]
476                     width.v[i] <- edge.width[B]
477                     lty.v[i] <- edge.lty[B]
478                     ## add a new line:
479                     y0v <- c(y0v, y0v[i])
480                     y1v <- c(y1v, yy[i + Ntip])
481                     x0v <- c(x0v, x0v[i])
482                     color.v <- c(color.v, edge.color[A])
483                     width.v <- c(width.v, edge.width[A])
484                     lty.v <- c(lty.v, edge.lty[A])
485                     ## shorten the line:
486                     y0v[i] <- yy[i + Ntip]
487                 } else {
488                     color.v[i] <- edge.color[A]
489                     width.v[i] <- edge.width[A]
490                     lty.v[i] <- edge.lty[A]
491                 }
492             }
493         }
494     }
495
496     if (horizontal) {
497         segments(x0h, y0h, x1h, y0h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws horizontal lines
498         segments(x0v, y0v, x0v, y1v, col = color.v, lwd = width.v, lty = lty.v) # draws vertical lines
499     } else {
500         segments(y0h, x0h, y0h, x1h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws vertical lines
501         segments(y0v, x0v, y1v, x0v, col = color.v, lwd = width.v, lty = lty.v) # draws horizontal lines
502     }
503 }
504
505 cladogram.plot <- function(edge, xx, yy, edge.color, edge.width, edge.lty)
506     segments(xx[edge[, 1]], yy[edge[, 1]], xx[edge[, 2]], yy[edge[, 2]],
507              col = edge.color, lwd = edge.width, lty = edge.lty)
508
509 circular.plot <- function(edge, Ntip, Nnode, xx, yy, theta,
510                           r, edge.color, edge.width, edge.lty)
511 ### 'edge' must be in pruningwise order
512 {
513     r0 <- r[edge[, 1]]
514     r1 <- r[edge[, 2]]
515     theta0 <- theta[edge[, 2]]
516     costheta0 <- cos(theta0)
517     sintheta0 <- sin(theta0)
518
519     x0 <- r0 * costheta0
520     y0 <- r0 * sintheta0
521     x1 <- r1 * costheta0
522     y1 <- r1 * sintheta0
523
524     segments(x0, y0, x1, y1, col = edge.color, lwd = edge.width, lty = edge.lty)
525
526     tmp <- which(diff(edge[, 1]) != 0)
527     start <- c(1, tmp + 1)
528     Nedge <- dim(edge)[1]
529     end <- c(tmp, Nedge)
530
531     ## function dispatching the features to the arcs
532     foo <- function(edge.feat, default) {
533         if (length(edge.feat) == 1) return(rep(edge.feat, Nnode))
534         else {
535             edge.feat <- rep(edge.feat, length.out = Nedge)
536             feat.arc <- rep(default, Nnode)
537             for (k in 1:Nnode) {
538                 tmp <- edge.feat[start[k]]
539                 if (tmp == edge.feat[end[k]]) feat.arc[k] <- tmp
540             }
541         }
542         feat.arc
543     }
544     co <- foo(edge.color, "black")
545     lw <- foo(edge.width, 1)
546     ly <- foo(edge.lty, 1)
547
548     for (k in 1:Nnode) {
549         i <- start[k]
550         j <- end[k]
551         X <- rep(r[edge[i, 1]], 100)
552         Y <- seq(theta[edge[i, 2]], theta[edge[j, 2]], length.out = 100)
553         lines(X*cos(Y), X*sin(Y), col = co[k], lwd = lw[k], lty = ly[k])
554     }
555 }
556
557 unrooted.xy <- function(Ntip, Nnode, edge, edge.length, nb.sp)
558 {
559     foo <- function(node, ANGLE, AXIS) {
560         ind <- which(edge[, 1] == node)
561         sons <- edge[ind, 2]
562         start <- AXIS - ANGLE/2
563         for (i in 1:length(sons)) {
564             h <- edge.length[ind[i]]
565             angle[sons[i]] <<- alpha <- ANGLE*nb.sp[sons[i]]/nb.sp[node]
566             axis[sons[i]] <<- beta <- start + alpha/2
567             start <- start + alpha
568             xx[sons[i]] <<- h*cos(beta) + xx[node]
569             yy[sons[i]] <<- h*sin(beta) + yy[node]
570         }
571         for (i in sons)
572           if (i > Ntip) foo(i, angle[i], axis[i])
573     }
574     Nedge <- dim(edge)[1]
575     yy <- xx <- numeric(Ntip + Nnode)
576     ## `angle': the angle allocated to each node wrt their nb of tips
577     ## `axis': the axis of each branch
578     axis <- angle <- numeric(Ntip + Nnode)
579     ## start with the root...
580     foo(Ntip + 1L, 2*pi, 0)
581
582     M <- cbind(xx, yy)
583     axe <- axis[1:Ntip] # the axis of the terminal branches (for export)
584     axeGTpi <- axe > pi
585     ## insures that returned angles are in [-PI, +PI]:
586     axe[axeGTpi] <- axe[axeGTpi] - 2*pi
587     list(M = M, axe = axe)
588 }
589
590 node.depth <- function(phy)
591 {
592     n <- length(phy$tip.label)
593     m <- phy$Nnode
594     N <- dim(phy$edge)[1]
595     phy <- reorder(phy, order = "pruningwise")
596     .C("node_depth", as.integer(n), as.integer(m),
597        as.integer(phy$edge[, 1]), as.integer(phy$edge[, 2]),
598        as.integer(N), double(n + m), DUP = FALSE, PACKAGE = "ape")[[6]]
599 }
600
601 plot.multiPhylo <- function(x, layout = 1, ...)
602 {
603     if (layout > 1)
604       layout(matrix(1:layout, ceiling(sqrt(layout)), byrow = TRUE))
605     else layout(matrix(1))
606     if (!par("ask")) {
607         par(ask = TRUE)
608         on.exit(par(ask = FALSE))
609     }
610     for (i in 1:length(x)) plot(x[[i]], ...)
611 }