0

I have created a change detection plot shown in image below for two rasters having 6 classes each using the code below. Now the plot, shows the change for all the classes but i am interested in only visualizing the change for class 4 and 5 i.e. pixels where class 4/5 changed to another class or where another class changed to 4/5.

How can i do this task?

enter image description here

Here is my sample code:

library(raster)
library(rasterVis)

r <- raster()

set.seed(123)

lc1 <- setValues(r, sample(1:6, 64800, replace = T))
lc2 <- setValues(r, sample(1:6, 64800, replace = T))
lc1_uniq <- unique(lc1)
lc2_uniq <- unique(lc2)
grid_ <- expand.grid(lc1_uniq,lc2_uniq)
names(grid_) <- c('from','to')
grid_$code <- 1:dim(grid_)[1]
grid_$change <- grid_[,1] != grid_[,2]

head(grid_)
change <- function(x){
  grid_[x[1] == grid_[,1] & x[2] == grid_[,2],'code']
}
changeDet1 <- calc(stack(lc1,lc2), fun = change)
codes_ <- data.frame(ID = grid_$code,value = paste0('from ',grid_[,1],' to ',grid_[,2]))
logical_test <- which(grid_$change == T) # remove no change classes
codes_ <- codes_[logical_test,]
# Create a Raster Attribute Table
rat <- levels(changeDet1)[[1]]
rat[["Changes"]] <- codes_
rat[["Changes"]] <- c("land","ocean/lake", "rivers","water bodies")
levels(changeDet1) <- rat
# Plot
levelplot(changeDet1, par.settings=PuOrTheme(), xlab="", ylab="")

This question continues Converting output of crosstab() in R to raster; from classified raster images to show overall land cover change in different classes?. All the details (plots,comments) might help in understanding this question more.

  • Need some clarity on: "I just want to visualize the data for class 4 and 5, i.e. classes that have changed to 4,5 and the change that occurred in class 4,5 themselves." You want one binary raster of pixels that have changed from all other classes to 4 or 5, and another binary raster of all pixels that were 4 or 5 and are now not 4 or 5? – Spacedman Aug 3 at 21:30
  • @Spacedman so the code above produces a raster which shows changes in all the 6 classes but i am interested in only viewing the changes in class 4 and 5 for this study. so how can i just view that data? – Rehan Aug 3 at 21:41
  • "what i want is to have only those values which contain 5 or 6 in either from or to" Did you mean "4 or 5"? – Spacedman Aug 4 at 8:42
  • @Spacedman yes. i have updated the question – Rehan Aug 4 at 18:11
0

i was able to accomplish this task by subsetting the data from the data frame which matched my classes. Here is the updates code section:

changeDet1 <- calc(stack(lc1,lc2), fun = change)
codes_ <- data.frame(ID = grid_$code,value = paste0('from ',grid_[,1],' to ',grid_[,2]))
logical_test <- which(grid_$change == T) # remove no change classes
codes_ <- codes_[logical_test,]
codes_
a<-subset(codes_, ID %in% c(2,10,12,13,14,15,16,17,18,20,29,38,47,56,65,74))
rat <- levels(changeDet1)[[1]]
rat[["Changes"]] <- a
levels(changeDet1) <- rat
levelplot(changeDet1, par.settings=PuOrTheme(), xlab="", ylab="")

After creating the dataframe and removing similar classes in both columns i manually checked the ID's of classes which i was interested in and created a subset of that which helped me in visualizing on the classes which i am interested in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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