2014-01-08 17 views
0

我有數據矩陣流動:如何設置顏色爲情節層次聚類

> 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() 

回答

1

猿包有一些很好的樹繪圖功能。試試這樣的:

library(ape) 
ph <- as.phylo(hc.euclid.single)  
plot(ph, main="HClust, Euclidean distance, Single Linkage", tip.color=data.df$Color, 
    direction = "downwards"))