]> git.donarmstrong.com Git - ape.git/blob - R/plot.phylo.R
finally the new plot.phylo...
[ape.git] / R / plot.phylo.R
1 ## plot.phylo.R (2009-09-23)
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) {
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     if (phyloORclado) {
66         ## we first compute the y-coordinates of the tips.
67         phyOrder <- attr(x, "order")
68         ## make sure the tree is in cladewise order:
69         if (is.null(phyOrder) || phyOrder != "cladewise") {
70             xe <- x$edge
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     }
134     if (type == "fan") {
135         ## if the tips are not in the same order in tip.label
136         ## and in edge[, 2], we must reorder the angles: we
137         ## use `xx' to store temporarily the angles
138         TIPS <- x$edge[which(x$edge[, 2] <= Ntip), 2]
139         xx <- seq(0, 2*pi*(1 - 1/Ntip), 2*pi/Ntip)
140         theta <- double(Ntip)
141         theta[TIPS] <- xx
142         theta <- c(theta, numeric(Nnode))
143         theta <- .nodeHeight(Ntip, Nnode, z$edge, Nedge, theta)
144         if (use.edge.length) {
145             r <- .nodeDepthEdgelength(Ntip, Nnode, z$edge, Nedge, z$edge.length)
146         } else {
147             r <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
148             r <- 1/r
149         }
150         xx <- r*cos(theta)
151         yy <- r*sin(theta)
152
153     }
154     if (type == "unrooted") {
155         nb.sp <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
156         XY <- if (use.edge.length)
157             unrooted.xy(Ntip, Nnode, z$edge, z$edge.length, nb.sp)
158         else
159             unrooted.xy(Ntip, Nnode, z$edge, rep(1, Nedge), nb.sp)
160         ## rescale so that we have only positive values
161         xx <- XY$M[, 1] - min(XY$M[, 1])
162         yy <- XY$M[, 2] - min(XY$M[, 2])
163     }
164     if (type == "radial") {
165         X <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
166         X[X == 1] <- 0
167         ## radius:
168         X <- 1 - X/Ntip
169         ## angle (1st compute the angles for the tips):
170         yy <- c((1:Ntip)*2*pi/Ntip, rep(0, Nnode))
171         Y <- .nodeHeight(Ntip, Nnode, z$edge, Nedge, yy)
172         xx <- X * cos(Y)
173         yy <- X * sin(Y)
174     }
175     if (phyloORclado) {
176         if (!horizontal) {
177             tmp <- yy
178             yy <- xx
179             xx <- tmp - min(tmp) + 1
180         }
181         if (root.edge) {
182             if (direction == "rightwards") xx <- xx + x$root.edge
183             if (direction == "upwards") yy <- yy + x$root.edge
184         }
185     }
186     if (no.margin) par(mai = rep(0, 4))
187     if (is.null(x.lim)) {
188         if (phyloORclado) {
189             if (horizontal) {
190                 x.lim <- c(0, NA)
191                 pin1 <- par("pin")[1] # width of the device in inches
192                 strWi <- strwidth(x$tip.label, "inches") # id. for the tip labels
193                 ## 1.04 comes from that we are using a regular axis system
194                 ## with 4% on both sides of the range of x:
195                 xx.tips <- xx[1:Ntip] * 1.04
196                 ## 'alp' is the conversion coefficient from
197                 ## user coordinates to inches:
198                 alp <- try(uniroot(function(a) max(a*xx.tips + strWi) - pin1,
199                                    c(0, 1e6))$root, silent = TRUE)
200                 ## if the above fails, give 1/3 of the device for the tip labels:
201                 if (is.character(alp)) tmp <- max(xx.tips)*1.5 else {
202                     tmp <- if (show.tip.label) max(xx.tips + strWi/alp) else max(xx.tips)
203                 }
204                 x.lim[2] <- tmp
205                 if (direction == "leftwards") xx <- x.lim[2] - xx #max(xx[ROOT] + tmp)
206 #                  else max(xx[1:Ntip] + tmp)
207             } else x.lim <- c(1, Ntip)
208         }
209         if (type == "fan") {
210             if (show.tip.label) {
211                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
212                 x.lim <- c(min(xx) - offset, max(xx) + offset)
213             } else x.lim <- c(min(xx), max(xx))
214         }
215         if (type == "unrooted") {
216             if (show.tip.label) {
217                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
218                 x.lim <- c(0 - offset, max(xx) + offset)
219             } else x.lim <- c(0, max(xx))
220         }
221         if (type == "radial") {
222             if (show.tip.label) {
223                 offset <- max(nchar(x$tip.label) * 0.03 * cex)
224                 x.lim <- c(-1 - offset, 1 + offset)
225             } else x.lim <- c(-1, 1)
226         }
227     } else if (length(x.lim) == 1) {
228         x.lim <- c(0, x.lim)
229         if (phyloORclado && !horizontal) x.lim[1] <- 1
230         if (type %in% c("fan", "unrooted") && show.tip.label)
231           x.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
232         if (type == "radial")
233           x.lim[1] <-
234             if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.03 * cex)
235             else -1
236     }
237     if (is.null(y.lim)) {
238         if (phyloORclado) {
239             if (horizontal) y.lim <- c(1, Ntip) else {
240                 y.lim <- c(0, NA)
241                 pin2 <- par("pin")[2] # height of the device in inches
242                 strWi <- strwidth(x$tip.label, "inches")
243                 ## 1.04 comes from that we are using a regular axis system
244                 ## with 4% on both sides of the range of x:
245                 yy.tips <- yy[1:Ntip] * 1.04
246                 ## 'alp' is the conversion coefficient from
247                 ## user coordinates to inches:
248                 alp <- try(uniroot(function(a) max(a*yy.tips + strWi) - pin2,
249                                    c(0, 1e6))$root, silent = TRUE)
250                 ## if the above fails, give 1/3 of the device for the tip labels:
251                 if (is.character(alp)) tmp <- max(yy.tips)*1.5 else {
252                     tmp <- if (show.tip.label) max(yy.tips + strWi/alp) else max(yy.tips)
253                 }
254                 y.lim[2] <- tmp
255                 if (direction == "downwards") yy <- y.lim[2] - yy
256             }
257         }
258         if (type == "fan") {
259             if (show.tip.label) {
260                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
261                 y.lim <- c(min(yy) - offset, max(yy) + offset)
262             } else y.lim <- c(min(yy), max(yy))
263         }
264         if (type == "unrooted") {
265             if (show.tip.label) {
266                 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
267                 y.lim <- c(0 - offset, max(yy) + offset)
268             } else y.lim <- c(0, max(yy))
269         }
270         if (type == "radial") {
271             if (show.tip.label) {
272                 offset <- max(nchar(x$tip.label) * 0.03 * cex)
273                 y.lim <- c(-1 - offset, 1 + offset)
274             } else y.lim <- c(-1, 1)
275         }
276     } else if (length(y.lim) == 1) {
277         y.lim <- c(0, y.lim)
278         if (phyloORclado && horizontal) y.lim[1] <- 1
279         if (type %in% c("fan", "unrooted") && show.tip.label)
280           y.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
281         if (type == "radial")
282           y.lim[1] <- if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.018 * max(yy) * cex) else -1
283     }
284     if (phyloORclado && root.edge) {
285         if (direction == "leftwards") x.lim[2] <- x.lim[2] + x$root.edge
286         if (direction == "downwards") y.lim[2] <- y.lim[2] + x$root.edge
287     }
288     ## fix by Klaus Schliep (2008-03-28):
289     asp <- if (type %in% c("fan", "radial")) 1 else NA
290     plot(0, type = "n", xlim = x.lim, ylim = y.lim, ann = FALSE, axes = FALSE, asp = asp, ...)
291     if (is.null(adj))
292         adj <- if (phyloORclado && direction == "leftwards") 1 else 0
293     if (phyloORclado && show.tip.label) {
294         MAXSTRING <- max(strwidth(x$tip.label, cex = cex))
295         loy <- 0
296         if (direction == "rightwards") {
297             lox <- label.offset + MAXSTRING * 1.05 * adj
298         }
299         if (direction == "leftwards") {
300             lox <- -label.offset - MAXSTRING * 1.05 * (1 - adj)
301             #xx <- xx + MAXSTRING
302         }
303         if (!horizontal) {
304             psr <- par("usr")
305             MAXSTRING <- MAXSTRING * 1.09 * (psr[4] - psr[3])/(psr[2] - psr[1])
306             loy <- label.offset + MAXSTRING * 1.05 * adj
307             lox <- 0
308             srt <- 90 + srt
309             if (direction == "downwards") {
310                 loy <- -loy
311                 ##yy <- yy + MAXSTRING
312                 srt <- 180 + srt
313             }
314         }
315     }
316     if (type == "phylogram") {
317         phylogram.plot(x$edge, Ntip, Nnode, xx, yy,
318                        horizontal, edge.color, edge.width, edge.lty)
319     } else {
320         if (type == "fan") {
321             ereorder <- match(z$edge[, 2], x$edge[, 2])
322             if (length(edge.color) > 1) {
323                 edge.color <- rep(edge.color, length.out = Nedge)
324                 edge.color <- edge.color[ereorder]
325             }
326             if (length(edge.width) > 1) {
327                 edge.width <- rep(edge.width, length.out = Nedge)
328                 edge.width <- edge.width[ereorder]
329             }
330             if (length(edge.lty) > 1) {
331                 edge.lty <- rep(edge.lty, length.out = Nedge)
332                 edge.lty <- edge.lty[ereorder]
333             }
334             circular.plot(z$edge, Ntip, Nnode, xx, yy, theta,
335                           r, edge.color, edge.width, edge.lty)
336         } else
337         cladogram.plot(x$edge, xx, yy, edge.color, edge.width, edge.lty)
338     }
339     if (root.edge)
340       switch(direction,
341              "rightwards" = segments(0, yy[ROOT], x$root.edge, yy[ROOT]),
342              "leftwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT] + x$root.edge, yy[ROOT]),
343              "upwards" = segments(xx[ROOT], 0, xx[ROOT], x$root.edge),
344              "downwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT], yy[ROOT] + x$root.edge))
345     if (show.tip.label) {
346         if (!underscore) x$tip.label <- gsub("_", " ", x$tip.label)
347
348         if (phyloORclado)
349             text(xx[1:Ntip] + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj,
350                  font = font, srt = srt, cex = cex, col = tip.color)
351
352         if (type == "unrooted") {
353             if (lab4ut == "horizontal") {
354                 y.adj <- x.adj <- numeric(Ntip)
355                 sel <- abs(XY$axe) > 0.75 * pi
356                 x.adj[sel] <- -strwidth(x$tip.label)[sel] * 1.05
357                 sel <- abs(XY$axe) > pi/4 & abs(XY$axe) < 0.75 * pi
358                 x.adj[sel] <- -strwidth(x$tip.label)[sel] * (2 * abs(XY$axe)[sel] / pi - 0.5)
359                 sel <- XY$axe > pi / 4 & XY$axe < 0.75 * pi
360                 y.adj[sel] <- strheight(x$tip.label)[sel] / 2
361                 sel <- XY$axe < -pi / 4 & XY$axe > -0.75 * pi
362                 y.adj[sel] <- -strheight(x$tip.label)[sel] * 0.75
363                 text(xx[1:Ntip] + x.adj*cex, yy[1:Ntip] + y.adj*cex,
364                      x$tip.label, adj = c(adj, 0), font = font,
365                      srt = srt, cex = cex, col = tip.color)
366             } else { # if lab4ut == "axial"
367                 adj <- as.numeric(abs(XY$axe) > pi/2)
368                 srt <- 180*XY$axe/pi
369                 srt[as.logical(adj)] <- srt[as.logical(adj)] - 180
370                 ## `srt' takes only a single value, so can't vectorize this:
371                 for (i in 1:Ntip)
372                   text(xx[i], yy[i], cex = cex, x$tip.label[i], adj = adj[i],
373                        font = font, srt = srt[i], col = tip.color[i])
374             }
375         }
376         if (type %in% c("fan", "radial")) {
377             xx.scaled <- xx[1:Ntip]
378             if (type == "fan") { # no need if type == "radial"
379                 maxx <- max(abs(xx.scaled))
380                 if (maxx > 1) xx.scaled <- xx.scaled/maxx
381             }
382             angle <- acos(xx.scaled)*180/pi
383             s1 <- angle > 90 & yy[1:Ntip] > 0
384             s2 <- angle < 90 & yy[1:Ntip] < 0
385             s3 <- angle > 90 & yy[1:Ntip] < 0
386             angle[s1] <- angle[s1] + 180
387             angle[s2] <- -angle[s2]
388             angle[s3] <- 180 - angle[s3]
389             adj <- numeric(Ntip)
390             adj[xx[1:Ntip] < 0] <- 1
391             ## `srt' takes only a single value, so can't vectorize this:
392             for (i in 1:Ntip)
393               text(xx[i], yy[i], x$tip.label[i], font = font, cex = cex,
394                    srt = angle[i], adj = adj[i], col = tip.color[i])
395         }
396     }
397     if (show.node.label)
398       text(xx[ROOT:length(xx)] + label.offset, yy[ROOT:length(yy)],
399            x$node.label, adj = adj, font = font, srt = srt, cex = cex)
400     L <- list(type = type, use.edge.length = use.edge.length,
401               node.pos = node.pos, show.tip.label = show.tip.label,
402               show.node.label = show.node.label, font = font,
403               cex = cex, adj = adj, srt = srt, no.margin = no.margin,
404               label.offset = label.offset, x.lim = x.lim, y.lim = y.lim,
405               direction = direction, tip.color = tip.color,
406               Ntip = Ntip, Nnode = Nnode)
407     assign("last_plot.phylo", c(L, list(edge = x$edge, xx = xx, yy = yy)),
408            envir = .PlotPhyloEnv)
409     invisible(L)
410 }
411
412 phylogram.plot <- function(edge, Ntip, Nnode, xx, yy, horizontal,
413                            edge.color, edge.width, edge.lty)
414 {
415     nodes <- (Ntip + 1):(Ntip + Nnode)
416     if (!horizontal) {
417         tmp <- yy
418         yy <- xx
419         xx <- tmp
420     }
421     ## un trait vertical Ã  chaque noeud...
422     x0v <- xx[nodes]
423     y0v <- y1v <- numeric(Nnode)
424     ## store the index of each node in the 1st column of edge:
425     NodeInEdge1 <- vector("list", Nnode)
426     for (i in nodes) {
427         ii <- i - Ntip
428         j <- NodeInEdge1[[ii]] <- which(edge[, 1] == i)
429         tmp <- range(yy[edge[j, 2]])
430         y0v[ii] <- tmp[1]
431         y1v[ii] <- tmp[2]
432     }
433     ## ... et un trait horizontal partant de chaque tip et chaque noeud
434     ##  vers la racine
435 ###    sq <- c(1:Ntip, nodes[-1])
436 ###    y0h <- yy[sq]
437 ###    x1h <- xx[sq]
438 ###    ## match() is very useful here becoz each element in edge[, 2] is
439 ###    ## unique (not sure this is so useful in edge[, 1]; needs to be checked)
440 ###    ## `pos' gives for each element in `sq' its index in edge[, 2]
441 ###    pos <- match(sq, edge[, 2])
442 ###    x0h <- xx[edge[pos, 1]]
443     x0h <- xx[edge[, 1]]
444     x1h <- xx[edge[, 2]]
445     y0h <- yy[edge[, 2]]
446 ### donc plus besoin de 'pos' ni 'sq'
447
448     nc <- length(edge.color)
449     nw <- length(edge.width)
450     nl <- length(edge.lty)
451
452     if (nc + nw + nl == 3) {
453         color.v <- edge.color
454         width.v <- edge.width
455         lty.v <- edge.lty
456     } else {
457         Nedge <- dim(edge)[1]
458         edge.color <- rep(edge.color, length.out = Nedge)
459         edge.width <- rep(edge.width, length.out = Nedge)
460         edge.lty <- rep(edge.lty, length.out = Nedge)
461         DF <- data.frame(edge.color, edge.width, edge.lty)
462         color.v <- rep("black", Nnode)
463         width.v <- rep(1, Nnode)
464         lty.v <- rep(1, Nnode)
465         for (i in 1:Nnode) {
466             br <- NodeInEdge1[[i]]
467             if (length(br) > 2) {
468                 x <- unique(DF[br, 1])
469                 if (length(x) == 1) color.v[i] <- x
470                 x <- unique(DF[br, 2])
471                 if (length(x) == 1) width.v[i] <- x
472                 x <- unique(DF[br, 3])
473                 if (length(x) == 1) lty.v[i] <- x
474             } else {
475                 A <- br[1]
476                 B <- br[2]
477                 if (any(DF[A, ] != DF[B, ])) {
478                     color.v[i] <- edge.color[B]
479                     width.v[i] <- edge.width[B]
480                     lty.v[i] <- edge.lty[B]
481                     ## add a new line:
482                     y0v <- c(y0v, y0v[i])
483                     y1v <- c(y1v, yy[i + Ntip])
484                     x0v <- c(x0v, x0v[i])
485                     color.v <- c(color.v, edge.color[A])
486                     width.v <- c(width.v, edge.width[A])
487                     lty.v <- c(lty.v, edge.lty[A])
488                     ## shorten the line:
489                     y0v[i] <- yy[i + Ntip]
490                 } else {
491                     color.v[i] <- edge.color[A]
492                     width.v[i] <- edge.width[A]
493                     lty.v[i] <- edge.lty[A]
494                 }
495             }
496         }
497     }
498
499 ###    ## function dispatching the features to the vertical edges
500 ###    foo <- function(edge.feat, default) {
501 ###        e <- unique(edge.feat)
502 ###        if (length(e) == 1) return(rep(e, Nnode))
503 ###        else {
504 ###            feat.v <- rep(default, Nnode)
505 ###            for (i in 1:Nnode) {
506 ###                br <- NodeInEdge1[[i]]
507 ###                if (length(br) > 2) {
508 ###                    x <- unique(edge.feat[br])
509 ###                    if (length(x) == 1) feat.v[i] <- x
510 ###                } else {
511 ###                    if (edge.feat[br[1]] == edge.feat[br[2]])
512 ###                        feat.v[i] <- edge.feat[br[1]]
513 ###                    else {
514 ###                        feat.v[i] <- edge.feat[br[2]]
515 ###                        ## add a new line:
516 ###                        y0v <<- c(y0v, y0v[i])
517 ###                        y1v <<- c(y1v, yy[i + Ntip])
518 ###                        x0v <<- c(x0v, x0v[i])
519 ###                        feat.v <- c(feat.v, edge.feat[br[1]])
520 ###                        ## shorten the line:
521 ###                        y0v[i] <<- yy[i + Ntip]
522 ###                    }
523 ###                }
524 ###            }
525 ###        }
526 ###        feat.v
527 ###    }
528 ###    color.v <- foo(edge.color, "black")
529 ###    width.v <- foo(edge.width, 1)
530 ###    lty.v <- foo(edge.lty, 1)
531
532 ###    ## we need to reorder:
533 ###    edge.width <- edge.width[pos]
534 ###    edge.color <- edge.color[pos]
535 ###    edge.lty <- edge.lty[pos]
536
537     if (horizontal) {
538         segments(x0h, y0h, x1h, y0h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws horizontal lines
539         segments(x0v, y0v, x0v, y1v, col = color.v, lwd = width.v, lty = lty.v) # draws vertical lines
540     } else {
541         segments(y0h, x0h, y0h, x1h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws vertical lines
542         segments(y0v, x0v, y1v, x0v, col = color.v, lwd = width.v, lty = lty.v) # draws horizontal lines
543     }
544 }
545
546 cladogram.plot <- function(edge, xx, yy, edge.color, edge.width, edge.lty)
547     segments(xx[edge[, 1]], yy[edge[, 1]], xx[edge[, 2]], yy[edge[, 2]],
548              col = edge.color, lwd = edge.width, lty = edge.lty)
549
550 circular.plot <- function(edge, Ntip, Nnode, xx, yy, theta,
551                           r, edge.color, edge.width, edge.lty)
552 ### 'edge' must be in pruningwise order
553 {
554     r0 <- r[edge[, 1]]
555     r1 <- r[edge[, 2]]
556     theta0 <- theta[edge[, 2]]
557     costheta0 <- cos(theta0)
558     sintheta0 <- sin(theta0)
559
560     x0 <- r0 * costheta0
561     y0 <- r0 * sintheta0
562     x1 <- r1 * costheta0
563     y1 <- r1 * sintheta0
564
565     segments(x0, y0, x1, y1, col = edge.color, lwd = edge.width, lty = edge.lty)
566
567     tmp <- which(diff(edge[, 1]) != 0)
568     start <- c(1, tmp + 1)
569     Nedge <- dim(edge)[1]
570     end <- c(tmp, Nedge)
571
572     ## function dispatching the features to the arcs
573     foo <- function(edge.feat, default) {
574         if (length(edge.feat) == 1) return(rep(edge.feat, Nnode))
575         else {
576             edge.feat <- rep(edge.feat, length.out = Nedge)
577             feat.arc <- rep(default, Nnode)
578             for (k in 1:Nnode) {
579                 tmp <- edge.feat[start[k]]
580                 if (tmp == edge.feat[end[k]]) feat.arc[k] <- tmp
581             }
582         }
583         feat.arc
584     }
585     co <- foo(edge.color, "black")
586     lw <- foo(edge.width, 1)
587     ly <- foo(edge.lty, 1)
588
589     for (k in 1:Nnode) {
590         i <- start[k]
591         j <- end[k]
592         X <- rep(r[edge[i, 1]], 100)
593         Y <- seq(theta[edge[i, 2]], theta[edge[j, 2]], length.out = 100)
594         lines(X*cos(Y), X*sin(Y), col = co[k], lwd = lw[k], lty = ly[k])
595     }
596 }
597
598 unrooted.xy <- function(Ntip, Nnode, edge, edge.length, nb.sp)
599 {
600     foo <- function(node, ANGLE, AXIS) {
601         ind <- which(edge[, 1] == node)
602         sons <- edge[ind, 2]
603         start <- AXIS - ANGLE/2
604         for (i in 1:length(sons)) {
605             h <- edge.length[ind[i]]
606             angle[sons[i]] <<- alpha <- ANGLE*nb.sp[sons[i]]/nb.sp[node]
607             axis[sons[i]] <<- beta <- start + alpha/2
608             start <- start + alpha
609             xx[sons[i]] <<- h*cos(beta) + xx[node]
610             yy[sons[i]] <<- h*sin(beta) + yy[node]
611         }
612         for (i in sons)
613           if (i > Ntip) foo(i, angle[i], axis[i])
614     }
615     Nedge <- dim(edge)[1]
616     yy <- xx <- numeric(Ntip + Nnode)
617     ## `angle': the angle allocated to each node wrt their nb of tips
618     ## `axis': the axis of each branch
619     axis <- angle <- numeric(Ntip + Nnode)
620     ## start with the root...
621     foo(Ntip + 1L, 2*pi, 0)
622
623     M <- cbind(xx, yy)
624     axe <- axis[1:Ntip] # the axis of the terminal branches (for export)
625     axeGTpi <- axe > pi
626     ## insures that returned angles are in [-PI, +PI]:
627     axe[axeGTpi] <- axe[axeGTpi] - 2*pi
628     list(M = M, axe = axe)
629 }
630
631 node.depth <- function(phy)
632 {
633     n <- length(phy$tip.label)
634     m <- phy$Nnode
635     N <- dim(phy$edge)[1]
636     phy <- reorder(phy, order = "pruningwise")
637     .C("node_depth", as.integer(n), as.integer(m),
638        as.integer(phy$edge[, 1]), as.integer(phy$edge[, 2]),
639        as.integer(N), double(n + m), DUP = FALSE, PACKAGE = "ape")[[6]]
640 }
641
642 plot.multiPhylo <- function(x, layout = 1, ...)
643 {
644     if (layout > 1)
645       layout(matrix(1:layout, ceiling(sqrt(layout)), byrow = TRUE))
646     else layout(matrix(1))
647     if (!par("ask")) {
648         par(ask = TRUE)
649         on.exit(par(ask = FALSE))
650     }
651     for (i in x) plot(i, ...)
652 }