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