# Now I see it! K-means cluster analysis in R

Of course, a picture on a computer monitor is a coloured plot of x and y coordinates or pixels. Still, I was smitten by David Sparks’ posts on is.r(), where he shows how easy it is to read images into R to analyse them. In two posts [1], [2] he replicates functionality of image manipulation programmes like GIMP.

I can’t resist to write about this here as well. David’s first post is about k-means cluster analysis. One of the popular algorithms for k-means is Lloyd’s algorithm. So, on that note I will use a picture of the Lloyd’s of London building to play around with David’s code, despite the fact that the two Lloyd have nothing to do with each other. Lloyd’s provides pictures of its building copyright free on its web site. However, I will use a reduced file size version, which I copied to GitHub.

The jpeg package by Simon Urbanek [3] allows me to load a jpeg-file into R. The R object of the images is an array, which has the structure of three layered matrices, representing the value of the colours red, green and blue for each x and y coordinate. I convert the array into a data frame, as this is an accepted structure by k-means and plot the data.

library(jpeg)
library(RCurl)
## Loading required package: bitops
url <-"https://raw.githubusercontent.com/mages/diesunddas/master/Blog/LloydsBuilding.jpg"
rgbImage <- data.frame(
x=rep(1:dm[2], each=dm[1]),
y=rep(dm[1]:1, dm[2]),

plot(y ~ x, data=rgbImage, main="Lloyd’s building",
col = rgb(rgbImage[c("r.value", "g.value", "b.value")]),
asp = 1, pch = ".")

Running a k-means analysis on the three colour columns in my data frame allows me to reduce the picture to k colours. The output gives me for each x and y coordinate the colour cluster it belongs to. Thus, I plot my picture again, but replace the original colours with the cluster colours.

kColors <- 5
kMeans <- kmeans(rgbImage[, c("r.value", "g.value", "b.value")],
centers = kColors)
clusterColour <- rgb(kMeans$centers[kMeans$cluster, ])

plot(y ~ x, data=rgbImage, main="Lloyd’s building",
col = clusterColour, asp = 1, pch = ".",
axes=FALSE, ylab="",
xlab="k-means cluster analysis of 5 colours")

Running the k-means algorithm not only over the colours but over the whole data frame, including the x and y coordinate will find localised clusters, or so called Voronoi cells. This gives quite an artistic picture.

nRegions <- 2000 # Number of Voronoi cells.
# Be patient, this takes time.
voronoiMeans <- kmeans(rgbImage, centers = nRegions, iter.max = 50)
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 27000000)
voronoiColor <- rgb(voronoiMeans$centers[voronoiMeans$cluster, 3:5])

plot(y ~ x, data=rgbImage, col = voronoiColor,
asp = 1, pch = ".", main="Lloyd’s building",
axes=FALSE, ylab="", xlab="2000 local clusters")

Instead of plotting all x and y coordinates I can also plot only the cluster centres. In the following chart I generated 500 cluster and plotted their centres as little rectangles, which provides an interesting picture in itself.

nRegions <- 500
voronoiMeans <- kmeans(rgbImage, centers = nRegions, iter.max = 50)
## Warning: Quick-TRANSfer stage steps exceeded maximum (= 27000000)
voronoiColor <- voronoiColor <- rgb(voronoiMeans$centers[,3:5]) plot(y ~ x, data=voronoiMeans$centers,
col = voronoiColor,
main="Lloyd’s building", asp = 1,
pch = 15, cex=1.5, axes=FALSE,
ylab="", xlab="500 local clusters")

You can access the R-code of this post also as gist on Github. Many thanks again to David Sparks for his inspirations on is.R().

### Session Info

sessionInfo()
## R version 3.5.1 (2018-07-02)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS  10.14.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.5/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
##
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base
##
## other attached packages:
## [1] RCurl_1.95-4.11 bitops_1.0-6    jpeg_0.1-8
##
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.0      bookdown_0.7    digest_0.6.18   rprojroot_1.3-2
##  [5] backports_1.1.2 magrittr_1.5    evaluate_0.12   blogdown_0.9
##  [9] stringi_1.2.4   rmarkdown_1.10  tools_3.5.1     stringr_1.3.1
## [13] xfun_0.4        yaml_2.2.0      compiler_3.5.1  htmltools_0.3.6
## [17] knitr_1.20