Skip to content

Commit ebb9c87

Browse files
committed
labels = TRUE|FALSE added form plot() and chart() histograms
1 parent 073ecf9 commit ebb9c87

File tree

2 files changed

+70
-46
lines changed

2 files changed

+70
-46
lines changed

devel/shiny/B05Sa_cluster/app.R

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ library(chart)
1212
# add news functions ----
1313
## This function move to a package
1414

15-
# CAH for SciViews, version 1.1.1
16-
# Copyright (c) 2021, Philippe Grosjean (phgrsojean@sciviews.org)
15+
# CAH for SciViews, version 1.2.0
16+
# Copyright (c) 2021, Philippe Grosjean (phgrosjean@sciviews.org)
1717

1818
# dist is really a dissimilarity matrix => we use dissimilarity() as in the
1919
# {cluster} package, i.e., class is c("dissimilarity", "dist")
@@ -257,23 +257,24 @@ augment.cluster <- function(x, data, k = NULL, h = NULL, ...) {
257257
# circular), see http://www.sthda.com/english/wiki
258258
# /beautiful-dendrogram-visualizations-in-r-5-must-known-methods
259259
# -unsupervised-machine-learning
260-
plot.cluster <- function(x, y, hang = -1, check = TRUE, type = "vertical",
261-
lab = "Height", ...) {
260+
plot.cluster <- function(x, y, labels = TRUE, hang = -1, check = TRUE,
261+
type = "vertical", lab = "Height", ...) {
262262
type <- match.arg(type[1], c("vertical", "horizontal", "circular"))
263263
# type == "circular" is special because we need to transform as ape::phylo
264264
if (type == "circular") {
265265
if (!missing(hang))
266266
warning("'hang' is not used with a circular dendrogram")
267267
phylo <- ape::as.phylo(x)
268-
plot(phylo, type = "fan", font = 1, ...)
268+
plot(phylo, type = "fan", font = 1, show.tip.label = labels, ...)
269269
} else {# Use plot.dendrogram() instead
270270
# We first convert into dendrogram objet, then we plot it
271271
# (better that plot.hclust())
272+
if (isTRUE(labels)) leaflab <- "perpendicular" else leaflab <- "none"
272273
dendro <- as.dendrogram(x, hang = hang, check = check)
273274
if (type == "horizontal") {
274-
plot(dendro, horiz = TRUE, xlab = lab, ...)
275+
plot(dendro, horiz = TRUE, leaflab = leaflab, xlab = lab, ...)
275276
} else {
276-
plot(dendro, horiz = FALSE, ylab = lab, ...) # note: label different axe
277+
plot(dendro, horiz = FALSE, leaflab = leaflab, ylab = lab, ...)
277278
}
278279
}
279280
}
@@ -285,8 +286,8 @@ circle <- function(x = 0, y = 0, d = 1, col = 0, lwd = 1, lty = 1, ...)
285286
inches = FALSE, add = TRUE, ...)
286287

287288
# TODO: make sure the dendrogram is correct with different ggplot themes
288-
autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
289-
theme = theme_sciviews(), xlab = "", ylab = "Height", ...) {
289+
autoplot.cluster <- function(object, labels = TRUE, type = "vertical",
290+
circ.text.size = 3, theme = theme_sciviews(), xlab = "", ylab = "Height", ...) {
290291
if (is.null(type))
291292
type <- "vertical"
292293
type <- match.arg(type[1], c("vertical", "horizontal", "circular"))
@@ -298,24 +299,29 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
298299
theme + xlab(xlab) + ylab(ylab)
299300

300301
if (type == "circular") {
301-
# Get labels (need one more to avoid last = first!)
302-
label_df <- tibble::tibble(labels = c(labels(object)[object$order], ""))
303-
xmax <- nobs(object) + 1
304-
label_df$id <- 1:xmax
305-
angle <- 360 * (label_df$id - 0.5) / xmax
306-
# Left or right?
307-
label_df$hjust <- ifelse(angle < 270 & angle > 90, 1, 0)
308-
# Angle for more readable text
309-
label_df$angle <- ifelse(angle < 270 & angle > 90, angle + 180, angle)
302+
if (isTRUE(labels)) {
303+
# Get labels (need one more to avoid last = first!)
304+
label_df <- tibble::tibble(labels = c(labels(object)[object$order], ""))
305+
xmax <- nobs(object) + 1
306+
label_df$id <- 1:xmax
307+
angle <- 360 * (label_df$id - 0.5) / xmax
308+
# Left or right?
309+
label_df$hjust <- ifelse(angle < 270 & angle > 90, 1, 0)
310+
# Angle for more readable text
311+
label_df$angle <- ifelse(angle < 270 & angle > 90, angle + 180, angle)
312+
}
310313

311314
# Make the dendrogram circular
312315
dendro <- dendro +
313316
scale_x_reverse() +
314317
scale_y_reverse() +
315-
coord_polar(start = pi/2) +
318+
coord_polar(start = pi/2)
319+
if (isTRUE(labels))
320+
dendro <- dendro +
316321
geom_text(data = label_df,
317322
aes(x = id, y = -0.02, label = labels, hjust = hjust),
318-
size = circ.text.size, angle = label_df$angle, inherit.aes = FALSE) +
323+
size = circ.text.size, angle = label_df$angle, inherit.aes = FALSE)
324+
dendro <- dendro +
319325
theme(panel.border = element_blank(),
320326
axis.text = element_blank(),
321327
axis.line = element_blank(),
@@ -332,6 +338,9 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
332338
axis.line.x = element_blank(),
333339
axis.ticks.x = element_blank(),
334340
axis.text.y = element_text(angle = 90, hjust = 0.5))
341+
if (!isTRUE(labels))
342+
dendro <- dendro +
343+
theme(axis.text.x = element_blank())
335344

336345
} else {# Horizontal dendrogram
337346
dendro <- dendro +
@@ -342,6 +351,9 @@ autoplot.cluster <- function(object, type = "vertical", circ.text.size = 3,
342351
theme(panel.border = element_blank(),
343352
axis.line.y = element_blank(),
344353
axis.ticks.y = element_blank())
354+
if (!isTRUE(labels))
355+
dendro <- dendro +
356+
theme(axis.text.y = element_blank())
345357
}
346358
dendro
347359
}

inst/tutorials/B05La_cah/B05La_cah.Rmd

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ runtime: shiny_prerendered
1414
```{r setup, include=FALSE}
1515
BioDataScience2::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
2020
SciViews::R()
2121
@@ -121,7 +121,7 @@ as.dissimilarity.matrix <- function(x, ...) {
121121
122122
# We want to print only the first few rows and columns
123123
print.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
163163
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+
...) {
166166
factoextra::fviz_dist(object, order = order, show_labels = show.labels,
167167
lab_size = lab.size, gradient = gradient)
168168
}
169169
170170
chart.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

Comments
 (0)