1

I have three rasters:

> raster1
class      : RasterLayer 
dimensions : 2803, 5303, 14864309  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : 60.85, 105.0417, 15.95833, 39.31667  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : memory
names      : layer 
values     : 0, 31.72  (min, max)

> landcoverraster
class      : RasterLayer 
dimensions : 2803, 5303, 14864309  (nrow, ncol, ncell)
resolution : 0.008333333, 0.008333333  (x, y)
extent     : 60.85, 105.0417, 15.95833, 39.31667  (xmin, xmax, ymin, ymax)
crs        : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
source     : C:/Users/XX.tif 
names      : landusemaskedme 
values     : 1, 12  (min, max)
attributes :
       ID
 from:  1
  to : 12

I would like to plot scatterplots between raster1 and raster2 according to landcover types in landcoverraster. But first I would like to subset a random subset of raster1 and raster2 (as they are very large rasters and when I plot scatterplots it gives a big chuncky scatterplot) according to landcover type. How can I subset a random raster according to land cover type and plot a scatterplot between raster1 and raster2? I am looking a desired output similar to this desired output

What I initally have done (without subsetting), and came up with this plot:

plot(raster1[landcoverrraster==1],raster2[landcoverraster==1]) 

myresult

Reproducible rasters:

library(raster)

ras1 <- raster(matrix(c(1,1,1,2,2,2)))
ras2 <- raster(matrix(c(1,1,1,2,2,2)))

#Generating landcover example data
raster2 <- raster(matrix(c(1,1,1,2,2,2,3,3,3),ncol =3))
raster2 <- as.factor(raster2)

rat <- levels(raster2 )[[1]]
rat[["landcover"]] <- c("land","ocean/lake", "rivers")
levels(raster2 ) <- rat
1

Your example rasters didn't have the same resolution, so I added rows to ras1 and ras2 to remedy that. Furthermore, in the code below I used sampleRandom() which returns a simple random sample. Depending on your data, sample size and reason for drawing a sample you could also consider employing sampleRegular(), or sampleStratified() using the different landcover types as the strata. You write that you want to draw a sample to avoid 'chunky' scatter-plots, but you could consider binning/aggregating the data for visualisation instead. Anyway, here is one approach:

library(raster)

# Example from question with expanded ras1 and ras2 to match resolution
ras1 <- raster(matrix(c(1,1,1,2,2,2,3,3,3)))
ras2 <- raster(matrix(c(1,1,1,2,2,2,3,3,3)))
ras3 <- raster(matrix(c(1,1,1,2,2,2,3,3,3)))
ras3 <- as.factor(ras3)
rat <- levels(ras3)[[1]]
rat[["landcover"]] <- c("land","ocean/lake", "rivers")
levels(ras3) <- rat

# Create raster stack
rs <- stack(ras1, ras2, ras3)

# Find a simple random sample of the stack, equating to 90% of the cells.
rs_sr <- sampleRandom(rs, ncell(rs)*0.9, cells = TRUE)
# Use the returned cell numbers to extract the sample from the raster stack. 
# Return this as a data frame and with factor values for the landcover layer.
sr_df <- extract(rs, rs_sr[,1], factors = TRUE, df = TRUE)

# Rename columns 
names(sr_df) <- c('ID', 'ras1', 'ras2', 'landcover')
# Retrieve subset of values by the "land" landcover
land <- sr_df[which(sr_df$landcover == "land"),]

# Plot. Since the values for the "land" landcover are all 1 on the 
# two example rasters, this doesn't look like much of course.
plot(land$ras1, land$ras2)

enter image description here

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.