Reputation: 100
I have a 300x2 matrix of data, i.e. 300 observations of 2 variables. Using the kmeans function in R, I can plot the resulting clusters in the following way:
data <- scale(data)
fit.kmeans <- kmeans(data, 3)
plot(data, col = fit.kmeans$cluster)
This gives a nice 2D plot of the original data, colored by cluster. Is there any simple way of doing the same using the hclust function? Or, alternatively, is there another function which lets me implement different clustering methods and subsequently plot the resulting clusters? Thanks in advance.
Upvotes: 2
Views: 8943
Reputation: 3053
A cluster dendrogram is a nice alternative to a scatter plot for hierarchical clustering:
tree <- hclust(d = dist(x = iris[1:50, 1:4], method = "euclidean"))
cl_members <- cutree(tree = tree, k = 3)
plot(x = tree, labels = row.names(tree), cex = 0.5)
rect.hclust(tree = tree, k = 3, which = 1:3, border = 1:3, cluster = cl_members)
Upvotes: 6
Reputation: 37641
Each clustering method reports the clusters in slightly different ways. In general, you will need to look at the structure returned by the clustering function.
But you ask specifically about hclust
. To get the clusters from hclust
you need to use the cutree
function together with the number of clusters you want. Here is an example of using it with the iris data.
HC = hclust(dist(iris[,1:4]))
plot(iris[,3:4], pch=20, col=cutree(HC,3))
Upvotes: 2