Introduction

Here I demonstrate how to generate k-means output from stacked raster data in R. For the .Rmd behind this output, right click, copy the link and paste in your browser my gitHub account.

Getting the data sets

I will make use of worldclim data and global administrative units data of Kenya.

my_rasters <- getData('worldclim', res = 10, var = 'tmin')
my_polygon <- getData('GADM', country = 'KEN', level = 0)
masked_rasters <- mask(crop(my_rasters, my_polygon), my_polygon)
plot(masked_rasters[[2]])

Stacking the rasters

stacked_rasters <- raster::stack(masked_rasters) 

Running k-means

Running k-means on raster data requires use of raster.kmeans function in ecbtools package. To install the ecbtools package use:

remotes::install_github(“ozjimbob/ecbtools”)

You may need to install remotes package first to have the install_github function.

kmeans_layer <- ecbtools::raster.kmeans(x = stacked_rasters, # The stack
                             k = 4,# Number of clusters
                             nstart = 3, # random sets 
                             geo = TRUE, # weighting by location (x,y coords)
                             geo.weight = 1) # wweight to location

raster::plot(kmeans_layer)

Saving the raster output

Saving the raster into the current working directory for future reference or subsequent analysis/sharing as need may be.

raster::writeRaster(kmeans_layer, 'output_clusters.tif', overwrite = TRUE) 

References

Allaire, JJ, Yihui Xie, Jonathan McPherson, Javier Luraschi, Kevin Ushey, Aron Atkins, Hadley Wickham, Joe Cheng, Winston Chang, and Richard Iannone. 2021. Rmarkdown: Dynamic Documents for r. https://CRAN.R-project.org/package=rmarkdown.
Bivand, Roger S., Edzer Pebesma, and Virgilio Gomez-Rubio. 2013. Applied Spatial Data Analysis with R, Second Edition. Springer, NY. https://asdar-book.org/.
Hijmans, Robert J. 2021. Raster: Geographic Data Analysis and Modeling. https://rspatial.org/raster.
Hijmans, Robert J., Steven Phillips, John Leathwick, and Jane Elith. 2020. Dismo: Species Distribution Modeling. https://rspatial.org/raster/sdm/.
Pebesma, Edzer J., and Roger S. Bivand. 2005. “Classes and Methods for Spatial Data in R.” R News 5 (2): 9–13. https://CRAN.R-project.org/doc/Rnews/.
Pebesma, Edzer, and Roger Bivand. 2021. Sp: Classes and Methods for Spatial Data. https://CRAN.R-project.org/package=sp.
R Core Team. 2021. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.
Williamson, Grant. 2016. Ecbtools: Environmental Change Biology Tools.
Xie, Yihui. 2014. “Knitr: A Comprehensive Tool for Reproducible Research in R.” In Implementing Reproducible Computational Research, edited by Victoria Stodden, Friedrich Leisch, and Roger D. Peng. Chapman; Hall/CRC. http://www.crcpress.com/product/isbn/9781466561595.
———. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. https://yihui.org/knitr/.
———. 2021. Knitr: A General-Purpose Package for Dynamic Report Generation in r. https://yihui.org/knitr/.
Xie, Yihui, J. J. Allaire, and Garrett Grolemund. 2018. R Markdown: The Definitive Guide. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown.
Xie, Yihui, Christophe Dervieux, and Emily Riederer. 2020. R Markdown Cookbook. Boca Raton, Florida: Chapman; Hall/CRC. https://bookdown.org/yihui/rmarkdown-cookbook.