我有数据矩阵流动:
> dim(data)
[1] 360 91
它的最后一列是因子(1,2,3,...,15),它代表了类。我想做分层聚类,但我不知道为图中不同类的点设置不同的颜色。我试过这个:
# assigning color code to data
data.df = data.frame(data[,1:90])
Color<-data[,91]
data.df$Color <- as.factor(Color)
data.norm <- as.data.frame(scale(data.df[,1:90]))
# clustering:
dist.euclid <- dist(data.norm, method="euclidean")
hc.euclid.single <- hclust(dist.euclid, method="single")
png("HClust, Euclidean distance, Single Linkage.png")
plot(hc.euclid.single, main="HClust, Euclidean distance, Single Linkage" , col=data.df$Color)
dev.off()
答案 0 :(得分:1)
ape包有一些很好的树绘图功能。尝试这样的事情:
library(ape)
ph <- as.phylo(hc.euclid.single)
plot(ph, main="HClust, Euclidean distance, Single Linkage", tip.color=data.df$Color,
direction = "downwards"))