| 1 |
#' @title Extract a list of \emph{k} subdendrograms from a given dendrogram |
| 2 |
#' object |
| 3 |
#' @export |
| 4 |
#' @description |
| 5 |
#' Extracts a list (\link{dendlist}) of subdendrogram structures based on the cutree \code{\link{cutree.dendrogram}} function |
| 6 |
#' from a given dendrogram object. It can be useful in case we're interested in a visual investigation of |
| 7 |
#' specific clustering results. |
| 8 |
#' @param dend a dendrogram object |
| 9 |
#' @param k the number of subdendrograms that should be extracted |
| 10 |
#' @param order_clusters_as_data passed to \link[dendextend]{cutree}, default is FALSE |
| 11 |
#' (while the cutree default is TRUE). The reason is since it's easier to look at the dendrogram plot |
| 12 |
#' and then get subtrees that are in the same order is in the plot/dendrogram object. |
| 13 |
#' This is in contrast to more traditional use of cutree, where it is used with the original order or rows from the data. |
| 14 |
#' @param ... parameters that should be passed to the cutree |
| 15 |
#' \code{\link{cutree.dendrogram}} |
| 16 |
#' @return |
| 17 |
#' A list of \emph{k} subdendrograms, based on the cutree |
| 18 |
#' \code{\link{cutree.dendrogram}} clustering |
| 19 |
#' clusters. |
| 20 |
#' @examples |
| 21 |
#' |
| 22 |
#' # needed packages: |
| 23 |
#' # install.packages(gplots) |
| 24 |
#' # install.packages(viridis) |
| 25 |
#' # install.packages(devtools) |
| 26 |
#' # devtools::install_github('talgalili/dendextend') #' dendextend from github |
| 27 |
#' |
| 28 |
#' # define dendrogram object to play with: |
| 29 |
#' dend <- iris[1:20, -5] %>% |
| 30 |
#' dist() %>% |
| 31 |
#' hclust() %>% |
| 32 |
#' as.dendrogram() %>% |
| 33 |
#' # set("labels_to_character") %>% |
| 34 |
#' color_branches(k = 5) |
| 35 |
#' labels(dend) <- letters[1:20] |
| 36 |
#' plot(dend) |
| 37 |
#' dend_list <- get_subdendrograms(dend, 5) |
| 38 |
#' lapply(dend_list, labels) |
| 39 |
#' # [[1]] |
| 40 |
#' # [1] "a" "b" |
| 41 |
#' # |
| 42 |
#' # [[2]] |
| 43 |
#' # [1] "c" "d" "e" "f" "g" |
| 44 |
#' # |
| 45 |
#' # [[3]] |
| 46 |
#' # [1] "h" "i" |
| 47 |
#' # |
| 48 |
#' # [[4]] |
| 49 |
#' # [1] "j" "k" "l" "m" |
| 50 |
#' # |
| 51 |
#' # [[5]] |
| 52 |
#' # [1] "n" "o" "p" "q" "r" "s" "t" |
| 53 |
#' |
| 54 |
#' # define dendrogram object to play with: |
| 55 |
#' dend <- iris[, -5] %>% |
| 56 |
#' dist() %>% |
| 57 |
#' hclust() %>% |
| 58 |
#' as.dendrogram() %>% |
| 59 |
#' set("labels_to_character") %>% |
| 60 |
#' color_branches(k = 5) |
| 61 |
#' dend_list <- get_subdendrograms(dend, 5) |
| 62 |
#' |
| 63 |
#' # Plotting the result |
| 64 |
#' par(mfrow = c(2, 3)) |
| 65 |
#' plot(dend, main = "Original dendrogram") |
| 66 |
#' sapply(dend_list, plot) |
| 67 |
#' |
| 68 |
#' # plot a heatmap of only one of the sub dendrograms |
| 69 |
#' par(mfrow = c(1, 1)) |
| 70 |
#' library(gplots) |
| 71 |
#' sub_dend <- dend_list[[1]] #' get the sub dendrogram |
| 72 |
#' # make sure of the size of the dend |
| 73 |
#' nleaves(sub_dend) |
| 74 |
#' length(order.dendrogram(sub_dend)) |
| 75 |
#' # get the subset of the data |
| 76 |
#' subset_iris <- as.matrix(iris[order.dendrogram(sub_dend), -5]) |
| 77 |
#' # update the dendrogram's internal order so to not cause an error in heatmap.2 |
| 78 |
#' order.dendrogram(sub_dend) <- as.integer(rank(order.dendrogram(sub_dend))) |
| 79 |
#' heatmap.2(subset_iris, Rowv = sub_dend, trace = "none", col = viridis::viridis(100)) |
| 80 |
get_subdendrograms <- function(dend, k, order_clusters_as_data = FALSE, ...) { |
| 81 |
clusters <- cutree(dend, k, order_clusters_as_data = order_clusters_as_data, ...) |
| 82 |
dend_list <- lapply(unique(clusters), function(cluster.id) { |
| 83 |
find_dendrogram(dend, names(which(clusters == cluster.id))) |
| 84 |
}) |
| 85 |
class(dend_list) <- "dendlist" |
| 86 |
dend_list |
| 87 |
} |
| 88 |
|
| 89 |
#' @title Search for the sub-dendrogram structure composed of selected labels |
| 90 |
#' @export |
| 91 |
#' @description |
| 92 |
#' Given a dendrogram object, the function performs a recursive DFS algorithm to determine |
| 93 |
#' the sub-dendrogram which is composed of (exactly) all 'selected_labels'. |
| 94 |
#' @param dend a dendrogram object |
| 95 |
#' @param selected_labels A character vector with the labels we expect to have |
| 96 |
#' in the sub-dendrogram. This doesn't have to be in the same order as in the dendrogram. |
| 97 |
#' @return |
| 98 |
#' Either a sub-dendrogram composed of only members of selected_labels. |
| 99 |
#' If such a sub-dendrogram doesn't exist, the function returns NULL. |
| 100 |
#' @examples |
| 101 |
#' |
| 102 |
#' \dontrun{ |
| 103 |
#' # define dendrogram object to play with: |
| 104 |
#' dend <- iris[, -5] %>% |
| 105 |
#' dist() %>% |
| 106 |
#' hclust() %>% |
| 107 |
#' as.dendrogram() %>% |
| 108 |
#' set("labels_to_character") %>% |
| 109 |
#' color_branches(k = 5) |
| 110 |
#' first.subdend.only <- names(cutree(dend, 4)[cutree(dend, 4) == 1]) |
| 111 |
#' sub.dend <- find_dendrogram(dend, first.subdend.only) |
| 112 |
#' # Plotting the result |
| 113 |
#' par(mfrow = c(1, 2)) |
| 114 |
#' plot(dend, main = "Original dendrogram") |
| 115 |
#' plot(sub.dend, main = "First subdendrogram") |
| 116 |
#' |
| 117 |
#' dend <- 1:10 %>% |
| 118 |
#' dist() %>% |
| 119 |
#' hclust() %>% |
| 120 |
#' as.dendrogram() %>% |
| 121 |
#' set("labels_to_character") %>% |
| 122 |
#' color_branches(k = 5) |
| 123 |
#' |
| 124 |
#' selected_labels <- as.character(1:4) |
| 125 |
#' sub_dend <- find_dendrogram(dend, selected_labels) |
| 126 |
#' plot(dend, main = "Original dendrogram") |
| 127 |
#' plot(sub_dend, main = "First subdendrogram") |
| 128 |
#' |
| 129 |
#' |
| 130 |
#' } |
| 131 |
#' |
| 132 |
find_dendrogram <- function(dend, selected_labels) { |
| 133 |
# if the dendrogram is exactly the labels in selected_labels - then we found our dend |
| 134 |
if (all(labels(dend) %in% selected_labels) && |
| 135 |
(length(labels(dend)) == length(selected_labels))) { |
| 136 |
return(dend) |
| 137 |
} |
| 138 |
|
| 139 |
# if not, either we can find such a sub dendrogram, or it doesn't exist (return NULL) |
| 140 |
for(i in 1:length(dend)) { |
| 141 |
if(all(selected_labels %in% labels(dend[[i]]))) { |
| 142 |
return(Recall(dend[[i]], selected_labels)) |
| 143 |
} |
| 144 |
} |
| 145 |
# if we couldn't find any sub-dend that includes all the labels we're looking for |
| 146 |
# then we return NULL |
| 147 |
return(NULL) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
squash_dendrogram <- function(dend, squashed_original_height = FALSE, ...) { |
| 155 |
squashed_labels <- paste(labels(dend), collapse = "_") |
| 156 |
squashed_height <- ifelse(squashed_original_height, attr(dend, "height"), 0) |
| 157 |
dend <- min(unlist(dend)) |
| 158 |
attr(dend, "midpoint") <- NULL |
| 159 |
attr(dend, "members") <- 1L |
| 160 |
attr(dend, "leaf") <- TRUE |
| 161 |
attr(dend, "height") <- squashed_height |
| 162 |
attr(dend, "label") <- squashed_labels |
| 163 |
class(dend) <- "dendrogram" |
| 164 |
dend |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
collapse_labels_0 <- function(dend, selected_labels,...) { |
| 169 |
# if the dendrogram is exactly the labels in selected_labels - then we found our dend |
| 170 |
# let's squash it |
| 171 |
if (all(labels(dend) %in% selected_labels) && |
| 172 |
(length(labels(dend)) == length(selected_labels))) { |
| 173 |
return(squash_dendrogram(dend,...)) |
| 174 |
} |
| 175 |
|
| 176 |
# if not, either we can find such a sub dendrogram, or it doesn't exist (return original dend) |
| 177 |
for(i in 1:length(dend)) { |
| 178 |
if(all(selected_labels %in% labels(dend[[i]]))) { |
| 179 |
dend[[i]] <- Recall(dend[[i]], selected_labels,...) |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
# return the dend (with/without squashing) |
| 184 |
return(dend) |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
#' @title Collapse a sub dendrogram of adjacent labels within a dend |
| 189 |
#' @export |
| 190 |
#' @description |
| 191 |
#' Given a dendrogram object, and a set of labels that are in the same sub-dendrogram, |
| 192 |
#' the function performs a recursive DFS algorithm to determine |
| 193 |
#' the sub-dendrogram which is composed of (exactly) all 'selected_labels'. |
| 194 |
#' It then squashes this sub-dendrogram, and returns the original dendrogram with the squashed |
| 195 |
#' dendrogram with it. |
| 196 |
#' @param dend a dendrogram object |
| 197 |
#' @param selected_labels A character vector with the labels we expect to have |
| 198 |
#' in the sub-dendrogram. This doesn't have to be in the same order as in the dendrogram. |
| 199 |
#' @param ... elipsis (passed to squash_dendrogram) |
| 200 |
#' @return |
| 201 |
#' Either the original dend. |
| 202 |
#' Or, if the labels properly are in the dend by each other, a dend with |
| 203 |
#' a squashed sub-dendrogram inside it. |
| 204 |
#' |
| 205 |
#' @examples |
| 206 |
#' library("dendextend") |
| 207 |
#' |
| 208 |
#' set.seed(23235) |
| 209 |
#' ss <- sample(1:150, 5) |
| 210 |
#' |
| 211 |
#' # Getting the dend object |
| 212 |
#' dend25 <- iris[ss, -5] %>% |
| 213 |
#' dist() %>% |
| 214 |
#' hclust() %>% |
| 215 |
#' as.dendrogram() %>% |
| 216 |
#' set("labels", letters[1:5]) |
| 217 |
#' |
| 218 |
#' par(mfrow = c(1,4)) |
| 219 |
#' plot(dend25) |
| 220 |
#' plot(collapse_labels(dend25, c("d", "e"))) |
| 221 |
#' plot(collapse_labels(dend25, c("c", "d", "e"))) |
| 222 |
#' plot(collapse_labels(dend25, c("c", "d", "e"), squashed_original_height=TRUE)) |
| 223 |
collapse_labels <- function(dend, selected_labels,...) { |
| 224 |
dend <- collapse_labels_0(dend, selected_labels,...) |
| 225 |
dend <- midcache.dendrogram(dend) |
| 226 |
return(dend) |
| 227 |
} |
| 228 |
|