2016-01-25 88 views
0

我想繪製K均值聚類。以下是我使用的代碼。如何在K中顯示行名稱意味着R中的聚類圖?

library(cluster) 
library(fpc) 

data(iris) 
dat <- iris[, -5] # without known classification 
# Kmeans clustre analysis 
clus <- kmeans(dat, centers=3) 
clusplot(dat, clus$cluster, color=TRUE, shade=TRUE, 
     labels=2, lines=0) 

我得到下面的圖片:

enter image description here

取而代之的行號,我想這與字符的行名稱顯示。我明白這幅畫是生產有一個像下面的數據:

Sepal.Length Sepal.Width Petal.Length Petal.Width Species 
1   5.1   3.5   1.4   0.2 setosa 
2   4.9   3.0   1.4   0.2 setosa 
3   4.7   3.2   1.3   0.2 setosa 
4   4.6   3.1   1.5   0.2 setosa 
5   5.0   3.6   1.4   0.2 setosa 
6   5.4   3.9   1.7   0.4 setosa 

但是好心假設我的工作數據就像下面

  Sepal.Length Sepal.Width Petal.Length Petal.Width Species 
Flower1   5.1   3.5   1.4   0.2 setosa 
Flower2   4.9   3.0   1.4   0.2 setosa 
Flower3   4.7   3.2   1.3   0.2 setosa 
Flower4   4.6   3.1   1.5   0.2 setosa 
Flower5   5.0   3.6   1.4   0.2 setosa 
Flower6   5.4   3.9   1.7   0.4 setosa 

我想有flower1,flower2等在可視化而不是序列號。有沒有辦法做到這一點?

謝謝。

回答

2

對我來說,它的工作原理是,當您以像row.names(dat) <- paste("flower",row.names(dat))這樣的方式重命名行名稱時。這意味着如果您的行名稱符合您的要求,它們將被正確顯示。

希望能幫上忙!

+0

感謝您的解決方案。這工作。 – Arun