除了菲利克斯的解决方案,我想发布我自己的答案:
library(magrittr)
library(grid)
library(gridExtra)
library(dendextend)
cols <- c("#009000", "#FF033E", "#CB410B", "#3B444B", "#007FFF")
dend <- iris[1:40,-5] %>% dist %>% hclust %>% as.dendrogram
dend <- color_branches(dend, k = 5, col = cols)
dend %<>% set("labels_col", value = cols, k= 5)
dend %<>% set("labels_cex", .8)
dend %<>% set("branches_lwd", 2)
clust <- cutree(dend, k = 5)
colors <- labels_colors(dend)[clust %>% sort %>% names]
clust_labs <- colors %>% unique
circlize_dendrogram(dend)
grid.circle(x = .95, y = .9, r = .02, gp = gpar(fill = clust_labs[1]))
grid.circle(x = .95, y = .85, r = .02, gp = gpar(fill = clust_labs[2]))
grid.circle(x = .95, y = .8, r = .02, gp = gpar(fill = clust_labs[3]))
grid.circle(x = .95, y = .75, r = .02, gp = gpar(fill = clust_labs[4]))
grid.circle(x = .95, y = .7, r = .02, gp = gpar(fill = clust_labs[5]))
grid.text(x = .95, y = .9, label = expression(bold(1)), gp = gpar(fontsize = 9, col = "white"))
grid.text(x = .95, y = .85, label = expression(bold(2)), gp = gpar(fontsize = 9, col = "white"))
grid.text(x = .95, y = .8, label = expression(bold(3)), gp = gpar(fontsize = 9, col = "white"))
grid.text(x = .95, y = .75, label = expression(bold(4)), gp = gpar(fontsize = 9, col = "white"))
grid.text(x = .95, y = .7, label = expression(bold(5)), gp = gpar(fontsize = 9, col = "white"))
grid.text(x = .91, y = .8, label = "CLUSTERS", rot = 90, gp = gpar(fontsize = 9))