Skip to content

Commit 9df22c3

Browse files
committed
fixes #26
1 parent 8f04a49 commit 9df22c3

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

R/tree.R

+19-9
Original file line numberDiff line numberDiff line change
@@ -1003,26 +1003,36 @@ draw.branch <- function(layout, tips, col='red', ...) {
10031003
#' @param tips: a character vector of tip labels
10041004
#'
10051005
#' @export
1006-
draw.clade <- function(layout, tips, col='red', ...) {
1007-
unmatched <- !is.element(tips, layout$nodes$label)
1006+
draw.clade <- function(obj, tips, col='red', ...) {
1007+
unmatched <- !is.element(tips, obj$nodes$label)
10081008
if (any(unmatched)) {
10091009
stop("Error: not all tip labels found in tree layout:")
10101010
cat(tips[!unmatched])
10111011
}
10121012

10131013
# find most recent common ancestor
1014-
idx <- sapply(tips, function(l) which(layout$nodes$label==l))
1015-
traj <- lapply(idx, function(i) .climb.tree(i, edges))
1014+
idx <- sapply(tips, function(l) which(obj$nodes$label==l))
1015+
traj <- lapply(idx, function(i) .climb.tree(i, obj$edges))
10161016
common <- Reduce(intersect, traj)
10171017
if (length(common) < 1) {
10181018
stop("Error: Failed to locate common ancestor of tips ", tips)
10191019
}
10201020

10211021
clade <- setdiff(Reduce(union, traj), common)
1022-
e <- layout$edges[is.element(layout$edges$child, clade), ]
1023-
segments(e$x0, e$y0, e$x1, e$y1, col, ...)
1024-
1025-
# do tips as well
1026-
e <- layout$edges[is.element(layout$edges$child, idx), ]
1022+
e <- obj$edges[is.element(obj$edges$child, c(idx, clade)), ]
10271023
segments(e$x0, e$y0, e$x1, e$y1, col, ...)
1024+
1025+
if (obj$layout == 'rectangular') {
1026+
parents <- sapply(e$parent, function(p) which(obj$edges$child==p))
1027+
segments(x0=e$x0, y0=e$y0, y1=obj$edges$y0[parents], col=col, ...)
1028+
}
1029+
else if (obj$layout == 'radial') {
1030+
nodes <- obj$nodes[e$child,]
1031+
parents <- sapply(e$parent, function(p) which(obj$edges$child==p))
1032+
pnodes <- obj$nodes[obj$edges$child[parents],]
1033+
for (i in 1:nrow(nodes)) {
1034+
draw.arc(x=0, y=0, theta0=nodes$angle[i], theta1=pnodes$angle[i],
1035+
r0=pnodes$r[i], col=col, ...)
1036+
}
1037+
}
10281038
}

0 commit comments

Comments
 (0)