Debian Codegraph @

R/ggdend.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 # source("R\\ggdendro.R")
22
23
24
25
26 #' @title Creates dendrogram plot using ggplot.
27 #' @rdname ggdend
28 #' @export
29 #'
30 #' @description
31 #' Several functions for creating a dendrogram plot using ggplot2.
32 #' The core process is to transform a dendrogram into a ggdend object using as.ggdend,
33 #' and then plot it using ggplot. These two steps can be done in one command with either the function
34 #' ggplot or ggdend.
35 #'
36 #' The reason we want to have as.ggdend (and not only ggplot.dendrogram), is (1) so that you could
37 #' create your own mapping of ggdend and, (2) since as.ggdend might be slow for large trees,
38 #' it is probably better to be able to run it only once for such cases.
39 #'
40 #' A ggdend class object is a list with 3 componants: segments, labels, nodes.
41 #' Each one contains the graphical parameters from the original dendrogram, but in a tabular form that
42 #' can be used by ggplot2+geom_segment+geom_text to create a dendrogram plot.
43 #'
44 #'
45 #'
46 #' @param dend a \link{dendrogram} tree (to be turned into a ggdend object)
47 #' @param type The type of plot, indicating the shape of the dendrogram. "rectangle" will draw
48 #' rectangular lines, while "triangle" will draw triangular lines.
49 #' @param edge.root currently ignored. One day it might do the following: logical; if true, draw an edge to the root node.
50 #' @param ... mostly ignored.
51 #' @param data,x a ggdend class object (passed to ggplot.dendrogram or print.ggdend).
52 #' @param segments a logical (TRUE) if to plot the segments (branches).
53 #' @param labels a logical (TRUE) if to plot the labels.
54 #' @param offset_labels a numeric value to offset the labels from the leaves
55 #' @param nodes a logical (TRUE) if to plot the nodes (points).
56 #' @param horiz a logical (TRUE) indicating if the dendrogram should be drawn horizontally or not.
57 #' @param theme the ggplot2 theme to use (default is \link{theme_dendro}, can also be NULL
58 #' for the default ggplot2 theme)
59 #' @param na.rm A logical (TRUE) to control removal of missing values. Passed to
60 #' \link[ggplot2]{geom_line} and \link[ggplot2]{geom_point}
61 #'
62 #' @param mapping (passed in ggplot.ggdend) Default list of aesthetic mappings to use for plot. If not specified, must be supplied in each layer added to the plot.
63 #' @param environment (passed in ggplot.ggdend) deprecated / ignored.
64 #'
65 #' @details
66 #'
67 #' \code{prepare.ggdend} is used by \code{plot.ggdend} to take the \code{ggdend} object
68 #' and prepare it for plotting. This is because the defaults of various parameters in \link{dendrogram}'s
69 #' are not always stored in the object itself, but are built-in into the \link{plot.dendrogram} function.
70 #' For example, the color of the labels is not (by default) specified in the dendrogram (only if we change it
71 #' from black to something else). Hence, when taking the object into a different plotting engine (say ggplot2), we
72 #' want to prepare the object by filling-in various defaults.
73 #' This function is autmatically invoked within the \code{plot.ggdend} function. You would probably use
74 #' it only if you'd wish to build your own ggplot2 mapping.
75 #'
76 #' @author Tal Galili, using code modified from Andrie de Vries
77 #'
78 #' @seealso
79 #'
80 #' \link{dendrogram}, \link{get_nodes_attr}, \link{get_leaves_nodePar},
81 #' \link[ggplot2]{ggplot},
82 #' \link[ggdendro]{ggdendrogram}, \link[ggdendro]{dendro_data},
83 #'
84 #' @return
85 #'
86 #' \itemize{
87 #' \item{\code{as.ggdend} - returns an object of class ggdend which is a list with 3 componants: segments, labels, nodes.
88 #' Each one contains the graphical parameters from the original dendrogram, but in a tabular form that
89 #' can be used by ggplot2+geom_segment+geom_text to create a dendrogram plot.}
90 #' \item{\code{prepare.ggdend} - a \code{ggdend} object (after filling it with various default values)}
91 #' \item{\code{ggplot.ggdend} - a \link[ggplot2]{ggplot} object}
92 #' }
93 #'
94 #' @source
95 #'
96 #' These are extended versions of the functions \link[ggdendro]{ggdendrogram}, \link[ggdendro]{dendro_data} (and the hidden dendrogram_data)
97 #' from Andrie de Vries's ggdendro package. The motivation for this fork is the need to add more graphical parameters
98 #' to the plotted tree. This required a strong mixter of functions from ggdendro and dendextend (to the point that
99 #' it seemed better to just fork the code into its current form)
100 #'
101 #' @examples
102 #'
103 #' \dontrun{
104 #'
105 #' library(dendextend)
106 #' # library(ggdendro)
107 #' # Create a complex dend:
108 #' dend <- iris[1:30, -5] %>%
109 #' dist() %>%
110 #' hclust() %>%
111 #' as.dendrogram() %>%
112 #' set("branches_k_color", k = 3) %>%
113 #' set("branches_lwd", c(1.5, 1, 1.5)) %>%
114 #' set("branches_lty", c(1, 1, 3, 1, 1, 2)) %>%
115 #' set("labels_colors") %>%
116 #' set("labels_cex", c(.9, 1.2))
117 #' # plot the dend in usual "base" plotting engine:
118 #' plot(dend)
119 #' # Now let's do it in ggplot2 :)
120 #' ggd1 <- as.ggdend(dend)
121 #' library(ggplot2)
122 #' ggplot(ggd1) # reproducing the above plot in ggplot2 :)
123 #'
124 #' # Triangle version:
125 #' plot(dend, type = "triangle")
126 #' ggd2 <- as.ggdend(dend, type = "triangle")
127 #' ggplot(ggd2)
128 #'
129 #'
130 #' # More modifications:
131 #' labels(dend) <- paste0(labels(dend), "00000")
132 #' ggd1 <- as.ggdend(dend)
133 #' # Use ylim to deal with long labels in ggplot2
134 #' ggplot(ggd1) + ylim(-.4, max(get_branches_heights(dend)))
135 #'
136 #'
137 #' ggplot(ggd1, horiz = TRUE) # horiz plot in ggplot2
138 #' # Adding some extra spice to it...
139 #' # creating a radial plot:
140 #' ggplot(ggd1) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta = "x")
141 #' # The text doesn't look so great, so let's remove it:
142 #' ggplot(ggd1, labels = FALSE) + scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta = "x")
143 #'
144 #' # This can now be sent to plot.ly - which adds zoom-in abilities, and more.
145 #' # Here is how it might look like: https://plot.ly/~talgalili/6/y-vs-x/
146 #'
147 #' ## Quick guide:
148 #' # install.packages("devtools")
149 #' # library("devtools")
150 #' # devtools::install_github("ropensci/plotly")
151 #' # library(plotly)
152 #' # set_credentials_file(...)
153 #' # you'll need to get it from here: https://plot.ly/ggplot2/getting-started/
154 #'
155 #' # ggplot(ggd1)
156 #' # py <- plotly()
157 #' # py$ggplotly()
158 #'
159 #' # And you'll get something like this: https://plot.ly/~talgalili/6/y-vs-x/
160 #'
161 #' # Another example: https://plot.ly/ggplot2/
162 #' }
163 ggdend <- function(...) {
164 cat("Use either as.ggdend or ggplot (or both). \n")
165 }
166
167
168
169
170 # I will need this...
171 allNA <- function(x) all(is.na(x))
172
173 #' @export
174 #' @rdname ggdend
175 as.ggdend <- function(dend, ...) {
176 UseMethod("as.ggdend")
177 }
178
179
180 #' @export
181 #' @rdname ggdend
182 as.ggdend.dendrogram <- function(dend, type = c("rectangle", "triangle"), edge.root = FALSE, ...) {
183
184
185 # Warning: This function is NOT the most optimized function. We would rather use it once and then work with the output (rather than calling it over and over...)
186
187 if (!is.dendrogram(dend)) stop("dend is not a dendrogram (and it needs to be...)")
188 if (nleaves(dend) == 0) stop("dend must have at least one node")
189 if (edge.root) stop("edge.root is not supported at this point (this parameter is a place-holder for when it will)")
190 type <- match.arg(type, c("rectangle", "triangle"))
191 # source('R/stats_imports.R', echo=FALSE)
192 # ggdata <- dendextend:::dendrogram_data(dend, type = "rectangle")
193 ggdata <- dendrogram_data(dend, type = type) # ggdendro:::dendrogram_data(dend)
194
195
196 # create nodes_xy
197 # ==========================
198 nodes_xy <- with(ggdata$segments, {
199 # > ggdata$segments
200 # x y xend yend
201 # 1 8.880859 2.0420578 3.25000 2.0420578
202 # 2 3.250000 2.0420578 3.25000 0.9219544
203 # 3 3.250000 0.9219544 1.50000 0.9219544
204 # x=ggdata$segments$x
205 id <- if(type == "rectangle") {
206 (seq_along(x) %% 2) == 0
207 } else {
208 TRUE
209 }
210 data.frame(
211 x = c(x[1], xend[id]), y = c(y[1], yend[id]),
212 pch = NA, cex = NA, col = NA,
213 members = NA, midpoint = NA, height = NA, leaf = NA,
214 stringsAsFactors = TRUE
215 )
216 })
217 # nnodes(dend) == nrow(nodes_xy) # sanity check
218
219
220
221
222 # dend %>% unclass %>% str
223
224 # add parameters to nodes_xy
225 # graphical parameters
226 nodes_attr <- get_nodes_attr(dend, "nodePar", simplify = FALSE)
227
228 # nodes_attr <- get_nodes_attr(d1, "nodePar", simplify = FALSE)
229 get_nodePar_attr_par <- function(par) {
230 # The rep is because a segment has two lines. So we use each: rep(1:4, each = 2)
231 # tmp <- rep(unlist(sapply(nodes_attr, `[`, name = par)), each = 1) # like doing edgePar_attr[[1]] ["col"]
232 values <- sapply(nodes_attr, `[`, name = par) # like doing edgePar_attr[[1]] ["col"]
233 null2NA <- function(x) ifelse(is.null(x), NA, x)
234 values <- sapply(values, null2NA) # in case the attr is missing, it fills the NULL with NA
235 # if(is.null(tmp)) tmp <- rep(NA, length(nodes_attr))
236 unlist(values)
237 }
238
239
240
241 nodes_xy$pch <- get_nodePar_attr_par("pch")
242 nodes_xy$cex <- get_nodePar_attr_par("cex")
243 nodes_xy$col <- get_nodePar_attr_par("col")
244
245 ##########################################
246
247
248
249
250 # others: (won't be used for plotts, but in the future someone might want to use them in some graphical way...)
251 nodes_xy$members <- get_nodes_attr(dend, "members")
252 nodes_xy$midpoint <- get_nodes_attr(dend, "midpoint")
253 nodes_xy$height <- get_nodes_attr(dend, "height")
254 nodes_xy$leaf <- get_nodes_attr(dend, "leaf")
255
256 # add nodes_xy
257 ggdata$nodes <- nodes_xy
258
259 # segments (edges) - add graphical parameters
260 # ==========================
261 edgePar_attr <- get_nodes_attr(dend, "edgePar", simplify = FALSE)
262 # [[1]]
263 # [1] NA
264 #
265 # [[2]]
266 # [[2]]$col
267 # [1] "#CC476B"
268 #
269 #
270 # [[3]]
271 # [[3]]$col
272 # [1] "#CC476B"
273 if (!edge.root) edgePar_attr <- edgePar_attr[-1] # remove the first edgePar since we don't support root edge at this point...
274 # default_par <- rep(NA, nrow(ggdata$segments))
275 # segments_par <- data.frame(col = default_par, lwd = default_par, lty = default_par)
276
277 # if(!allNA(edgePar_attr)) {
278 # edgePar_attr_names <- sapply(edgePar_attr, names)
279 # ss_col <- sapply(edgePar_attr_names, function(x) {"col" %in% x}) # no longer needed
280
281 # par is a character of the par to get from edgePar_attr
282 get_edgePar_attr_par <- function(par) {
283 values <- sapply(edgePar_attr, `[`, name = par)
284 # null2NA <- function(x) ifelse(is.na(x) || is.null(x), NA, x)
285 null2NA <- function(x) ifelse(is.null(x), NA, x)
286 values <- sapply(values, null2NA) # in case the attr is missing, it fills the NULL with NA
287 rep(unlist(values), each = if(type == "rectangle") 2 else 1) # like doing edgePar_attr[[1]] ["col"]
288 }
289
290 # The rep is because a segment has two lines. So we use each: rep(1:4, each = 2)
291
292 # length(get_edgePar_attr_par("col"))
293 # length(get_edgePar_attr_par("lwd"))
294 # dim(ggdata$segments)
295
296 ggdata$segments$col <- get_edgePar_attr_par("col") # like doing edgePar_attr[[1]] ["col"]
297 ggdata$segments$lwd <- get_edgePar_attr_par("lwd")
298 ggdata$segments$lty <- get_edgePar_attr_par("lty")
299 # }
300
301
302 # labels - add graphical
303 # ==========================
304
305
306
307 leaves_edgePar_attr <- get_leaves_nodePar(dend)
308 # [[1]]
309 # [[1]]$lab.col
310 # [1] 2
311 #
312 # [[1]]$pch
313 # [1] NA
314
315
316 get_leaves_edgePar_attr_par <- function(par) {
317 values <- sapply(leaves_edgePar_attr, `[`, name = par)
318 null2NA <- function(x) ifelse(is.null(x), NA, x)
319 values <- sapply(values, null2NA) # in case the attr is missing, it fills the NULL with NA
320 unlist(values) # like doing edgePar_attr[[1]] ["col"]
321 }
322
323
324
325 the_lab.col <- get_leaves_edgePar_attr_par("lab.col") # like doing edgePar_attr[[1]] ["col"]
326 the_lab.cex <- get_leaves_edgePar_attr_par("lab.cex") # like doing edgePar_attr[[1]] ["col"]
327 the_lab.height <- get_leaves_attr(dend, "height")
328 if (is.null(the_lab.col)) the_lab.col <- NA
329 if (is.null(the_lab.cex)) the_lab.cex <- NA
330
331 ggdata$labels$col <- the_lab.col
332 ggdata$labels$cex <- the_lab.cex
333 ggdata$labels$y <- the_lab.height
334 # The above saves us from errors such as:
335 # Error in eval(expr, envir, enclos) : object 'cex' not found
336 # In addition: Warning message:
337 # In is.na(data$labels$cex) :
338 # is.na() applied to non-(list or vector) of type 'NULL'
339
340
341 class(ggdata) <- "ggdend"
342 ggdata
343 }
344
345
346 # as.ggdend(dend)
347
348 # print.ggdend # maybe say how many labels, nodes, splits, and graphical parameters it has
349
350
351
352
353
354
355
356 # allows for the S3 to kick in, without calling ggplot2
357 # don't run this when testing! (it doesn't have the namespace of ggplot2 for finding ggplot.data.frame etc.)
358 # ggplot <- function (data = NULL, ...) {
359 # UseMethod("ggplot")
360 # }
361
362
363 #
364 #
365 # data(iris) # load data
366 # dend <- iris[1:30,-5] %>% dist %>% hclust %>% as.dendrogram %>%
367 # set("branches_k_color",k=3) %>% set("branches_lwd", c(1,2)) %>% set("branches_lty", c(1,2,1,3))
368 # plot(dend)
369 # data <- as.ggdend(dend)
370 # rm(ggplot)
371 # ggplot.ggdend(data)
372
373
374 # ggplot(data)
375 # ggplot.ggdend(data, horiz = T) +
376 # coord_flip() +
377 # scale_y_reverse(expand = c(0.2, 0)) + coord_polar(theta="x")
378
379
380
381
382
383
384 #' @export
385 #' @rdname ggdend
386 prepare.ggdend <- function(data, ...) {
387 # Fix segements
388 # ===============
389
390 # Fix lty
391 #-------------
392 # Using scale_linetype_identity() fixed all of this!
393 # Dealing with the linetypes:
394 # http://sape.inf.usi.ch/quick-reference/ggplot2/linetype
395 # http://www.cookbook-r.com/Graphs/Shapes_and_line_types/
396 # linetypes <- c("blank", "solid", "dashed", "dotted", "dotdash", "longdash", "twodash") # , "1F", "F1", "4C88C488", "12345678")
397 # # lty must be either numeric or character
398 # if(is.numeric(data$segments$lty)) {
399 # data$segments$lty[is.na(data$segments$lty)] <- 1
400 # data$segments$lty <- factor(data$segments$lty, levels = 0:6)
401 # levels(data$segments$lty) <- linetypes
402 # data$segments$lty <- as.character(data$segments$lty)
403 #
404 # } else { # character
405 # # filling missing values (what ever is missing is solid)
406 # data$segments$lty[is.na(data$segments$lty)] <- "solid"
407 # }
408 # # if lty is NOT numeric or character == a problem...
409 # if(!is.numeric(data$segments$lty) & !is.character(data$segments$lty)) {
410 # warning("lty in segments should be either a number or a character. falls back to solid.")
411 # data$segments$lty <- "solid"
412 # }
413
414 if (is.numeric(data$segments$lty)) {
415 data$segments$lty[is.na(data$segments$lty)] <- 1
416 } else { # character
417 # filling missing values (what ever is missing is solid)
418 data$segments$lty[is.na(data$segments$lty)] <- "solid"
419 }
420
421
422
423 # Fix lwd
424 #-------------
425 # filling missing values
426 data$segments$lwd[is.na(data$segments$lwd)] <- 1
427
428 # Fix col
429 # -------------
430 # filling missing values
431 if (is.numeric(data$segments$col)) {
432 data$segments$col[is.na(data$segments$col)] <- 1
433 } else { # character
434 # filling missing values (what ever is missing is solid)
435 data$segments$col[is.na(data$segments$col)] <- "black" # "#000000"
436 # data$segments$col <- "#000000"
437 }
438
439
440 # Fix labels
441 # ===============
442
443 # Fix col
444 # -------------
445 # filling missing values
446 if (is.numeric(data$labels$col)) {
447 data$labels$col[is.na(data$labels$col)] <- 1
448 } else { # character
449 data$labels$col[is.na(data$labels$col)] <- "black" # "#000000"
450 }
451
452 # Fix cex
453 # -------------
454 # filling missing values
455 if (any(is.na(data$labels$cex))) data$labels$cex[is.na(data$labels$cex)] <- 1
456
457
458 # Fix the "nodes" table
459 # -------------
460 # We fix a row only if we have some value for pch/cex/col in that row.
461 ss_has_value <- !apply(data$nodes[, c("pch", "cex", "col")], 1, allNA)
462 # filling missing values
463 data$nodes$pch <- ifelse(ss_has_value & is.na(data$nodes$pch), 1, data$nodes$pch)
464 data$nodes$cex <- ifelse(ss_has_value & is.na(data$nodes$cex), 3.5, data$nodes$cex)
465 data$nodes$col <- ifelse(ss_has_value & is.na(data$nodes$col), 1, data$nodes$col)
466
467
468 data
469 }
470
471
472
473 # ' @ importFrom ggplot2 ggplot geom_segment geom_point aes guides scale_colour_identity scale_size_identity scale_linetype_identity scale_shape_identity geom_text coord_flip scale_y_reverse aes_string
474 # NULL
475
476 #' @import ggplot2
477
478
479 # ggplot2:::ggplot.data.frame
480 # based on ggdendrogram! from the ggdendro package
481 # polar cor is a problem with text: https://stackoverflow.com/questions/8468472/adjusting-position-of-text-labels-in-coord-polar-histogram-in-ggplot2
482
483 #' @export
484 #' @rdname ggdend
485 ggplot.ggdend <- function(data = NULL, mapping = aes(), ..., segments = TRUE, labels = TRUE, nodes = TRUE,
486 horiz = FALSE, theme = theme_dendro(),
487 offset_labels = 0, na.rm = TRUE,
488 environment = parent.frame()) {
489 # library(dendextend)
490 # library(ggdendro)
491 # Get all the ggplot2 functions ready: (this could have been evoided if ggplot2 was imported...)
492 # This saves us from the error: "ggplot.ggdend: no visible global function definition for ..."
493 # library(ggplot2)
494 # ggplot <- ggplot2::ggplot
495 # geom_segment <- ggplot2::geom_segment
496 # geom_point <- ggplot2::geom_point
497 # aes <- ggplot2::aes
498 # guides <- ggplot2::guides
499 # scale_colour_identity <- ggplot2::scale_colour_identity
500 # scale_size_identity <- ggplot2::scale_size_identity
501 # scale_linetype_identity <- ggplot2::scale_linetype_identity
502 # scale_shape_identity <- ggplot2::scale_shape_identity
503 # geom_text <- ggplot2::geom_text
504 # coord_flip <- ggplot2::coord_flip
505 # scale_y_reverse <- ggplot2::scale_y_reverse
506 # aes_string <- ggplot2::aes_string
507
508 # By using "x" instead of x - we avoid the error:
509 # ggplot.ggdend: no visible binding for global variable 'x'
510
511
512
513 data <- prepare.ggdend(data)
514
515
516 # turning off legends.
517 # https://stackoverflow.com/questions/14604435/turning-off-some-legends-in-a-ggplot
518
519 angle <- ifelse(horiz, 0, 90)
520 hjust <- ifelse(horiz, 0, 1)
521
522
523
524 p <- ggplot(mapping = mapping, ...)
525
526
527 # ggplot() +
528 # geom_segment(data = data$segments,
529 # aes(x = x, y = y, xend = xend, yend = yend, colour = col, linetype = lty, size = lwd)) +
530 # guides(linetype = "none", col = "none") +
531 # scale_colour_identity() + scale_size_identity() + scale_linetype_identity() +
532 # geom_text(data = data$labels,
533 # aes(x = x, y = y, label = label), angle = angle, hjust = 1) # ,angle = 90
534
535
536 if (segments) {
537 p <- p + geom_segment(
538 data = data$segments, na.rm = na.rm,
539 aes(
540 x = .data$x, y = .data$y, xend = .data$xend, yend = .data$yend,
541 colour = .data$col, linetype = .data$lty, size = .data$lwd
542 ),
543 lineend = "square"
544 ) +
545 guides(linetype = "none", col = "none") +
546 scale_colour_identity() + scale_size_identity() + scale_linetype_identity()
547 }
548
549 if (nodes) {
550 p <- p + geom_point(
551 data = data$nodes, na.rm = na.rm,
552 aes(x = .data$x, y = .data$y, colour = .data$col, shape = .data$pch, size = .data$cex)
553 ) +
554 guides(shape = "none", col = "none", size = "none") +
555 # scale_colour_identity() + scale_size_identity() +
556 scale_shape_identity()
557 }
558 # http://docs.ggplot2.org/0.9.3.1/geom_point.html
559
560 # p + geom_point(data = data$nodes,
561 # aes_string(x = "x", y = "y", colour = "col", shape = "pch", size = 3.5)) +
562 # guides(shape = "none", col = "none", size = "none") +
563 # scale_shape_identity()
564
565
566 if (labels) {
567 # default size is 5! http://sape.inf.usi.ch/quick-reference/ggplot2/geom_text
568 data$labels$cex <- 5 * data$labels$cex
569 data$labels$y <- data$labels$y + offset_labels
570 p <- p + geom_text(
571 data = data$labels,
572 aes(
573 x = .data$x,
574 y = .data$y,
575 label = .data$label,
576 colour = .data$col,
577 size = .data$cex
578 ),
579 hjust = hjust,
580 angle = angle
581 )
582 }
583 # p <- p + scale_x_discrete(labels = data$labels$label)
584 # if (horiz) {
585 # p <- p + scale_x_discrete(labels = data$labels$label)
586 # }
587 # else {
588 # p <- p + scale_x_discrete(labels = data$labels$label)
589 # }
590 if (horiz) {
591 p <- p + coord_flip() + scale_y_reverse(expand = c(0.2, 0))
592 }
593 # p <- p + scale_y_reverse(expand = c(0.2, 0)) # scale_y_continuous() +
594
595 # if (theme_dendro)
596 # p <- p + theme_dendro()
597 # p <- p + theme(axis.text.x = element_text(angle = angle,
598 # hjust = 1))
599 # p <- p + theme(axis.text.y = element_text(angle = angle,
600 # hjust = 1))
601
602 if (!is.null(theme)) {
603 p <- p + theme
604 }
605
606 p
607
608
609 # + scale_colour_brewer()
610 # + scale_color_manual(values = unique(data$segments$col))
611
612
613 # scale_linetype_manual(guide=FALSE) # values=linetypes) #,
614 #
615 # + # Change linetypes
616 # scale_color_manual(, guide=FALSE)
617 #
618 #
619 # p <- ggplot() +
620 # geom_segment(data = data$segments,
621 # aes(x = x, y = y, xend = xend, yend = yend, colour = col, linetype = "2", size =2)) +
622 # coord_flip() +
623 # scale_y_reverse(expand = c(0.2, 0)) +
624 # geom_text(data = data$labels,
625 # aes(x = x, y = y, label = label))
626 # p
627 # + theme_dendro() + coord_polar(theta="x")
628 }
629
630
631 #' @export
632 #' @rdname ggdend
633 ggplot.dendrogram <- function(data, ...) {
634 # library(ggplot2) # enough to use ggplot2::
635 ggplot2::ggplot(as.ggdend(data), ...)
636 }
637
638
639 #' @export
640 #' @rdname ggdend
641 print.ggdend <- function(x, ...) {
642 # library(ggplot2) # enough to use ggplot2::
643 print(ggplot2::ggplot(x, ...))
644 }
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670 # ===
671 ## JUNK
672
673 #
674 # labels_colors(dend2)
675 # plot(dend2)
676 # unclass(dend2)
677 # labels_colors(dend2) <- 2
678 # get_leaves_attr(dend2, "nodePar")
679 # get_leaves_attr(dend, "nodePar")
680 #
681 #
682 # x=get_nodes_attr(dend, "edgePar", simplify = FALSE)
683 # x=get_nodes_attr(set(dend, "clear_branches"), "edgePar", simplify = FALSE)
684 # "col" %in% sapply(x, names)
685 #
686 # nodes_xy$fsaaf
687 # x=get_nodes_attr(dend, "edgePar", simplify = FALSE)
688 # unlist(x)
689 # head(x,3)
690 # sapply(x, names)
691 # sapply(get_nodes_attr(dend2, "edgePar", simplify = FALSE), names)
692 # do.call(rbind, )
693
694 # ggplot.ggdend(data)
695 # ggplot.ggdend(data, horiz = T)