| 1 |
# modified version of stats:::plotNode |
| 2 |
plotNode <- function (x1, x2, subtree, type, center, leaflab, dLeaf, nodePar, |
| 3 |
edgePar, horiz = FALSE) |
| 4 |
{ |
| 5 |
ddsegments <- NULL |
| 6 |
ddlabels <- list() |
| 7 |
|
| 8 |
wholetree <- subtree |
| 9 |
depth <- 0L |
| 10 |
llimit <- list() |
| 11 |
KK <- integer() |
| 12 |
kk <- integer() |
| 13 |
repeat { |
| 14 |
inner <- !is.leaf(subtree) && x1 != x2 |
| 15 |
yTop <- attr(subtree, "height") |
| 16 |
bx <- plotNodeLimit(x1, x2, subtree, center) |
| 17 |
xTop <- bx$x |
| 18 |
depth <- depth + 1L |
| 19 |
llimit[[depth]] <- bx$limit |
| 20 |
hasP <- !is.null(nPar <- attr(subtree, "nodePar")) |
| 21 |
if (!hasP) |
| 22 |
nPar <- nodePar |
| 23 |
if (getOption("verbose")) { |
| 24 |
cat(if (inner) |
| 25 |
"inner node" |
| 26 |
else "leaf", ":") |
| 27 |
if (!is.null(nPar)) { |
| 28 |
cat(" with node pars\n") |
| 29 |
utils::str(nPar) |
| 30 |
} |
| 31 |
cat(if (inner) |
| 32 |
paste(" height", formatC(yTop), "; "), "(x1,x2)= (", |
| 33 |
formatC(x1, width = 4), ",", formatC(x2, width = 4), |
| 34 |
")", "--> xTop=", formatC(xTop, width = 8), "\n", |
| 35 |
sep = "") |
| 36 |
} |
| 37 |
Xtract <- function(nam, L, default, indx) |
| 38 |
rep(if (nam %in% names(L)) L[[nam]] else default, length.out = indx)[indx] |
| 39 |
asTxt <- function(x) if (is.character(x) || is.expression(x) || is.null(x)) x else as.character(x) |
| 40 |
i <- if (inner || hasP) 1 else 2 |
| 41 |
if (!is.null(nPar)) { |
| 42 |
pch <- Xtract("pch", nPar, default = 1L:2, i) |
| 43 |
cex <- Xtract("cex", nPar, default = c(1, 1), i) |
| 44 |
col <- Xtract("col", nPar, default = par("col"), |
| 45 |
i) |
| 46 |
bg <- Xtract("bg", nPar, default = par("bg"), i) |
| 47 |
points(if (horiz) |
| 48 |
cbind(yTop, xTop) |
| 49 |
else cbind(xTop, yTop), pch = pch, bg = bg, col = col, |
| 50 |
cex = cex) |
| 51 |
} |
| 52 |
if (leaflab == "textlike") |
| 53 |
p.col <- Xtract("p.col", nPar, default = "white", i) |
| 54 |
lab.col <- Xtract("lab.col", nPar, default = par("col"), i) |
| 55 |
lab.cex <- Xtract("lab.cex", nPar, default = c(1, 1), i) |
| 56 |
lab.font <- Xtract("lab.font", nPar, default = par("font"), i) |
| 57 |
lab.xpd <- Xtract("xpd", nPar, default = c(TRUE, TRUE), i) |
| 58 |
if (is.leaf(subtree)) { |
| 59 |
if (leaflab == "perpendicular") { |
| 60 |
if (horiz) { |
| 61 |
X <- yTop + dLeaf * lab.cex |
| 62 |
Y <- xTop |
| 63 |
srt <- 0 |
| 64 |
adj <- c(0, 0.5) |
| 65 |
} |
| 66 |
else { |
| 67 |
Y <- yTop - dLeaf * lab.cex |
| 68 |
X <- xTop |
| 69 |
srt <- 90 |
| 70 |
adj <- 1 |
| 71 |
} |
| 72 |
nodeText <- asTxt(attr(subtree, "label")) |
| 73 |
# text(X, Y, nodeText, xpd = lab.xpd, srt = srt, |
| 74 |
# adj = adj, cex = lab.cex, col = lab.col, font = lab.font) |
| 75 |
ddlabels$xy <- c(ddlabels$xy, X, 0) |
| 76 |
ddlabels$text <- c(ddlabels$text, nodeText) |
| 77 |
} |
| 78 |
} |
| 79 |
else if (inner) { |
| 80 |
# segmentsHV <- function(x0, y0, x1, y1) { |
| 81 |
# if (horiz) |
| 82 |
# segments(y0, x0, y1, x1, col = col, lty = lty, lwd = lwd) |
| 83 |
# else segments(x0, y0, x1, y1, col = col, lty = lty, lwd = lwd) |
| 84 |
# } |
| 85 |
for (k in seq_along(subtree)) { |
| 86 |
child <- subtree[[k]] |
| 87 |
yBot <- attr(child, "height") |
| 88 |
if (getOption("verbose")) |
| 89 |
cat("ch.", k, "@ h=", yBot, "; ") |
| 90 |
if (is.null(yBot)) |
| 91 |
yBot <- 0 |
| 92 |
xBot <- if (center) |
| 93 |
mean(bx$limit[k:(k + 1)]) |
| 94 |
else bx$limit[k] + .midDend(child) |
| 95 |
hasE <- !is.null(ePar <- attr(child, "edgePar")) |
| 96 |
if (!hasE) |
| 97 |
ePar <- edgePar |
| 98 |
i <- if (!is.leaf(child) || hasE) 1 else 2 |
| 99 |
col <- Xtract("col", ePar, default = par("col"), i) |
| 100 |
lty <- Xtract("lty", ePar, default = par("lty"), i) |
| 101 |
lwd <- Xtract("lwd", ePar, default = par("lwd"), i) |
| 102 |
if (type == "triangle") { |
| 103 |
# segmentsHV(xTop, yTop, xBot, yBot) |
| 104 |
# segmentsHV(xTop, yTop, xBot, yBot) |
| 105 |
# ddsegments <- rbind(ddsegments, data.frame(x = xTop, y = yTop, xend = xBot, yend = yBot)) |
| 106 |
ddsegments <- c(ddsegments, xTop, yTop, xBot, yBot) |
| 107 |
} |
| 108 |
else { |
| 109 |
# segmentsHV(xTop, yTop, xBot, yTop) |
| 110 |
# segmentsHV(xBot, yTop, xBot, yBot) |
| 111 |
# ddsegments <- rbind(ddsegments, data.frame(x = xTop, y = yTop, xend = xBot, yend = yTop)) |
| 112 |
# ddsegments <- rbind(ddsegments, data.frame(x = xBot, y = yTop, xend = xBot, yend = yBot)) |
| 113 |
ddsegments <- c(ddsegments, xTop, yTop, xBot, yTop) |
| 114 |
ddsegments <- c(ddsegments, xBot, yTop, xBot, yBot) |
| 115 |
} |
| 116 |
vln <- NULL |
| 117 |
# if (is.leaf(child) && leaflab == "textlike") { |
| 118 |
# nodeText <- asTxt(attr(child, "label")) |
| 119 |
# if (getOption("verbose")) |
| 120 |
# cat("-- with \"label\"", format(nodeText)) |
| 121 |
# hln <- 0.6 * strwidth(nodeText, cex = lab.cex)/2 |
| 122 |
# vln <- 1.5 * strheight(nodeText, cex = lab.cex)/2 |
| 123 |
# rect(xBot - hln, yBot, xBot + hln, yBot + 2 * |
| 124 |
# vln, col = p.col) |
| 125 |
# text(xBot, yBot + vln, nodeText, xpd = lab.xpd, |
| 126 |
# cex = lab.cex, col = lab.col, font = lab.font) |
| 127 |
# } |
| 128 |
# if (!is.null(attr(child, "edgetext"))) { |
| 129 |
# edgeText <- asTxt(attr(child, "edgetext")) |
| 130 |
# if (getOption("verbose")) |
| 131 |
# cat("-- with \"edgetext\"", format(edgeText)) |
| 132 |
# if (!is.null(vln)) { |
| 133 |
# mx <- if (type == "triangle") |
| 134 |
# (xTop + xBot + ((xTop - xBot)/(yTop - yBot)) * |
| 135 |
# vln)/2 |
| 136 |
# else xBot |
| 137 |
# my <- (yTop + yBot + 2 * vln)/2 |
| 138 |
# } |
| 139 |
# else { |
| 140 |
# mx <- if (type == "triangle") |
| 141 |
# (xTop + xBot)/2 |
| 142 |
# else xBot |
| 143 |
# my <- (yTop + yBot)/2 |
| 144 |
# } |
| 145 |
# p.col <- Xtract("p.col", ePar, default = "white", i) |
| 146 |
# p.border <- Xtract("p.border", ePar, default = par("fg"), i) |
| 147 |
# p.lwd <- Xtract("p.lwd", ePar, default = lwd, i) |
| 148 |
# p.lty <- Xtract("p.lty", ePar, default = lty, i) |
| 149 |
# t.col <- Xtract("t.col", ePar, default = col, i) |
| 150 |
# t.cex <- Xtract("t.cex", ePar, default = 1, i) |
| 151 |
# t.font <- Xtract("t.font", ePar, default = par("font"), i) |
| 152 |
# vlm <- strheight(c(edgeText, "h"), cex = t.cex)/2 |
| 153 |
# hlm <- strwidth(c(edgeText, "m"), cex = t.cex)/2 |
| 154 |
# hl3 <- c(hlm[1L], hlm[1L] + hlm[2L], hlm[1L]) |
| 155 |
# if (horiz) { |
| 156 |
# polygon(my + c(-hl3, hl3), mx + sum(vlm) * |
| 157 |
# c(-1L:1L, 1L:-1L), col = p.col, border = p.border, |
| 158 |
# lty = p.lty, lwd = p.lwd) |
| 159 |
# text(my, mx, edgeText, cex = t.cex, col = t.col, |
| 160 |
# font = t.font) |
| 161 |
# } |
| 162 |
# else { |
| 163 |
# polygon(mx + c(-hl3, hl3), my + sum(vlm) * |
| 164 |
# c(-1L:1L, 1L:-1L), col = p.col, border = p.border, |
| 165 |
# lty = p.lty, lwd = p.lwd) |
| 166 |
# text(mx, my, edgeText, cex = t.cex, col = t.col, |
| 167 |
# font = t.font) |
| 168 |
# } |
| 169 |
# } |
| 170 |
} |
| 171 |
} |
| 172 |
if (inner && length(subtree)) { |
| 173 |
KK[depth] <- length(subtree) |
| 174 |
if (storage.mode(kk) != storage.mode(KK)) |
| 175 |
storage.mode(kk) <- storage.mode(KK) |
| 176 |
kk[depth] <- 1L |
| 177 |
x1 <- bx$limit[1L] |
| 178 |
x2 <- bx$limit[2L] |
| 179 |
subtree <- subtree[[1L]] |
| 180 |
} |
| 181 |
else { |
| 182 |
repeat { |
| 183 |
depth <- depth - 1L |
| 184 |
if (!depth || kk[depth] < KK[depth]) |
| 185 |
break |
| 186 |
} |
| 187 |
if (!depth) |
| 188 |
break |
| 189 |
length(kk) <- depth |
| 190 |
kk[depth] <- k <- kk[depth] + 1L |
| 191 |
x1 <- llimit[[depth]][k] |
| 192 |
x2 <- llimit[[depth]][k + 1L] |
| 193 |
subtree <- wholetree[[kk]] |
| 194 |
} |
| 195 |
} |
| 196 |
# invisible() |
| 197 |
list( |
| 198 |
segments = ddsegments, |
| 199 |
labels = ddlabels |
| 200 |
) |
| 201 |
} |