@@ -14,8 +14,8 @@ runtime: shiny_prerendered
14
14
``` {r setup, include=FALSE}
15
15
BioDataScience2::learnr_setup()
16
16
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)
19
19
20
20
SciViews::R()
21
21
@@ -121,7 +121,7 @@ as.dissimilarity.matrix <- function(x, ...) {
121
121
122
122
# We want to print only the first few rows and columns
123
123
print.dissimilarity <- function(x, digits.d = 3L, rownames.lab = "labels",
124
- ...) {
124
+ ...) {
125
125
mat <- as.matrix(x)
126
126
mat <- format(round(mat, digits.d))
127
127
diag(mat) <- ""
@@ -161,14 +161,14 @@ nobs.dissimilarity <- function(object, ...)
161
161
# TODO: `[` by first transforming into a matrix with as.matrix()
162
162
163
163
autoplot.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
+ ...) {
166
166
factoextra::fviz_dist(object, order = order, show_labels = show.labels,
167
167
lab_size = lab.size, gradient = gradient)
168
168
}
169
169
170
170
chart.dissimilarity <- function(data, ...,
171
- type = NULL, env = parent.frame())
171
+ type = NULL, env = parent.frame())
172
172
autoplot(data, type = type, ...)
173
173
174
174
# cluster object (inheriting from hclust)
@@ -261,23 +261,24 @@ augment.cluster <- function(x, data, k = NULL, h = NULL, ...) {
261
261
# circular), see http://www.sthda.com/english/wiki
262
262
# /beautiful-dendrogram-visualizations-in-r-5-must-known-methods
263
263
# -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", ...) {
266
266
type <- match.arg(type[1], c("vertical", "horizontal", "circular"))
267
267
# type == "circular" is special because we need to transform as ape::phylo
268
268
if (type == "circular") {
269
269
if (!missing(hang))
270
270
warning("'hang' is not used with a circular dendrogram")
271
271
phylo <- ape::as.phylo(x)
272
- plot(phylo, type = "fan", font = 1, ...)
272
+ plot(phylo, type = "fan", font = 1, show.tip.label = labels, ...)
273
273
} else {# Use plot.dendrogram() instead
274
274
# We first convert into dendrogram objet, then we plot it
275
275
# (better that plot.hclust())
276
+ if (isTRUE(labels)) leaflab <- "perpendicular" else leaflab <- "none"
276
277
dendro <- as.dendrogram(x, hang = hang, check = check)
277
278
if (type == "horizontal") {
278
- plot(dendro, horiz = TRUE, xlab = lab, ...)
279
+ plot(dendro, horiz = TRUE, leaflab = leaflab, xlab = lab, ...)
279
280
} else {
280
- plot(dendro, horiz = FALSE, ylab = lab, ...) # note: label different axe
281
+ plot(dendro, horiz = FALSE, leaflab = leaflab, ylab = lab, ...)
281
282
}
282
283
}
283
284
}
@@ -289,8 +290,8 @@ circle <- function(x = 0, y = 0, d = 1, col = 0, lwd = 1, lty = 1, ...)
289
290
inches = FALSE, add = TRUE, ...)
290
291
291
292
# 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", ...) {
294
295
if (is.null(type))
295
296
type <- "vertical"
296
297
type <- match.arg(type[1], c("vertical", "horizontal", "circular"))
@@ -302,24 +303,29 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
302
303
theme + xlab(xlab) + ylab(ylab)
303
304
304
305
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
+ }
314
317
315
318
# Make the dendrogram circular
316
319
dendro <- dendro +
317
320
scale_x_reverse() +
318
321
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 +
323
329
theme(panel.border = element_blank(),
324
330
axis.text = element_blank(),
325
331
axis.line = element_blank(),
@@ -336,6 +342,9 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
336
342
axis.line.x = element_blank(),
337
343
axis.ticks.x = element_blank(),
338
344
axis.text.y = element_text(angle = 90, hjust = 0.5))
345
+ if (!isTRUE(labels))
346
+ dendro <- dendro +
347
+ theme(axis.text.x = element_blank())
339
348
340
349
} else {# Horizontal dendrogram
341
350
dendro <- dendro +
@@ -346,6 +355,9 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
346
355
theme(panel.border = element_blank(),
347
356
axis.line.y = element_blank(),
348
357
axis.ticks.y = element_blank())
358
+ if (!isTRUE(labels))
359
+ dendro <- dendro +
360
+ theme(axis.text.y = element_blank())
349
361
}
350
362
dendro
351
363
}
0 commit comments