1 ## plot.phylo.R (2009-03-27)
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)
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().")
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")
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")
41 yy <- numeric(Ntip + Nnode)
42 TIPS <- x$edge[x$edge[, 2] <= Ntip, 2]
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):
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]
56 if (is.null(node.pos)) {
58 if (type == "cladogram" && !use.edge.length) node.pos <- 2
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]]
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")
76 if (!use.edge.length) {
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
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]]
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)
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]]
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]]
121 if (type == "unrooted") {
122 XY <- if (use.edge.length)
123 unrooted.xy(Ntip, Nnode, x$edge, x$edge.length)
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])
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]]
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]]
147 if (phyloORclado && direction != "rightwards") {
148 if (direction == "leftwards") {
155 xx <- tmp - min(tmp) + 1
156 if (direction == "downwards") {
162 if (phyloORclado && root.edge) {
163 if (direction == "rightwards") xx <- xx + x$root.edge
164 if (direction == "upwards") yy <- yy + x$root.edge
166 if (no.margin) par(mai = rep(0, 4))
167 if (is.null(x.lim)) {
172 if (show.tip.label) nchar(x$tip.label) * 0.018 * max(xx) * cex
175 if (direction == "leftwards") max(xx[ROOT] + tmp)
176 else max(xx[1:Ntip] + tmp)
177 } else x.lim <- c(1, Ntip)
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))
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))
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)
197 } else if (length(x.lim) == 1) {
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")
204 if (show.tip.label) -1 - max(nchar(x$tip.label) * 0.03 * cex)
207 if (is.null(y.lim)) {
209 if (horizontal) y.lim <- c(1, Ntip) else {
212 if (show.tip.label) nchar(x$tip.label) * 0.018 * max(yy) * cex
215 if (direction == "downwards") max(yy[ROOT] + tmp)
216 else max(yy[1:Ntip] + tmp)
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))
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))
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)
237 } else if (length(y.lim) == 1) {
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
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
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, ...)
254 adj <- if (phyloORclado && direction == "leftwards") 1 else 0
256 MAXSTRING <- max(strwidth(x$tip.label, cex = cex))
257 if (direction == "rightwards") {
258 lox <- label.offset + MAXSTRING * 1.05 * adj
261 if (direction == "leftwards") {
262 lox <- -label.offset - MAXSTRING * 1.05 * (1 - adj)
268 MAXSTRING <- MAXSTRING * 1.09 * (psr[4] - psr[3]) / (psr[2] - psr[1])
269 loy <- label.offset + MAXSTRING * 1.05 * adj
272 if (direction == "downwards") {
279 if (type == "phylogram") {
280 phylogram.plot(x$edge, Ntip, Nnode, xx, yy,
281 horizontal, edge.color, edge.width, edge.lty)
284 circular.plot(x$edge, Ntip, Nnode, xx, yy, theta,
285 r, edge.color, edge.width, edge.lty)
287 cladogram.plot(x$edge, xx, yy, edge.color, edge.width, edge.lty)
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)
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)
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)
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
324 ## `srt' takes only a single value, so we cannot vectorize this:
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])
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
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]
344 adj[xx[1:Ntip] < 0] <- 1
345 ## `srt' takes only a single value, so we cannot vectorize this:
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])
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)
366 phylogram.plot <- function(edge, Ntip, Nnode, xx, yy, horizontal,
367 edge.color, edge.width, edge.lty)
369 nodes <- (Ntip + 1):(Ntip + Nnode)
375 ## un trait vertical à chaque noeud...
377 y0v <- y1v <- numeric(Nnode)
379 j <- edge[which(edge[, 1] == i), 2]
380 y0v[i - Ntip] <- min(yy[j])
381 y1v[i - Ntip] <- max(yy[j])
383 ## ... et un trait horizontal partant de chaque tip et chaque noeud
385 sq <- if (Nnode == 1) 1:Ntip else c(1:Ntip, nodes[-1])
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]]
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)
400 br <- which(edge[, 1] == i + Ntip)
401 x <- unique(edge.feat[br])
402 if (length(x) == 1) feat.v[i] <- x
407 color.v <- foo(edge.color, "black")
408 width.v <- foo(edge.width, 1)
409 lty.v <- foo(edge.lty, 1)
411 ## we need to reorder:
412 edge.width <- edge.width[pos]
413 edge.color <- edge.color[pos]
414 edge.lty <- edge.lty[pos]
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
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
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)
428 circular.plot <- function(edge, Ntip, Nnode, xx, yy, theta,
429 r, edge.color, edge.width, edge.lty)
433 theta0 <- theta[edge[, 2]]
440 segments(x0, y0, x1, y1, col = edge.color, lwd = edge.width, lty = edge.lty)
442 tmp <- which(diff(edge[, 1]) != 0)
443 start <- c(1, tmp + 1)
444 end <- c(tmp, dim(edge)[1])
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)
458 unrooted.xy <- function(Ntip, Nnode, edge, edge.length)
460 foo <- function(node, ANGLE, AXIS) {
461 ind <- which(edge[, 1] == node)
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]
473 if (i > Ntip) foo(i, angle[i], axis[i])
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!
490 axe <- axis[1:Ntip] # the axis of the terminal branches (for export)
492 ## insures that returned angles are in [-PI, +PI]:
493 axe[axeGTpi] <- axe[axeGTpi] - 2*pi
494 list(M = M, axe = axe)
497 node.depth <- function(phy)
499 n <- length(phy$tip.label)
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]]
508 plot.multiPhylo <- function(x, layout = 1, ...)
511 layout(matrix(1:layout, ceiling(sqrt(layout)), byrow = TRUE))
512 else layout(matrix(1))
515 on.exit(par(ask = FALSE))
517 for (i in 1:length(x)) plot(x[[i]], ...)