【发布时间】:2023-03-14 09:58:01
【问题描述】:
我想按数据框中定义的特定组为树状图的分支着色。
library(reshape2)
library(factoextra) # clustering visualization
library(dendextend)
#iris dataset
#defining colors
colori = rep(NA, length=length(iris$Species))
colori[which(iris$Species=="setosa")] = "red"
colori[which(iris$Species=="versicolor")] = "blue"
colori[which(iris$Species=="virginica")] = "yellow"
iris_dist <- dist(iris[ ,1:4],)
hc1_iris <- hclust(iris_dist,method = "average")
col_dendro_iris <- color_branches(as.dendrogram(hc1_iris),groupLabels =T, clusters = iris$Species,col=colori)
col_dendro_iris_plot <- plot(col_dendro_iris,main = "Dendrogram of normalized BLS\ncolored by manmade groups",labels = NULL,xlab = NULL)
编辑: 当我这样做时它会起作用
pca_iris <- PCA(iris[ ,1:4])
colori = rep(NA, length=length(iris$Species))
colori[which(iris$Species=="versicolor")] = "red"
colori[which(iris$Species=="virginica")] = "yellow"
colori[which(iris$Species=="setosa")] = "blue"
# species <- iris$Species
iris_gr <- cbind(iris,colori)
#
pca_iris <- fviz_pca_ind(pca_iris,
pointshape = 21,habillage = iris$Species,
geom.ind = c("point"),geom = c("point"),palette = iris$colori,
title="PCA of normalized BLS\ncolored by manmade groups")
pca_iris<- pca_iris + theme(legend.position = "upper.right")
只为未来的读者。
但实际上我无法以模拟方式为树状图着色。
我没有用于定义集群的k 或h 元素。就像在 iris 中一样,我已经预定义了要着色的簇。
【问题讨论】:
-
就像我编辑的那样,我没有用于定义集群的
k或h元素,而是预定义的组。在鸢尾花的例子中,它是物种 -
对于预定义的组,我建议使用 color_labels 为带有此信息的文本着色。另请查看:talgalili.github.io/dendextend/reference/colored_bars.html
标签: r dendrogram graph-coloring factoextra