Cluster results visualisation in R with fviz_cluster

R comes as a handy tool for a data scientist. One package I found useful while working in R, is factoextra, a package for visualisation and extraction of results from multivariate data analysis including different dimensionality reduction techniques and clustering methods. The official documentation of factoextra is at http://www.sthda.com/english/rpkgs/factoextra/. It can be installed from CRAN using the command ,

install.packages("factoextra")

fviz_cluster function from factoextra is useful in clustering results visualisation. The function usage details are at http://www.sthda.com/english/rpkgs/factoextra/fviz_cluster.html

A quick example showing the clustering of Iris

#Load iris dataset
data("iris")
irisDataScaled<- scale(as.matrix(iris[, 1:4]))
kmeansClusters<- kmeans(irisDataScaled, 3, nstart = 25)

#Visualisation of resuls using fviz_cluster from factoextra
library("factoextra")
fviz_cluster(kmeansClusters, irisDataScaled, stand = FALSE, geom = "point")

The resulting figure would be

kmeanswithiris

The output from the fviz_cluster function is a ggplot. If you are to use fviz_cluster within a loop,  the output ggplot should be printed out explicitly as

for (i in 1:iterEnd){
.....
print(fviz_cluster(kmeansClusters, irisDataScaled, stand = FALSE, geom = "point")
......
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s