| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#' @title Plot a circlized dendrograms |
| 24 |
#' @export |
| 25 |
#' @author Zuguang Gu, Tal Galili |
| 26 |
#' @description |
| 27 |
#' Plot a circlized dendrograms using the circlize package (must be installed for the function to work). |
| 28 |
#' |
| 29 |
#' This type of plot is also sometimes called fan tree plot (although the name fan-plot is also used for a different |
| 30 |
#' plot in time series analysis), radial tree plot, polar tree plot, circular tree plot, and probably other names as well. |
| 31 |
#' |
| 32 |
#' An advantage for using the circlize package directly is for plotting a |
| 33 |
#' circular dendrogram so that you can add more graphics for the elements |
| 34 |
#' in the tree just by adding more tracks using \link[circlize]{circos.track}. |
| 35 |
#' |
| 36 |
#' @param dend a \link{dendrogram} object |
| 37 |
#' @param facing Is the dendromgrams facing inside to the circle or outside. |
| 38 |
#' @param labels logical (TRUE) - should the labels be plotted as well. |
| 39 |
#' @param labels_track_height a value for adjusting the room for the labels. |
| 40 |
#' It is 0.2 by default, but if NULL or NA, it will adjust automatically based on |
| 41 |
#' the max width of the labels. However, if this is too long, the plot will give an error: |
| 42 |
#' Error in check.track.position(track.index, track.start, track.height) : |
| 43 |
#' not enough space for cells at track index '2'. |
| 44 |
#' @param dend_track_height a value for adjusting the room for the dendrogram. |
| 45 |
#' |
| 46 |
#' @param ... Ignored. |
| 47 |
#' |
| 48 |
#' @seealso |
| 49 |
#' \link[circlize]{circos.dendrogram} |
| 50 |
#' |
| 51 |
#' @source |
| 52 |
#' This code is based on the work of Zuguang Gu. If you use the function, please cite both |
| 53 |
#' dendextend (see: \code{citation("dendextend")}), as well as the circlize package (see: \code{citation("circlize")}). |
| 54 |
#' |
| 55 |
#' @return |
| 56 |
#' The dend that was used for plotting. |
| 57 |
#' |
| 58 |
#' @examples |
| 59 |
#' |
| 60 |
#' \dontrun{ |
| 61 |
#' |
| 62 |
#' dend <- iris[1:40, -5] %>% |
| 63 |
#' dist() %>% |
| 64 |
#' hclust() %>% |
| 65 |
#' as.dendrogram() %>% |
| 66 |
#' set("branches_k_color", k = 3) %>% |
| 67 |
#' set("branches_lwd", c(5, 2, 1.5)) %>% |
| 68 |
#' set("branches_lty", c(1, 1, 3, 1, 1, 2)) %>% |
| 69 |
#' set("labels_colors") %>% |
| 70 |
#' set("labels_cex", c(.9, 1.2)) %>% |
| 71 |
#' set("nodes_pch", 19) %>% |
| 72 |
#' set("nodes_col", c("orange", "black", "plum", NA)) |
| 73 |
#' |
| 74 |
#' circlize_dendrogram(dend) |
| 75 |
#' circlize_dendrogram(dend, labels = FALSE) |
| 76 |
#' circlize_dendrogram(dend, facing = "inside", labels = FALSE) |
| 77 |
#' |
| 78 |
#' |
| 79 |
#' # In the following we get the dendrogram but can also get extra information on top of it |
| 80 |
#' circos.initialize("foo", xlim = c(0, 40)) |
| 81 |
#' circos.track(ylim = c(0, 1), panel.fun = function(x, y) { |
| 82 |
#' circos.rect(1:40 - 0.8, rep(0, 40), 1:40 - 0.2, runif(40), col = rand_color(40), border = NA) |
| 83 |
#' }, bg.border = NA) |
| 84 |
#' circos.track(ylim = c(0, 1), panel.fun = function(x, y) { |
| 85 |
#' circos.text(1:40 - 0.5, rep(0, 40), labels(dend), |
| 86 |
#' col = labels_colors(dend), |
| 87 |
#' facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5) |
| 88 |
#' ) |
| 89 |
#' }, bg.border = NA, track.height = 0.1) |
| 90 |
#' max_height <- attr(dend, "height") |
| 91 |
#' circos.track(ylim = c(0, max_height), panel.fun = function(x, y) { |
| 92 |
#' circos.dendrogram(dend, max_height = max_height) |
| 93 |
#' }, track.height = 0.5, bg.border = NA) |
| 94 |
#' circos.clear() |
| 95 |
#' } |
| 96 |
circlize_dendrogram <- function(dend, facing = c("outside", "inside"), labels = TRUE, |
| 97 |
labels_track_height = 0.1, |
| 98 |
dend_track_height = 0.5, |
| 99 |
...) { |
| 100 |
if (!requireNamespace("circlize")) { |
| 101 |
stop("The 'circlize' package is not installed on your system.\nYou first need to run:\n install.packages('circlize') \nbefore you can use the circlize_dendrogram function.") |
| 102 |
} |
| 103 |
|
| 104 |
n_labaels <- nleaves(dend) |
| 105 |
dend_labels_colors <- labels_colors(dend) |
| 106 |
dend_labels_cex <- labels_cex(dend) |
| 107 |
labels_dend <- labels(dend) |
| 108 |
if (as.logical(anyDuplicated(labels_dend))) { |
| 109 |
warning("Not all labels are unique. Therefore, we pad the labels with a running number, so to be able to produce the plot.") |
| 110 |
labels(dend) <- paste0(seq_along(labels_dend), "_", labels_dend) |
| 111 |
labels_dend <- labels(dend) |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
if (is.null(dend_labels_cex)) dend_labels_cex <- rep(1, n_labaels) |
| 117 |
|
| 118 |
circlize::circos.initialize("dendrogram", xlim = c(0, n_labaels)) |
| 119 |
|
| 120 |
if (is.null(labels_track_height) | is.na(labels_track_height)) labels_track_height <- max(strwidth(labels_dend)) |
| 121 |
|
| 122 |
|
| 123 |
# plot labels |
| 124 |
if (labels) { |
| 125 |
circlize::circos.track(ylim = c(0, 1), panel.fun = function(x, y) { |
| 126 |
circlize::circos.text( |
| 127 |
x = 1:n_labaels - 0.5, |
| 128 |
y = rep(0, n_labaels), |
| 129 |
labels = labels_dend, |
| 130 |
cex = dend_labels_cex, |
| 131 |
col = dend_labels_colors, |
| 132 |
facing = "clockwise", niceFacing = TRUE, adj = c(0, 0.5) |
| 133 |
) |
| 134 |
}, bg.border = NA, track.height = labels_track_height) |
| 135 |
} |
| 136 |
|
| 137 |
# plot tree |
| 138 |
max_height <- attr(dend, "height") |
| 139 |
circlize::circos.track(ylim = c(0, max_height), panel.fun = function(x, y) { |
| 140 |
circlize::circos.dendrogram(dend, facing = facing, max_height = max_height) |
| 141 |
}, track.height = dend_track_height, bg.border = NA) |
| 142 |
|
| 143 |
|
| 144 |
circlize::circos.clear() |
| 145 |
|
| 146 |
invisible(dend) |
| 147 |
} |
| 148 |
|
| 149 |
# labels_cex(dend) <- 3 |
| 150 |
# plot(dend) |
| 151 |
# circlize_dendrogram(dend, labels_ylim = c(0,100)) |
| 152 |
# |
| 153 |
|
| 154 |
|
| 155 |
# |
| 156 |
# library(dendextend) |
| 157 |
# |
| 158 |
# circlize_dendrogram(dend) |
| 159 |
# |
| 160 |
# |