Skip to content

Instantly share code, notes, and snippets.

@vsimko
Created July 4, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsimko/cd83f31924cc0f8ee7b93642304306ba to your computer and use it in GitHub Desktop.
Save vsimko/cd83f31924cc0f8ee7b93642304306ba to your computer and use it in GitHub Desktop.
# these libraries required corrplot 0.84, download from github
# install.packages("devtools")
# devtools::install_github("taiyun/corrplot")
library(raster)
library(corrplot)
citation("corrplot")
s <- stack("~/Work/biggis/data/data-julian/Auto150_georef.tif")
nlayers(s)
plot(s[[1]])
s3<-s
# now we manually draw some extent for cropping
# please wait during cropping, it may take few seconds - minutes
e <- drawExtent()
s3 <- crop(s, e)
# plotting the new cropped extent
plot(s3[[1]])
# # randomly chosen pixels (by offset in the vector of all pixels)
# a = 170
# b = 169
#
# # drawing all bands from two pixels
#
# plot(NA, xlim=c(1, nlayers(s3)), ylim=c(0, max(maxValue(s3))) )
# lines(1:nlayers(s3), s3[a], col="red")
# lines(1:nlayers(s3), s3[b], col="blue")
# create a matrix where cols are bands and rows are all pixels
m <- matrix(NA, ncol = nlayers(s3), nrow = ncell(s3) )
colnames(m) <- paste0("b", 1:ncol(m))
for(i in 1:138) {
m[,i] <- values(s3[[i]])
}
m2 <- m[,20:119] # band 138 seems to always contain zeroes -> removing
dim(m2) # sanity check
# compute correlation amongst all bands pairwise
C <- cor(m2, use = "everything", method = "pearson")
# use corrplot to visualize this (including FPC reordeing of the features)
corrplot(C, method = "color", type = "upper", order = "hclust", tl.cex = 0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment