I have two classified landsat images from 2014 and 2018 both of them containing 6 classes including urban,forest,barren etc. In order to see how each pixel's class changed or remained same from earlier image i computed cross table taking both images like this :
contingencyTable <- raster::crosstab(landscape_2014, landscape_2018, long = TRUE)
This gave me the number of pixels in both images for each class but this has no latitude or longitude so this is where my question arises. How can i convert this dataframe/table into a raster so that i am able to visualize that a specific area changed from forest to urban and so on?
I know how to convert dataframe to a raster using rasterFromXYZ(df)
but that requires lat,long which is not there in the table.
The table looks something like this:
crosstab(r1,r2)
[r1] [r2] Count
0 0 3456
1 41 23456
0 41 768
1 42 21
0 42 6
I am following Creating Land Cover Change Classification in R? for detecting land cover change
** R Code **
library(raster)
f2014<- raster("landsat_2014.tif")
f2018<- raster("landsat_2018.tif")
output <- overlay(f2014,
f2018,
fun=function(r1, r2){return(r1-r2)})
plot(output)
crosstab
output is an error matrix, it has no spatial attributes. What you need is a more sophisticated analysis. Since you aren't posting data or code values, I can't elaborate an accurate answer for your needs. You can use logical functions for meeting your needsplot()
that should give me the change for each pixel. right ?