Debian Codegraph @

R/ggdendro.R

raw
1 # Copyright (C) Tal Galili
2 #
3 # This file is part of dendextend.
4 #
5 # dendextend is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # dendextend is distributed in the hope that it will be useful, but
11 # 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 # Run first so that it would have access to all of these functions from stats.
20 # source("R\\stats_imports.R")
21
22 # This is a copy (import) of the function dendrogram_data, from Andrie de Vries's ggdendro package (which is a modified \link{plot.dendrogram}).
23 # https://cran.r-project.org/package=ggdendro
24 # This function is the basis for the as.ggdend function.
25 dendrogram_data <- function(x, type = c("rectangle", "triangle"), ...) {
26 # dev.new()
27 # plot(x) # we must add a new plot of the dend in order to extract features from it!
28
29 # without the following line, we will get:
30 # Error in plot.xy(xy.coords(x, y), type = type, ...) :
31 # plot.new has not been called yet
32 # unless a regular plot device was first initiated.
33 # this is resolved by clearing the nodes from attributes!
34 x <- x %>%
35 remove_branches_edgePar() %>%
36 remove_nodes_nodePar()
37 # x %>% remove_branches_edgePar %>% remove_nodes_nodePar %>% unclass
38
39 leaflab <- "perpendicular"
40 center <- FALSE
41 xlab <- ""
42 ylab <- ""
43 horiz <- FALSE
44 xaxt <- "n"
45 yaxt <- "s"
46 nodePar <- NULL
47 edgePar <- list()
48 dLeaf <- NULL
49 edge.root <- is.leaf(x) || !is.null(attr(x, "edgetext"))
50 type <- match.arg(type)
51 hgt <- attr(x, "height")
52 if (edge.root && is.logical(edge.root)) {
53 edge.root <- 0.0625 * if (is.leaf(x)) {
54 1
55 } else {
56 hgt
57 }
58 }
59 mem.x <- .memberDend(x)
60 yTop <- hgt + edge.root
61 if (center) {
62 x1 <- 0.5
63 x2 <- mem.x + 0.5
64 }
65 else {
66 x1 <- 1
67 x2 <- mem.x
68 }
69 xl. <- c(x1 - 1 / 2, x2 + 1 / 2)
70 yl. <- c(0, yTop)
71 if (edge.root) {
72 if (!is.null(et <- attr(x, "edgetext"))) {
73 my <- mean(hgt, yTop)
74 }
75 }
76 gg.plotNode <- function(x1, x2, subtree, type, center, leaflab,
77 dLeaf, nodePar, edgePar, horiz = FALSE, ddsegments = NULL,
78 ddlabels = NULL) {
79 inner <- !is.leaf(subtree) && x1 != x2
80 yTop <- attr(subtree, "height")
81 bx <- plotNodeLimit(x1, x2, subtree, center)
82 xTop <- bx$x
83 hasP <- !is.null(nPar <- attr(subtree, "nodePar"))
84 if (!hasP) {
85 nPar <- nodePar
86 }
87 Xtract <- function(nam, L, default, indx) rep(if (nam %in%
88 names(L)) {
89 L[[nam]]
90 } else {
91 default
92 }, length.out = indx)[indx]
93 asTxt <- function(x) {
94 if (is.character(x) || is.expression(x)) {
95 x
96 } else if (is.null(x)) {
97 ""
98 } else {
99 as.character(x)
100 }
101 }
102 i <- if (inner || hasP) {
103 1
104 } else {
105 2
106 }
107 if (!is.null(nPar)) {
108 pch <- Xtract("pch", nPar, default = 1L:2, i)
109 cex <- Xtract("cex", nPar, default = c(1, 1), i)
110 col <- Xtract("col", nPar,
111 default = par("col"),
112 i
113 )
114 bg <- Xtract("bg", nPar, default = par("bg"), i)
115 points(if (horiz) {
116 cbind(yTop, xTop)
117 } else {
118 cbind(xTop, yTop)
119 },
120 pch = pch, bg = bg, col = col,
121 cex = cex
122 )
123 }
124 lab.cex <- 1
125 if (is.leaf(subtree)) {
126 if (leaflab == "perpendicular") {
127 Y <- yTop - dLeaf * lab.cex
128 X <- xTop
129 srt <- 90
130 adj <- 1
131 nodeText <- asTxt(attr(subtree, "label"))
132 ddlabels <- rbind(ddlabels, data.frame(
133 x = X,
134 y = 0, text = nodeText, stringsAsFactors = TRUE
135 ))
136 }
137 }
138 else if (inner) {
139 segmentsHV <- function(x0, y0, x1, y1) {
140 data.frame(x0, y0, x1, y1, stringsAsFactors = TRUE)
141 }
142 for (k in seq_along(subtree)) {
143 child <- subtree[[k]]
144 yBot <- attr(child, "height")
145 if (getOption("verbose")) {
146 cat("ch.", k, "@ h=", yBot, "; ")
147 }
148 if (is.null(yBot)) {
149 yBot <- 0
150 }
151 xBot <- if (center) {
152 mean(bx$limit[k:(k + 1)])
153 } else {
154 bx$limit[k] + .midDend(child)
155 }
156 if (type == "triangle") {
157 ddsegments <- rbind(ddsegments, segmentsHV(
158 xTop,
159 yTop, xBot, yBot
160 ))
161 }
162 else {
163 ddsegments <- rbind(ddsegments, segmentsHV(
164 xTop,
165 yTop, xBot, yTop
166 ))
167 ddsegments <- rbind(ddsegments, segmentsHV(
168 xBot,
169 yTop, xBot, yBot
170 ))
171 }
172 vln <- NULL
173 if (!is.null(attr(child, "edgetext"))) {
174 edgeText <- asTxt(attr(child, "edgetext"))
175 if (!is.null(vln)) {
176 mx <- if (type == "triangle") {
177 (xTop + xBot + ((xTop - xBot) / (yTop - yBot)) *
178 vln) / 2
179 } else {
180 xBot
181 }
182 my <- (yTop + yBot + 2 * vln) / 2
183 }
184 else {
185 mx <- if (type == "triangle") {
186 (xTop + xBot) / 2
187 } else {
188 xBot
189 }
190 my <- (yTop + yBot) / 2
191 }
192 }
193 plotNode_result <- gg.plotNode(bx$limit[k], bx$limit[k +
194 1],
195 subtree = child, type, center, leaflab,
196 dLeaf, nodePar, edgePar, horiz, ddsegments,
197 ddlabels
198 )
199 ddsegments <- plotNode_result$segments
200 ddlabels <- plotNode_result$labels
201 }
202 }
203 return(list(segments = ddsegments, labels = ddlabels))
204 }
205 ret <- gg.plotNode(x1, x2, x,
206 type = type, center = center,
207 leaflab = leaflab, dLeaf = dLeaf, nodePar = nodePar,
208 edgePar = edgePar, horiz = FALSE, ddsegments = NULL,
209 ddlabels = NULL
210 )
211 names(ret$segments) <- c("x", "y", "xend", "yend")
212 names(ret$labels) <- c("x", "y", "label")
213
214
215
216 # dev.off() # Turn off device!
217
218 ret
219 }
220
221
222
223
224
225
226 #' @title Creates completely blank theme in ggplot
227 #' @export
228 #' @description
229 #' Sets most of the ggplot options to blank, by returning blank theme elements for the panel grid, panel background, axis title, axis text, axis line and axis ticks.
230 #' @author Andrie de Vries
231 #' @seealso \link{ggdend}
232 #' @source
233 #'
234 #' This function is from Andrie de Vries's ggdendro package.
235 #'
236 #' The motivation for this fork is the need to add more graphical parameters to the plotted tree.
237 #' This required a strong mixter of functions from ggdendro and dendextend (to the point that
238 #' it seemed better to just fork the code into its current form)
239 #'
240 theme_dendro <- function() {
241 # library(ggplot2)
242 element_blank <- ggplot2::element_blank
243 element_text <- ggplot2::element_text
244 theme <- ggplot2::theme
245
246 theme(
247 panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
248 panel.background = element_blank(), axis.title.x = element_text(colour = NA),
249 axis.title.y = element_blank(), axis.text.x = element_blank(),
250 axis.text.y = element_blank(), axis.line = element_blank(),
251 axis.ticks = element_blank()
252 )
253 }