Debian Codegraph @

R/ggdendrogram.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 #' Creates dendrogram plot using ggplot.
21 #'
22 #' This is a convenience function
23 #'
24 #' @param data Either a dendro object or an object that can be coerced to class
25 #' dendro using the [dendro_data()] function, i.e. objects of class
26 #' dendrogram, hclust or tree
27 #'
28 #' @param segments If TRUE, show line segments
29 #'
30 #' @param labels if TRUE, shows segment labels
31 #'
32 #' @param leaf_labels if TRUE, shows leaf labels
33 #'
34 #' @param rotate if TRUE, rotates plot by 90 degrees
35 #'
36 #' @param theme_dendro if TRUE, applies a blank theme to plot (see
37 #' [theme_dendro()])
38 #'
39 #' @param ... other parameters passed to [ggplot2::geom_text()]
40 #'
41 #' @export
42 #' @return A [ggplot2::ggplot()] object
43 #' @seealso [dendro_data()]
44 #' @example inst/examples/example_ggdendrogram.R
45 ggdendrogram <- function(data,
46 segments = TRUE, labels = TRUE, leaf_labels = TRUE,
47 rotate = FALSE, theme_dendro = TRUE, ...) {
48 dataClass <- if (inherits(data, "dendro")) data$class else class(data)
49 angle <- if (dataClass %in% c("dendrogram", "hclust")) {
50 ifelse(rotate, 0, 90)
51 } else {
52 ifelse(rotate, 90, 0)
53 }
54 hjust <- if (dataClass %in% c("dendrogram", "hclust")) {
55 ifelse(rotate, 1, 1)
56 } else {
57 0.5
58 }
59 if (!is.dendro(data)) data <- dendro_data(data)
60 p <- ggplot() +
61 geom_blank()
62 if (segments && !is.null(data$segments)) {
63 p <- p + geom_segment(
64 data = segment(data),
65 aes(x = .data[["x"]], y = .data[["y"]], xend = .data[["xend"]], yend = .data[["yend"]])
66 )
67 }
68 if (leaf_labels && !is.null(data$leaf_labels)) {
69 p <- p + geom_text(
70 data = leaf_label(data),
71 aes(x = .data[["x"]], y = .data[["y"]], label = .data[["label"]]), hjust = hjust, angle = angle, ...
72 )
73 }
74 if (labels) {
75 p <- p + scale_x_continuous(
76 breaks = seq_along(data$labels$label),
77 labels = data$labels$label
78 )
79 }
80 if (rotate) {
81 p <- p + coord_flip()
82 p <- p + scale_y_continuous()
83 } else {
84 p <- p + scale_y_continuous()
85 }
86 if (theme_dendro) p <- p + theme_dendro()
87 p <- p +
88 theme(axis.text.x = element_text(angle = angle, hjust = 1, vjust = 0.5)) +
89 theme(axis.text.y = element_text(angle = angle, hjust = 1))
90
91 p
92 }
93
94
95 #' Creates completely blank theme in ggplot.
96 #'
97 #' Sets most of the `ggplot` options to blank, by returning blank `theme`
98 #' elements for the panel grid, panel background, axis title, axis text, axis
99 #' line and axis ticks.
100 #' @export
101 theme_dendro <- function() {
102 element_blank <- ggplot2::element_blank
103 ggplot2::theme(
104 panel.grid.major = element_blank(),
105 panel.grid.minor = element_blank(),
106 panel.background = element_blank(),
107 axis.title.x = element_text(colour = NA),
108 axis.title.y = element_blank(),
109 axis.text.x = element_blank(),
110 axis.text.y = element_blank(),
111 axis.line = element_blank(),
112 axis.ticks = element_blank()
113 )
114 }