@@ -14,8 +14,8 @@ runtime: shiny_prerendered
1414``` {r setup, include=FALSE}
1515BioDataScience2::learnr_setup()
1616
17- # CAH for SciViews, version 1.1.1
18- # Copyright (c) 2021, Philippe Grosjean (phgrsojean @sciviews.org)
17+ # CAH for SciViews, version 1.2.0
18+ # Copyright (c) 2021, Philippe Grosjean (phgrosjean @sciviews.org)
1919
2020SciViews::R()
2121
@@ -121,7 +121,7 @@ as.dissimilarity.matrix <- function(x, ...) {
121121
122122# We want to print only the first few rows and columns
123123print.dissimilarity <- function(x, digits.d = 3L, rownames.lab = "labels",
124- ...) {
124+ ...) {
125125 mat <- as.matrix(x)
126126 mat <- format(round(mat, digits.d))
127127 diag(mat) <- ""
@@ -161,14 +161,14 @@ nobs.dissimilarity <- function(object, ...)
161161# TODO: `[` by first transforming into a matrix with as.matrix()
162162
163163autoplot.dissimilarity <- function(object, order = TRUE, show.labels = TRUE,
164- lab.size = NULL, gradient = list(low = "red", mid = "white", high = "blue"),
165- ...) {
164+ lab.size = NULL, gradient = list(low = "red", mid = "white", high = "blue"),
165+ ...) {
166166 factoextra::fviz_dist(object, order = order, show_labels = show.labels,
167167 lab_size = lab.size, gradient = gradient)
168168}
169169
170170chart.dissimilarity <- function(data, ...,
171- type = NULL, env = parent.frame())
171+ type = NULL, env = parent.frame())
172172 autoplot(data, type = type, ...)
173173
174174# cluster object (inheriting from hclust)
@@ -261,23 +261,24 @@ augment.cluster <- function(x, data, k = NULL, h = NULL, ...) {
261261# circular), see http://www.sthda.com/english/wiki
262262# /beautiful-dendrogram-visualizations-in-r-5-must-known-methods
263263# -unsupervised-machine-learning
264- plot.cluster <- function(x, y, hang = -1, check = TRUE, type = "vertical" ,
265- lab = "Height", ...) {
264+ plot.cluster <- function(x, y, labels = TRUE, hang = -1, check = TRUE ,
265+ type = "vertical", lab = "Height", ...) {
266266 type <- match.arg(type[1], c("vertical", "horizontal", "circular"))
267267 # type == "circular" is special because we need to transform as ape::phylo
268268 if (type == "circular") {
269269 if (!missing(hang))
270270 warning("'hang' is not used with a circular dendrogram")
271271 phylo <- ape::as.phylo(x)
272- plot(phylo, type = "fan", font = 1, ...)
272+ plot(phylo, type = "fan", font = 1, show.tip.label = labels, ...)
273273 } else {# Use plot.dendrogram() instead
274274 # We first convert into dendrogram objet, then we plot it
275275 # (better that plot.hclust())
276+ if (isTRUE(labels)) leaflab <- "perpendicular" else leaflab <- "none"
276277 dendro <- as.dendrogram(x, hang = hang, check = check)
277278 if (type == "horizontal") {
278- plot(dendro, horiz = TRUE, xlab = lab, ...)
279+ plot(dendro, horiz = TRUE, leaflab = leaflab, xlab = lab, ...)
279280 } else {
280- plot(dendro, horiz = FALSE, ylab = lab, ...) # note: label different axe
281+ plot(dendro, horiz = FALSE, leaflab = leaflab, ylab = lab, ...)
281282 }
282283 }
283284}
@@ -289,8 +290,8 @@ circle <- function(x = 0, y = 0, d = 1, col = 0, lwd = 1, lty = 1, ...)
289290 inches = FALSE, add = TRUE, ...)
290291
291292# TODO: make sure the dendrogram is correct with different ggplot themes
292- autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3 ,
293- theme = theme_sciviews(), xlab = "", ylab = "Height", ...) {
293+ autoplot.cluster <- function(object, labels = TRUE, type = "vertical" ,
294+ circ.text.size = 3, theme = theme_sciviews(), xlab = "", ylab = "Height", ...) {
294295 if (is.null(type))
295296 type <- "vertical"
296297 type <- match.arg(type[1], c("vertical", "horizontal", "circular"))
@@ -302,24 +303,29 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
302303 theme + xlab(xlab) + ylab(ylab)
303304
304305 if (type == "circular") {
305- # Get labels (need one more to avoid last = first!)
306- label_df <- tibble::tibble(labels = c(labels(object)[object$order], ""))
307- xmax <- nobs(object) + 1
308- label_df$id <- 1:xmax
309- angle <- 360 * (label_df$id - 0.5) / xmax
310- # Left or right?
311- label_df$hjust <- ifelse(angle < 270 & angle > 90, 1, 0)
312- # Angle for more readable text
313- label_df$angle <- ifelse(angle < 270 & angle > 90, angle + 180, angle)
306+ if (isTRUE(labels)) {
307+ # Get labels (need one more to avoid last = first!)
308+ label_df <- tibble::tibble(labels = c(labels(object)[object$order], ""))
309+ xmax <- nobs(object) + 1
310+ label_df$id <- 1:xmax
311+ angle <- 360 * (label_df$id - 0.5) / xmax
312+ # Left or right?
313+ label_df$hjust <- ifelse(angle < 270 & angle > 90, 1, 0)
314+ # Angle for more readable text
315+ label_df$angle <- ifelse(angle < 270 & angle > 90, angle + 180, angle)
316+ }
314317
315318 # Make the dendrogram circular
316319 dendro <- dendro +
317320 scale_x_reverse() +
318321 scale_y_reverse() +
319- coord_polar(start = pi/2) +
320- geom_text(data = label_df,
321- aes(x = id, y = -0.02, label = labels, hjust = hjust),
322- size = circ.text.size, angle = label_df$angle, inherit.aes = FALSE) +
322+ coord_polar(start = pi/2)
323+ if (isTRUE(labels))
324+ dendro <- dendro +
325+ geom_text(data = label_df,
326+ aes(x = id, y = -0.02, label = labels, hjust = hjust),
327+ size = circ.text.size, angle = label_df$angle, inherit.aes = FALSE)
328+ dendro <- dendro +
323329 theme(panel.border = element_blank(),
324330 axis.text = element_blank(),
325331 axis.line = element_blank(),
@@ -336,6 +342,9 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
336342 axis.line.x = element_blank(),
337343 axis.ticks.x = element_blank(),
338344 axis.text.y = element_text(angle = 90, hjust = 0.5))
345+ if (!isTRUE(labels))
346+ dendro <- dendro +
347+ theme(axis.text.x = element_blank())
339348
340349 } else {# Horizontal dendrogram
341350 dendro <- dendro +
@@ -346,6 +355,9 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
346355 theme(panel.border = element_blank(),
347356 axis.line.y = element_blank(),
348357 axis.ticks.y = element_blank())
358+ if (!isTRUE(labels))
359+ dendro <- dendro +
360+ theme(axis.text.y = element_blank())
349361 }
350362 dendro
351363}
0 commit comments