Debian Codegraph @

R/dendro_data.R

raw
1 #
2 # ggdendro/R/dendro_tree.R by Andrie de Vries Copyright (C) 2011-2015
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 or 3 of the License
7 # (at your option).
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # A copy of the GNU General Public License is available at
15 # http://www.r-project.org/Licenses/
16 #
17 #
18
19
20 #' Extract cluster data from a model into a list of data frames.
21 #'
22 #' This function provides a generic mechanism to extract relevant plotting data,
23 #' typically line segments and labels, from a variety of cluster models.
24 #'
25 #' For [stats::dendrogram()] and `tree::tree()` models, extracts line segment
26 #' data and labels.
27 #'
28 #'
29 #' @param model object of type [stats::hclust()], [stats::dendrogram()] or
30 #' [tree::tree()]
31 #'
32 #' @param ... ignored
33 #'
34 #' @export dendro_data dendro_data.default
35 #'
36 #' @return a list of data frames that contain the data appropriate to each
37 #' cluster model
38 #'
39 #' @seealso There are several implementations for specific cluster algorithms:
40 #'
41 #' - [dendro_data.hclust()]
42 #' - [dendro_data.dendrogram()]
43 #' - [dendro_data.tree()]
44 #' - [dendro_data.rpart()]
45 #'
46 #' To extract the data for line segments, labels or leaf labels use:
47 #'
48 #' - [segment()]: the line segment data
49 #' - [label()]: the text for each end segment
50 #' - [leaf_label()]: the leaf labels of a tree diagram
51 #'
52 dendro_data <- function(model, ...) {
53 UseMethod("dendro_data", model)
54 }
55
56 #' @rdname dendro_data
57 #' @export
58 dendro_data.default <- function(model, ...) {
59 x <- class(model)
60 stop(paste("No dendro_data method defined for class", x))
61 return(NULL)
62 }
63
64 #' Tests whether an object is of class dendro.
65 #'
66 #' Is a dendro? Tests whether an object is of class dendro.
67 #'
68 #' @param x Object to check
69 #' @export
70 #' @seealso [dendro_data()] and [ggdendro-package()]
71 is.dendro <- function(x) {
72 inherits(x, "dendro")
73 }
74
75 #' Coerces object to class dendro.
76 #'
77 #' Method for coercing object to class dendro.
78 #'
79 #' @param segments data.frame with segment data
80 #'
81 #' @param labels data.frame with labels data
82 #'
83 #' @param leaf_labels data.frame with leaf label data
84 #'
85 #' @param class The class of the original model object, e.g. "hclust". This is
86 #' used by [ggdendrogram()] to determine the angle and justification
87 #' of labels
88 #'
89 #' @seealso [dendro_data()] and [ggdendro-package()]
90 #'
91 #' @export
92 as.dendro <- function(segments, labels, leaf_labels = NULL, class) {
93 if (missing(class)) stop("Missing class in as.dendro")
94 x <- list(
95 segments = segments,
96 labels = labels,
97 leaf_labels = leaf_labels,
98 class = class
99 )
100 class(x) <- "dendro"
101 x
102 }
103
104
105 #' Returns segment, label or leaf-label data from dendro object.
106 #'
107 #' `segment` extracts line segments, `label` extracts labels, and `leaf_label`
108 #' extracts leaf labels from a dendro object.
109 #'
110 #' @param x dendro object
111 #' @aliases segment label leaf_label
112 #' @seealso [dendro_data()]
113 #' @export
114 segment <- function(x) {
115 x$segments
116 }
117
118 #' @rdname segment
119 #' @export
120 label <- function(x) {
121 x$labels
122 }
123
124 #' @rdname segment
125 #' @export
126 leaf_label <- function(x) {
127 x$leaf_labels
128 }