blue-sky
blue-sky

Reputation: 53796

How to label k-means clusters in r

The wikibook on kmeans clustering (http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Clustering/K-Means) gives an example cluster analysis :

Can the code be amended so that a label is generated from each cluster? Below graph does not indicate what is being compared. There are three clusters but what are the names of each cluster ?

enter image description here

Here is the code that generates the graph :

# import data (assume that all data in "data.txt" is stored as comma separated values)
x <- read.csv("data.txt", header=TRUE, row.names=1)

# run K-Means
km <- kmeans(x, 3, 15)

# print components of km
print(km)

# plot clusters
plot(x, col = km$cluster)
# plot centers
points(km$centers, col = 1:2, pch = 8)

Upvotes: 5

Views: 15102

Answers (1)

ialm
ialm

Reputation: 8717

As I mentioned in the comments, the clusters are already "labelled" by colour, where different colours are associated with cluster membership. To plot the "cluster labels" instead, you can use:

plot(x, type='n')
text(x, labels=km$cluster, col=km$cluster)

This should plot the "cluster name" instead of the points, and also colour the labels by the clusters.

Upvotes: 4

Related Questions