| 1 |
# Copyright (C) Tal Galili |
| 2 |
# |
| 3 |
# This file is part of dendextend. |
| 4 |
# |
| 5 |
# dendextend is free software: you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation, either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# dendextend is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# A copy of the GNU General Public License is available at |
| 16 |
# http://www.r-project.org/Licenses/ |
| 17 |
# |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
# We had to write a stripped down version of the following functions: |
| 23 |
# stats:::plot.dendrogram |
| 24 |
# stats:::plotNodeLimit |
| 25 |
# stats:::plotNode |
| 26 |
# stats:::.memberDend(dend) |
| 27 |
# stats:::.midDend(dend) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#' @title Get the x-y coordinates of a dendrogram's nodes |
| 33 |
#' @description Get the x-y coordinates of a dendrogram's nodes. Can be used to add text or images on the tree. |
| 34 |
#' @export |
| 35 |
#' @param dend a dendrogram object |
| 36 |
#' @param type type of plot. |
| 37 |
#' @param center logical; if TRUE, nodes are plotted centered with respect to the |
| 38 |
#' leaves in the branch. Otherwise (default), |
| 39 |
#' plot them in the middle of all direct child nodes. |
| 40 |
#' @param horiz logical indicating if the dendrogram should be drawn horizontally or not. |
| 41 |
#' @param ... not used |
| 42 |
#' @source This is a striped down version of the |
| 43 |
#' function \code{\link{plot.dendrogram}}. |
| 44 |
#' It performs (almost) the same task, only it does not do any plotting |
| 45 |
#' but it does save the x-y coordiantes of the nodes. |
| 46 |
#' @return |
| 47 |
#' A 2-dimensional matrix, with rows as the number of nodes, |
| 48 |
#' and the first column is the x location, while the second is the |
| 49 |
#' y location. |
| 50 |
#' @seealso \link{get_nodes_attr}, \link{nnodes}, |
| 51 |
#' \link{nleaves} |
| 52 |
#' @examples |
| 53 |
#' \dontrun{ |
| 54 |
#' |
| 55 |
#' # If we would like to see the numbers from plot: |
| 56 |
#' # ?getOption("verbose") |
| 57 |
#' # options(verbose=TRUE) |
| 58 |
#' # options(verbose=FALSE) |
| 59 |
#' |
| 60 |
#' # ----- |
| 61 |
#' # Draw a depth first search illustration |
| 62 |
#' # ----- |
| 63 |
#' |
| 64 |
#' dend <- 1:5 %>% |
| 65 |
#' dist() %>% |
| 66 |
#' hclust() %>% |
| 67 |
#' as.dendrogram() |
| 68 |
#' get_nodes_xy(dend) |
| 69 |
#' |
| 70 |
#' # polygon(get_nodes_xy(dend), col = 2) |
| 71 |
#' plot(dend, |
| 72 |
#' leaflab = "none", |
| 73 |
#' main = "Depth-first search in a dendrogram" |
| 74 |
#' ) |
| 75 |
#' xy <- get_nodes_xy(dend) |
| 76 |
#' for (i in 1:(nrow(xy) - 1)) { |
| 77 |
#' arrows(xy[i, 1], xy[i, 2], |
| 78 |
#' angle = 17, |
| 79 |
#' length = .5, |
| 80 |
#' xy[i + 1, 1], xy[i + 1, 2], |
| 81 |
#' lty = 1, col = 3, lwd = 1.5 |
| 82 |
#' ) |
| 83 |
#' } |
| 84 |
#' points(xy, pch = 19, cex = 4) |
| 85 |
#' text(xy, labels = 1:nnodes(dend), cex = 1.2, col = "white", adj = c(0.4, 0.4)) |
| 86 |
#' } |
| 87 |
get_nodes_xy <- function(dend, type = c("rectangle", "triangle"), center = FALSE, |
| 88 |
horiz = FALSE, ...) { |
| 89 |
x <- dend # TODO: this code could be cleaned |
| 90 |
|
| 91 |
# this function/env let's us keep a "stack" of the xy matrix, and update it |
| 92 |
# no matter how deep we get in the recursion... |
| 93 |
nodes_xy <- local({ |
| 94 |
|
| 95 |
# notice how we must create this function within get_nodes_xy |
| 96 |
# since it relies on the specific value of "x" when first created. |
| 97 |
xy_matrix <- matrix(0, nrow = nnodes(x), ncol = 2) |
| 98 |
i_node <- 0 |
| 99 |
|
| 100 |
function(xy) { |
| 101 |
if (missing(xy)) { |
| 102 |
return(with(environment(nodes_xy), xy_matrix)) |
| 103 |
} |
| 104 |
|
| 105 |
# next node |
| 106 |
i_node <<- i_node + 1 |
| 107 |
xy_matrix[i_node, ] <<- xy |
| 108 |
} |
| 109 |
}) |
| 110 |
|
| 111 |
|
| 112 |
type <- match.arg(type) |
| 113 |
hgt <- attr(x, "height") |
| 114 |
mem.x <- nleaves(x) |
| 115 |
|
| 116 |
if (center) { |
| 117 |
x1 <- 0.5 |
| 118 |
x2 <- mem.x + 0.5 |
| 119 |
} |
| 120 |
else { |
| 121 |
x1 <- 1 |
| 122 |
x2 <- mem.x |
| 123 |
} |
| 124 |
|
| 125 |
plotNode2(x1, x2, x, |
| 126 |
type = type, center = center, leaflab = "none", |
| 127 |
horiz = horiz, |
| 128 |
nodes_xy = nodes_xy |
| 129 |
) |
| 130 |
|
| 131 |
return(nodes_xy()) |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
plotNodeLimit2 <- function(x1, x2, subtree, center) { |
| 152 |
inner <- !is.leaf(subtree) && x1 != x2 |
| 153 |
if (inner) { |
| 154 |
K <- length(subtree) |
| 155 |
mTop <- nleaves(subtree) |
| 156 |
limit <- integer(K) |
| 157 |
xx1 <- x1 |
| 158 |
for (k in 1L:K) { |
| 159 |
m <- nleaves(subtree[[k]]) |
| 160 |
xx1 <- xx1 + (if (center) { |
| 161 |
(x2 - x1) * m / mTop |
| 162 |
} else { |
| 163 |
m |
| 164 |
}) |
| 165 |
limit[k] <- xx1 |
| 166 |
} |
| 167 |
limit <- c(x1, limit) |
| 168 |
} |
| 169 |
else { |
| 170 |
limit <- c(x1, x2) |
| 171 |
} |
| 172 |
mid <- attr(subtree, "midpoint") |
| 173 |
center <- center || (inner && !is.numeric(mid)) |
| 174 |
x <- if (center) { |
| 175 |
mean(c(x1, x2)) |
| 176 |
} else { |
| 177 |
x1 + (if (inner) { |
| 178 |
mid |
| 179 |
} else { |
| 180 |
0 |
| 181 |
}) |
| 182 |
} |
| 183 |
list(x = x, limit = limit) |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
.midDend2 <- function(x) |
| 189 |
if (is.null(mp <- attr(x, "midpoint"))) 0 else mp |
| 190 |
|
| 191 |
|
| 192 |
plotNode2 <- function(x1, x2, subtree, type, center, leaflab, horiz = FALSE, nodes_xy) { |
| 193 |
inner <- !is.leaf(subtree) && x1 != x2 |
| 194 |
yTop <- attr(subtree, "height") |
| 195 |
bx <- plotNodeLimit2(x1, x2, subtree, center) |
| 196 |
xTop <- bx$x |
| 197 |
|
| 198 |
if (getOption("verbose")) { |
| 199 |
cat(if (inner) { |
| 200 |
"inner node" |
| 201 |
} else { |
| 202 |
"leaf" |
| 203 |
}, ":") |
| 204 |
cat(if (inner) { |
| 205 |
paste(" height", formatC(yTop), "; ") |
| 206 |
}, "(x1,x2)= (", |
| 207 |
formatC(x1, width = 4), ",", formatC(x2, width = 4), |
| 208 |
")", "--> xTop=", formatC(xTop, width = 8), "\n", |
| 209 |
sep = "" |
| 210 |
) |
| 211 |
} |
| 212 |
|
| 213 |
# update the xy coordinates using a function defined in the beginning! |
| 214 |
nodes_xy(c(xTop, yTop)) |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
i <- if (inner) { |
| 219 |
1 |
| 220 |
} else { |
| 221 |
2 |
| 222 |
} |
| 223 |
|
| 224 |
if (inner) { |
| 225 |
for (k in seq_along(subtree)) { |
| 226 |
child <- subtree[[k]] |
| 227 |
yBot <- attr(child, "height") |
| 228 |
if (getOption("verbose")) { |
| 229 |
cat("ch.", k, "@ h=", yBot, "; ") |
| 230 |
} |
| 231 |
if (is.null(yBot)) { |
| 232 |
yBot <- 0 |
| 233 |
} |
| 234 |
xBot <- if (center) { |
| 235 |
mean(bx$limit[k:(k + 1)]) |
| 236 |
} else { |
| 237 |
bx$limit[k] + .midDend2(child) |
| 238 |
} |
| 239 |
|
| 240 |
i <- if (!is.leaf(child)) { |
| 241 |
1 |
| 242 |
} else { |
| 243 |
2 |
| 244 |
} |
| 245 |
|
| 246 |
vln <- NULL |
| 247 |
|
| 248 |
if (!is.null(attr(child, "edgetext"))) { |
| 249 |
if (getOption("verbose")) { |
| 250 |
# cat("-- with \"edgetext\"", format(edgeText)) |
| 251 |
if (!is.null(vln)) { |
| 252 |
mx <- if (type == "triangle") { |
| 253 |
(xTop + xBot + ((xTop - xBot) / (yTop - yBot)) * |
| 254 |
vln) / 2 |
| 255 |
} else { |
| 256 |
xBot |
| 257 |
} |
| 258 |
my <- (yTop + yBot + 2 * vln) / 2 |
| 259 |
} |
| 260 |
else { |
| 261 |
mx <- if (type == "triangle") { |
| 262 |
(xTop + xBot) / 2 |
| 263 |
} else { |
| 264 |
xBot |
| 265 |
} |
| 266 |
my <- (yTop + yBot) / 2 |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
# vlm <- strheight(c(edgeText, "h"), cex = t.cex)/2 |
| 271 |
# hlm <- strwidth(c(edgeText, "m"), cex = t.cex)/2 |
| 272 |
# hl3 <- c(hlm[1L], hlm[1L] + hlm[2L], hlm[1L]) |
| 273 |
} |
| 274 |
plotNode2(bx$limit[k], bx$limit[k + 1], |
| 275 |
subtree = child, |
| 276 |
type, center, leaflab, horiz, nodes_xy = nodes_xy |
| 277 |
) |
| 278 |
} |
| 279 |
} |
| 280 |
invisible() |
| 281 |
} |