我正在进行层次聚类,并且我有数据标签的基本事实。 我想添加颜色边栏,以显示群集纯度(此群集的百分比属于A类,B类的样本) 我很乐意得到您的答复。
这就是我的意思: dend< - as.dendrogram(hclust(dist(mtcars)))
# Create a vector giving a color for each car to which company it belongs to
car_type <- rep("Other", length(rownames(mtcars)))
is_x <- grepl("Merc", rownames(mtcars))
car_type[is_x] <- "Mercedes"
is_x <- grepl("Mazda", rownames(mtcars))
car_type[is_x] <- "Mazda"
is_x <- grepl("Toyota", rownames(mtcars))
car_type[is_x] <- "Toyota"
car_type <- factor(car_type)
n_car_types <- length(unique(car_type))
cols_4 <- colorspace::rainbow_hcl(n_car_types, c = 70, l = 50)
col_car_type <- cols_4[car_type]
plot(dend)
# extra: showing the various clusters cuts
k234 <- cutree(dend, k = 2:4)