1 ## plot.phylo.R (2009-09-30)
5 ## Copyright 2002-2009 Emmanuel Paradis
7 ## This file is part of the R-package `ape'.
8 ## See the file ../COPYING for licensing issues.
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", ...)
19 Ntip <- length(x$tip.label)
21 warning("found only one tip in the tree")
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()")
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]]
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]]
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]]
46 Nedge <- dim(x$edge)[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
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")
63 phyloORclado <- type %in% c("phylogram", "cladogram")
64 horizontal <- direction %in% c("rightwards", "leftwards")
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") {
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]
79 if (length(edge.width) > 1) {
80 edge.width <- rep(edge.width, length.out = Nedge)
81 edge.width <- edge.width[ereorder]
83 if (length(edge.lty) > 1) {
84 edge.lty <- rep(edge.lty, length.out = Nedge)
85 edge.lty <- edge.lty[ereorder]
89 ### By contrats to ape (< 2.4), the arguments edge.color, etc., are
90 ### not elongated before being passed to segments(), except if needed
92 yy <- numeric(Ntip + Nnode)
93 TIPS <- x$edge[x$edge[, 2] <= Ntip, 2]
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):
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]
110 if (is.null(node.pos)) {
112 if (type == "cladogram" && !use.edge.length) node.pos <- 2
115 yy <- .nodeHeight(Ntip, Nnode, z$edge, Nedge, yy)
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")
127 if (!use.edge.length) {
128 if (node.pos != 2) xx <- .nodeDepth(Ntip, Nnode, z$edge, Nedge) - 1
131 xx <- .nodeDepthEdgelength(Ntip, Nnode, z$edge, Nedge, z$edge.length)
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)
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)
146 r <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
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)
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])
161 X <- .nodeDepth(Ntip, Nnode, z$edge, Nedge)
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)
175 xx <- tmp - min(tmp) + 1
178 if (direction == "rightwards") xx <- xx + x$root.edge
179 if (direction == "upwards") yy <- yy + x$root.edge
182 if (no.margin) par(mai = rep(0, 4))
183 if (is.null(x.lim)) {
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)
201 if (direction == "leftwards") xx <- x.lim[2] - xx #max(xx[ROOT] + tmp)
202 # else max(xx[1:Ntip] + tmp)
203 } else x.lim <- c(1, Ntip)
204 } else switch(type, "fan" = {
205 if (show.tip.label) {
206 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
207 x.lim <- c(min(xx) - offset, max(xx) + offset)
208 } else x.lim <- c(min(xx), max(xx))
210 if (show.tip.label) {
211 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
212 x.lim <- c(0 - offset, max(xx) + offset)
213 } else x.lim <- c(0, max(xx))
215 if (show.tip.label) {
216 offset <- max(nchar(x$tip.label) * 0.03 * cex)
217 x.lim <- c(-1 - offset, 1 + offset)
218 } else x.lim <- c(-1, 1)
220 } else if (length(x.lim) == 1) {
222 if (phyloORclado && !horizontal) x.lim[1] <- 1
223 if (type %in% c("fan", "unrooted") && show.tip.label)
224 x.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
225 if (type == "radial")
227 if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.03 * cex)
230 if (is.null(y.lim)) {
232 if (horizontal) y.lim <- c(1, Ntip) else {
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)
248 if (direction == "downwards") yy <- y.lim[2] - yy
250 } else switch(type, "fan" = {
251 if (show.tip.label) {
252 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
253 y.lim <- c(min(yy) - offset, max(yy) + offset)
254 } else y.lim <- c(min(yy), max(yy))
256 if (show.tip.label) {
257 offset <- max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
258 y.lim <- c(0 - offset, max(yy) + offset)
259 } else y.lim <- c(0, max(yy))
261 if (show.tip.label) {
262 offset <- max(nchar(x$tip.label) * 0.03 * cex)
263 y.lim <- c(-1 - offset, 1 + offset)
264 } else y.lim <- c(-1, 1)
266 } else if (length(y.lim) == 1) {
268 if (phyloORclado && horizontal) y.lim[1] <- 1
269 if (type %in% c("fan", "unrooted") && show.tip.label)
270 y.lim[1] <- -max(nchar(x$tip.label) * 0.018 * max(yy) * cex)
271 if (type == "radial")
272 y.lim[1] <- if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.018 * max(yy) * cex) else -1
274 if (phyloORclado && root.edge) {
275 if (direction == "leftwards") x.lim[2] <- x.lim[2] + x$root.edge
276 if (direction == "downwards") y.lim[2] <- y.lim[2] + x$root.edge
278 asp <- if (type %in% c("fan", "radial")) 1 else NA # fix by Klaus Schliep (2008-03-28)
279 plot(0, type = "n", xlim = x.lim, ylim = y.lim, ann = FALSE, axes = FALSE, asp = asp, ...)
281 adj <- if (phyloORclado && direction == "leftwards") 1 else 0
282 if (phyloORclado && show.tip.label) {
283 MAXSTRING <- max(strwidth(x$tip.label, cex = cex))
285 if (direction == "rightwards") {
286 lox <- label.offset + MAXSTRING * 1.05 * adj
288 if (direction == "leftwards") {
289 lox <- -label.offset - MAXSTRING * 1.05 * (1 - adj)
290 #xx <- xx + MAXSTRING
294 MAXSTRING <- MAXSTRING * 1.09 * (psr[4] - psr[3])/(psr[2] - psr[1])
295 loy <- label.offset + MAXSTRING * 1.05 * adj
298 if (direction == "downwards") {
300 ##yy <- yy + MAXSTRING
305 if (type == "phylogram") {
306 phylogram.plot(x$edge, Ntip, Nnode, xx, yy,
307 horizontal, edge.color, edge.width, edge.lty)
310 ereorder <- match(z$edge[, 2], x$edge[, 2])
311 if (length(edge.color) > 1) {
312 edge.color <- rep(edge.color, length.out = Nedge)
313 edge.color <- edge.color[ereorder]
315 if (length(edge.width) > 1) {
316 edge.width <- rep(edge.width, length.out = Nedge)
317 edge.width <- edge.width[ereorder]
319 if (length(edge.lty) > 1) {
320 edge.lty <- rep(edge.lty, length.out = Nedge)
321 edge.lty <- edge.lty[ereorder]
323 circular.plot(z$edge, Ntip, Nnode, xx, yy, theta,
324 r, edge.color, edge.width, edge.lty)
326 cladogram.plot(x$edge, xx, yy, edge.color, edge.width, edge.lty)
330 "rightwards" = segments(0, yy[ROOT], x$root.edge, yy[ROOT]),
331 "leftwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT] + x$root.edge, yy[ROOT]),
332 "upwards" = segments(xx[ROOT], 0, xx[ROOT], x$root.edge),
333 "downwards" = segments(xx[ROOT], yy[ROOT], xx[ROOT], yy[ROOT] + x$root.edge))
334 if (show.tip.label) {
335 if (!underscore) x$tip.label <- gsub("_", " ", x$tip.label)
338 text(xx[1:Ntip] + lox, yy[1:Ntip] + loy, x$tip.label, adj = adj,
339 font = font, srt = srt, cex = cex, col = tip.color)
341 if (type == "unrooted") {
342 if (lab4ut == "horizontal") {
343 y.adj <- x.adj <- numeric(Ntip)
344 sel <- abs(XY$axe) > 0.75 * pi
345 x.adj[sel] <- -strwidth(x$tip.label)[sel] * 1.05
346 sel <- abs(XY$axe) > pi/4 & abs(XY$axe) < 0.75 * pi
347 x.adj[sel] <- -strwidth(x$tip.label)[sel] * (2 * abs(XY$axe)[sel] / pi - 0.5)
348 sel <- XY$axe > pi / 4 & XY$axe < 0.75 * pi
349 y.adj[sel] <- strheight(x$tip.label)[sel] / 2
350 sel <- XY$axe < -pi / 4 & XY$axe > -0.75 * pi
351 y.adj[sel] <- -strheight(x$tip.label)[sel] * 0.75
352 text(xx[1:Ntip] + x.adj*cex, yy[1:Ntip] + y.adj*cex,
353 x$tip.label, adj = c(adj, 0), font = font,
354 srt = srt, cex = cex, col = tip.color)
355 } else { # if lab4ut == "axial"
356 adj <- as.numeric(abs(XY$axe) > pi/2)
358 srt[as.logical(adj)] <- srt[as.logical(adj)] - 180
359 ## `srt' takes only a single value, so can't vectorize this:
361 text(xx[i], yy[i], cex = cex, x$tip.label[i], adj = adj[i],
362 font = font, srt = srt[i], col = tip.color[i])
365 if (type %in% c("fan", "radial")) {
366 xx.scaled <- xx[1:Ntip]
367 if (type == "fan") { # no need if type == "radial"
368 maxx <- max(abs(xx.scaled))
369 if (maxx > 1) xx.scaled <- xx.scaled/maxx
371 angle <- acos(xx.scaled)*180/pi
372 s1 <- angle > 90 & yy[1:Ntip] > 0
373 s2 <- angle < 90 & yy[1:Ntip] < 0
374 s3 <- angle > 90 & yy[1:Ntip] < 0
375 angle[s1] <- angle[s1] + 180
376 angle[s2] <- -angle[s2]
377 angle[s3] <- 180 - angle[s3]
379 adj[xx[1:Ntip] < 0] <- 1
380 ## `srt' takes only a single value, so can't vectorize this:
382 text(xx[i], yy[i], x$tip.label[i], font = font, cex = cex,
383 srt = angle[i], adj = adj[i], col = tip.color[i])
387 text(xx[ROOT:length(xx)] + label.offset, yy[ROOT:length(yy)],
388 x$node.label, adj = adj, font = font, srt = srt, cex = cex)
389 L <- list(type = type, use.edge.length = use.edge.length,
390 node.pos = node.pos, show.tip.label = show.tip.label,
391 show.node.label = show.node.label, font = font,
392 cex = cex, adj = adj, srt = srt, no.margin = no.margin,
393 label.offset = label.offset, x.lim = x.lim, y.lim = y.lim,
394 direction = direction, tip.color = tip.color,
395 Ntip = Ntip, Nnode = Nnode)
396 assign("last_plot.phylo", c(L, list(edge = x$edge, xx = xx, yy = yy)),
397 envir = .PlotPhyloEnv)
401 phylogram.plot <- function(edge, Ntip, Nnode, xx, yy, horizontal,
402 edge.color, edge.width, edge.lty)
404 nodes <- (Ntip + 1):(Ntip + Nnode)
410 ## un trait vertical à chaque noeud...
412 y0v <- y1v <- numeric(Nnode)
413 ## store the index of each node in the 1st column of edge:
414 NodeInEdge1 <- vector("list", Nnode)
417 j <- NodeInEdge1[[ii]] <- which(edge[, 1] == i)
418 tmp <- range(yy[edge[j, 2]])
422 ## ... et un trait horizontal partant de chaque tip et chaque noeud
428 nc <- length(edge.color)
429 nw <- length(edge.width)
430 nl <- length(edge.lty)
432 if (nc + nw + nl == 3) {
433 color.v <- edge.color
434 width.v <- edge.width
437 Nedge <- dim(edge)[1]
438 edge.color <- rep(edge.color, length.out = Nedge)
439 edge.width <- rep(edge.width, length.out = Nedge)
440 edge.lty <- rep(edge.lty, length.out = Nedge)
441 DF <- data.frame(edge.color, edge.width, edge.lty)
442 color.v <- rep("black", Nnode)
443 width.v <- rep(1, Nnode)
444 lty.v <- rep(1, Nnode)
446 br <- NodeInEdge1[[i]]
447 if (length(br) > 2) {
448 x <- unique(DF[br, 1])
449 if (length(x) == 1) color.v[i] <- x
450 x <- unique(DF[br, 2])
451 if (length(x) == 1) width.v[i] <- x
452 x <- unique(DF[br, 3])
453 if (length(x) == 1) lty.v[i] <- x
457 if (any(DF[A, ] != DF[B, ])) {
458 color.v[i] <- edge.color[B]
459 width.v[i] <- edge.width[B]
460 lty.v[i] <- edge.lty[B]
462 y0v <- c(y0v, y0v[i])
463 y1v <- c(y1v, yy[i + Ntip])
464 x0v <- c(x0v, x0v[i])
465 color.v <- c(color.v, edge.color[A])
466 width.v <- c(width.v, edge.width[A])
467 lty.v <- c(lty.v, edge.lty[A])
469 y0v[i] <- yy[i + Ntip]
471 color.v[i] <- edge.color[A]
472 width.v[i] <- edge.width[A]
473 lty.v[i] <- edge.lty[A]
480 segments(x0h, y0h, x1h, y0h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws horizontal lines
481 segments(x0v, y0v, x0v, y1v, col = color.v, lwd = width.v, lty = lty.v) # draws vertical lines
483 segments(y0h, x0h, y0h, x1h, col = edge.color, lwd = edge.width, lty = edge.lty) # draws vertical lines
484 segments(y0v, x0v, y1v, x0v, col = color.v, lwd = width.v, lty = lty.v) # draws horizontal lines
488 cladogram.plot <- function(edge, xx, yy, edge.color, edge.width, edge.lty)
489 segments(xx[edge[, 1]], yy[edge[, 1]], xx[edge[, 2]], yy[edge[, 2]],
490 col = edge.color, lwd = edge.width, lty = edge.lty)
492 circular.plot <- function(edge, Ntip, Nnode, xx, yy, theta,
493 r, edge.color, edge.width, edge.lty)
494 ### 'edge' must be in pruningwise order
498 theta0 <- theta[edge[, 2]]
499 costheta0 <- cos(theta0)
500 sintheta0 <- sin(theta0)
507 segments(x0, y0, x1, y1, col = edge.color, lwd = edge.width, lty = edge.lty)
509 tmp <- which(diff(edge[, 1]) != 0)
510 start <- c(1, tmp + 1)
511 Nedge <- dim(edge)[1]
514 ## function dispatching the features to the arcs
515 foo <- function(edge.feat, default) {
516 if (length(edge.feat) == 1) return(rep(edge.feat, Nnode))
518 edge.feat <- rep(edge.feat, length.out = Nedge)
519 feat.arc <- rep(default, Nnode)
521 tmp <- edge.feat[start[k]]
522 if (tmp == edge.feat[end[k]]) feat.arc[k] <- tmp
527 co <- foo(edge.color, "black")
528 lw <- foo(edge.width, 1)
529 ly <- foo(edge.lty, 1)
534 X <- rep(r[edge[i, 1]], 100)
535 Y <- seq(theta[edge[i, 2]], theta[edge[j, 2]], length.out = 100)
536 lines(X*cos(Y), X*sin(Y), col = co[k], lwd = lw[k], lty = ly[k])
540 unrooted.xy <- function(Ntip, Nnode, edge, edge.length, nb.sp)
542 foo <- function(node, ANGLE, AXIS) {
543 ind <- which(edge[, 1] == node)
545 start <- AXIS - ANGLE/2
546 for (i in 1:length(sons)) {
547 h <- edge.length[ind[i]]
548 angle[sons[i]] <<- alpha <- ANGLE*nb.sp[sons[i]]/nb.sp[node]
549 axis[sons[i]] <<- beta <- start + alpha/2
550 start <- start + alpha
551 xx[sons[i]] <<- h*cos(beta) + xx[node]
552 yy[sons[i]] <<- h*sin(beta) + yy[node]
555 if (i > Ntip) foo(i, angle[i], axis[i])
557 Nedge <- dim(edge)[1]
558 yy <- xx <- numeric(Ntip + Nnode)
559 ## `angle': the angle allocated to each node wrt their nb of tips
560 ## `axis': the axis of each branch
561 axis <- angle <- numeric(Ntip + Nnode)
562 ## start with the root...
563 foo(Ntip + 1L, 2*pi, 0)
566 axe <- axis[1:Ntip] # the axis of the terminal branches (for export)
568 ## insures that returned angles are in [-PI, +PI]:
569 axe[axeGTpi] <- axe[axeGTpi] - 2*pi
570 list(M = M, axe = axe)
573 node.depth <- function(phy)
575 n <- length(phy$tip.label)
577 N <- dim(phy$edge)[1]
578 phy <- reorder(phy, order = "pruningwise")
579 .C("node_depth", as.integer(n), as.integer(m),
580 as.integer(phy$edge[, 1]), as.integer(phy$edge[, 2]),
581 as.integer(N), double(n + m), DUP = FALSE, PACKAGE = "ape")[[6]]
584 plot.multiPhylo <- function(x, layout = 1, ...)
587 layout(matrix(1:layout, ceiling(sqrt(layout)), byrow = TRUE))
588 else layout(matrix(1))
591 on.exit(par(ask = FALSE))
593 for (i in x) plot(i, ...)