-
Notifications
You must be signed in to change notification settings - Fork 11
/
dendrogram.R
171 lines (154 loc) · 4.77 KB
/
dendrogram.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#
# ggdendro/R/dendro_tree.R by Andrie de Vries Copyright (C) 2011-2015
# Contains code adapted from stats/hclust.R Copyright (C) 1995-2015 The R Core Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 or 3 of the License
# (at your option).
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available at
# http://www.r-project.org/Licenses/
#
#
#' Extract line segment and label data from dendrogram or hclust object.
#'
#' Extract line segment and label data from [stats::dendrogram()] or
#' [stats::hclust()] object. The resulting object is a list of data frames
#' containing line segment data and label data.
#'
#' @param model object of class "dendrogram", e.g. the output of as.dendrogram()
#'
#' @param type The type of plot, indicating the shape of the dendrogram.
#' "rectangle" will draw rectangular lines, while "triangle" will draw
#' triangular lines.
#'
#' @param ... ignored
#'
#'
#' @export
#' @return
#' A list with components:
#' \item{segments}{Line segment data}
#' \item{labels}{Label data}
#'
#' @seealso [ggdendrogram()]
#' @rdname dendro_data
#' @family dendro_data methods
#' @family dendrogram/hclust functions
#' @example inst/examples/example_dendro_data.R
#'
dendro_data.dendrogram <- function(model, type = c("rectangle", "triangle"), ...) {
hcdata <- dendrogram_data(model, type = type, ...)
as.dendro(
segments = hcdata$segments,
labels = hcdata$labels,
class = "dendrogram"
)
}
#' @rdname dendro_data
#' @export
dendro_data.hclust <- function(model, type = c("rectangle", "triangle"), ...) {
dhc <- as.dendrogram(model)
hcdata <- dendrogram_data(dhc, type = type, ...)
as.dendro(
segments = hcdata$segments,
labels = hcdata$labels,
class = "hclust"
)
}
#' @rdname dendro_data
#' @example inst/examples/example_dendro_twins.R
#' @export
dendro_data.twins <- function(model, type = c("rectangle", "triangle"), ...) {
dhc <- as.dendrogram(model)
hcdata <- dendrogram_data(dhc, type = type, ...)
as.dendro(
segments = hcdata$segments,
labels = hcdata$labels,
class = "hclust"
)
}
#' Extract data frame from dendrogram object for plotting using ggplot.
#'
#' Extract data frame from dendrogram object for plotting using ggplot
#'
#' @param x object of class "dendrogram", e.g. the output of as.dendrogram()
#'
#' @param type The type of plot, indicating the shape of the dendrogram:
#' "rectangle" will draw rectangular lines, while "triangle" will draw
#' triangular lines.
#'
#' @param ... ignored
#' @seealso [ggdendrogram()]
#' @family dendro_data methods
#' @family dendrogram/hclust functions
#' @keywords internal
dendrogram_data <- function(x, type = c("rectangle", "triangle"), ...) {
# Initialise variables that used to be in parameter list
leaflab <- "perpendicular"
center <- FALSE
xlab <- ""
ylab <- ""
horiz <- FALSE
# frame.plot <- FALSE
xaxt <- "n"
yaxt <- "s"
nodePar <- NULL
edgePar <- list()
dLeaf <- NULL
edge.root <- is.leaf(x) || !is.null(attr(x, "edgetext"))
type <- match.arg(type)
# leaflab <- match.arg(leaflab)
hgt <- attr(x, "height")
if (edge.root && is.logical(edge.root)) {
edge.root <- 0.0625 * if (is.leaf(x)) 1 else hgt
}
mem.x <- .memberDend(x)
yTop <- hgt + edge.root
if (center) {
x1 <- 0.5
x2 <- mem.x + 0.5
}
else {
x1 <- 1
x2 <- mem.x
}
xl. <- c(x1 - 1 / 2, x2 + 1 / 2)
yl. <- c(0, yTop)
# if (missing(xlim) || is.null(xlim)) xlim <- xl.
# if (missing(ylim) || is.null(ylim)) ylim <- yl.
# plot(0, xlim = xlim, ylim = ylim, type = "n", xlab = xlab,
# ylab = ylab, xaxt = xaxt, yaxt = yaxt, frame.plot = frame.plot,
# ...)
# if (is.null(dLeaf))
# dLeaf <- 0.75 * (if (horiz)
# strwidth("w")
# else strheight("x"))
if (edge.root) {
# x0 <- stats:::plotNodeLimit(x1, x2, x, center)$x
# (x0, hgt, x0, yTop)
if (!is.null(et <- attr(x, "edgetext"))) {
my <- mean(hgt, yTop)
# text(x0, my, et)
}
}
ret <- plotNode(x1, x2, x,
type = type, center = center, leaflab = leaflab,
dLeaf = dLeaf, nodePar = nodePar, edgePar = edgePar, horiz = FALSE
)
ret$segments <- as.data.frame(matrix(
ret$segments, ncol = 4, byrow = TRUE,
dimnames = list(NULL, c("x", "y", "xend", "yend"))
))
ret$labels <- cbind(
as.data.frame(matrix(ret$labels$xy, ncol = 2, byrow = TRUE, dimnames = list(NULL, c("x", "y")))),
data.frame(label = ret$labels$text)
)
ret
}