3
$\begingroup$

I am working through the following tutorial about how to create clusters within a dataset. The tutorial can be found here.

Using the following code, we can create a plot where we visualise the three clusters which have been formed.

library(factoextra)
library(cluster)
library(NbClust)
#
data(iris)
head(iris)
#
iris.scaled <- scale(iris[, -5])
head(iris.scaled)
#
# K-means clustering
set.seed(123)
km.res <- kmeans(iris.scaled, 3, nstart = 25)


#
# lets inspect the output here
kmeans(iris.scaled, 3, nstart = 25)
#
#


# k-means group number of each observation
km.res$cluster
#
# Visualize k-means clusters
fviz_cluster(km.res, data = iris.scaled, geom = "point",
             stand = FALSE, frame.type = "norm") + theme_bw()

While, it looks fantastic that we have found three clusters within the dataset, I cannot understand what we have plotted on our x and y axis, what is the Dim1 and Dim2 and what does the 73 and 22.9% have to do with anything.

We plot the output from the kmeans command, where I assume it calls upon the Cluster means:??

$\endgroup$

2 Answers 2

9
$\begingroup$

It looks like the choose.vars argument is missing in your fviz_cluster() function. Try something like this:

iris.scaled <- scale(x = iris[, -5])

set.seed(123)
km.res <- kmeans(x = iris.scaled, centers = 3, nstart = 25)

fviz_cluster(object = km.res, data = iris.scaled, geom = "point",
choose.vars = c("Sepal.Length", "Sepal.Width"), stand = FALSE, 
ellipse.type = "norm") + theme_bw()

I also changed the frame.type argument since it is deprecated to ellipse.type.

Equivalent base R plot:

plot(x = iris$Sepal.Length, y = iris$Sepal.Width, col = km.res$cluster)

Update The author of the factoextra package, Alboukadel Kassambara, informed me that if you omit the choose.vars argument, the function fviz_cluster transforms the initial set of variables into a new set of variables through principal component analysis (PCA). This dimensionality reduction algorithm operates on the four variables and outputs two new variables (Dim1 and Dim2) that represent the original variables, a projection or "shadow" of the original data set. Each dimension represent a certain amount of the variation (i.e. information) contained in the original data set. In this example, Dim1 and Dim2 represent 73% and 22.9% respectively. When plotted, this lower-dimensional picture can be difficult to interpret. In exploratory data analysis, it is therefore perhaps more useful to purposefully select two variables at a time through the choose.vars argument, and then compare the plots.

$\endgroup$
1
  • $\begingroup$ thank you for the info, this is really useful. But how do you determine the two variables to choose in the choose.vars option?I suppose it's based on the results from the clustering. $\endgroup$ Commented Feb 18, 2021 at 11:02
3
$\begingroup$

I wish that I could just point you to the documentation and say you should have known, but the documentation on fviz_cluster does not provide much help on your questions.

What is the Dim1 and Dim2 and what does the 73 and 22.9% have to do with anything.

The iris data that you clustered has four dimensions. To get a nice plot, you need to get it down to two dimensions somehow. The graph produced by fviz_cluster is not some selection of two of the dimensions from the original four. Instead, they have done a Principle Components Analysis and projected the data onto the first two principle components. Those should be the two dimensions that show the most variation in the data. The 73% means that the first principle component accounts for 73% of the variation. The second principle component accounts for 22.9% of the variation. So together they account for 95.9% of the variation.

The cluster means are plotted. All of the points in cluster one are plotted with a red circle, but there is one red circle larger than the others. That is the cluster mean. Similarly, the large green triangle and the large blue box are the cluster means. The ellipses are fit to the points in the three clusters.

$\endgroup$
1
  • $\begingroup$ thank you for your answer, is there a way to make Dim1 and Dim2 more understandable to an outside reader? Is it possible to know which of the 4 variables contribute to the 2 dimensions? $\endgroup$ Commented Feb 18, 2021 at 11:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.