/
dendro_data.R
128 lines (119 loc) · 3.34 KB
/
dendro_data.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
#
# ggdendro/R/dendro_tree.R by Andrie de Vries Copyright (C) 2011-2015
#
# 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 cluster data from a model into a list of data frames.
#'
#' This function provides a generic mechanism to extract relevant plotting data,
#' typically line segments and labels, from a variety of cluster models.
#'
#' For [stats::dendrogram()] and `tree::tree()` models, extracts line segment
#' data and labels.
#'
#'
#' @param model object of type [stats::hclust()], [stats::dendrogram()] or
#' [tree::tree()]
#'
#' @param ... ignored
#'
#' @export dendro_data dendro_data.default
#'
#' @return a list of data frames that contain the data appropriate to each
#' cluster model
#'
#' @seealso There are several implementations for specific cluster algorithms:
#'
#' - [dendro_data.hclust()]
#' - [dendro_data.dendrogram()]
#' - [dendro_data.tree()]
#' - [dendro_data.rpart()]
#'
#' To extract the data for line segments, labels or leaf labels use:
#'
#' - [segment()]: the line segment data
#' - [label()]: the text for each end segment
#' - [leaf_label()]: the leaf labels of a tree diagram
#'
dendro_data <- function(model, ...) {
UseMethod("dendro_data", model)
}
#' @rdname dendro_data
#' @export
dendro_data.default <- function(model, ...) {
x <- class(model)
stop(paste("No dendro_data method defined for class", x))
return(NULL)
}
#' Tests whether an object is of class dendro.
#'
#' Is a dendro? Tests whether an object is of class dendro.
#'
#' @param x Object to check
#' @export
#' @seealso [dendro_data()] and [ggdendro-package()]
is.dendro <- function(x) {
inherits(x, "dendro")
}
#' Coerces object to class dendro.
#'
#' Method for coercing object to class dendro.
#'
#' @param segments data.frame with segment data
#'
#' @param labels data.frame with labels data
#'
#' @param leaf_labels data.frame with leaf label data
#'
#' @param class The class of the original model object, e.g. "hclust". This is
#' used by [ggdendrogram()] to determine the angle and justification
#' of labels
#'
#' @seealso [dendro_data()] and [ggdendro-package()]
#'
#' @export
as.dendro <- function(segments, labels, leaf_labels = NULL, class) {
if (missing(class)) stop("Missing class in as.dendro")
x <- list(
segments = segments,
labels = labels,
leaf_labels = leaf_labels,
class = class
)
class(x) <- "dendro"
x
}
#' Returns segment, label or leaf-label data from dendro object.
#'
#' `segment` extracts line segments, `label` extracts labels, and `leaf_label`
#' extracts leaf labels from a dendro object.
#'
#' @param x dendro object
#' @aliases segment label leaf_label
#' @seealso [dendro_data()]
#' @export
segment <- function(x) {
x$segments
}
#' @rdname segment
#' @export
label <- function(x) {
x$labels
}
#' @rdname segment
#' @export
leaf_label <- function(x) {
x$leaf_labels
}