How to display the row name in K means cluster plot in R?

旧时模样 提交于 2019-12-04 05:16:03

问题


I am trying to plot the K-means cluster. The below is the code i use.

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)

I get the below picture:

Instead of the row numbers, I want it displayed with a row name in characters. I understand this picture is producing had the data like the below:

  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

However kindly assume that the my work data is like below

         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

I want to have the flower1, flower2 etc in the visualization rather than the serial number. Is there a way to achieve this please?

Thank you.


回答1:


for me it worked, when you rename the row names in a way like row.names(dat) <- paste("flower",row.names(dat)). This means if your row names are in the way you want them to be, they'll be properly displayed.

Hope that'll help!



来源:https://stackoverflow.com/questions/34986337/how-to-display-the-row-name-in-k-means-cluster-plot-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!