Rasters can contain either continuous (e.g., elevation) or categorical (e.g., habitat type) data—they are only fancy matrices after all. Here is an example of how to set up a categorical raster with a Raster Attribute Table.

Example Data

These data are from github.

## class      : RasterLayer 
## dimensions : 246, 138, 33948  (nrow, ncol, ncell)
## resolution : 4, 4  (x, y)
## extent     : 304050, 304602, 4133356, 4134340  (xmin, xmax, ymin, ymax)
## crs        : NA 
## source     : /Users/rodney/Desktop/DLab-Spatial/data/pines.asc 
## names      : pines

This is a simple raster with two representations

##  [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0

zeros everywhere there is no pines and the category 2 where the conifer canopy exists.

For completeness, I’ll add a few more rasters.

Defining Categories

To begin, lets put these three together1.

Here is what the actual data look like.

##  [1] 0 0 0 0 0 0 0 0 0 0 0 0 3 1 2 0 0 0 0 0 0 0 0 3 3 3 0 1 3 0 2 0 1 1 1
## [36] 1 1 0 3 3

And it is not a factor:

## [1] FALSE

To categorize it, we need to both make it a factor:

##   ID
## 1  0
## 2  1
## 3  2
## 4  3

To make it more usable, we can define these categories on the raster using a Raster Attribute Table (RAT)

## class      : RasterLayer 
## dimensions : 246, 138, 33948  (nrow, ncol, ncell)
## resolution : 4, 4  (x, y)
## extent     : 304050, 304602, 4133356, 4134340  (xmin, xmax, ymin, ymax)
## crs        : NA 
## source     : memory
## names      : shrubs 
## values     : 0, 3  (min, max)
## attributes :
##  ID Landcover
##   0      Open
##   1   Shrubry
##   2     Pines
##   3      Oaks

Extracting Data

Now we can play around with the levels a bit. Here, lets summarize a locale by grabbing a point in the middle of the forest raster2.

## [1] "POINT (304402 4133856)"

then extract the

## 
##  Oaks  Open Pines 
##     4     2     6

OK, so now, you have a categorical data raster and can play with it.


  1. There will be overlap and we are going to pretend it is not an issue, this is a tutorial after all!

  2. I know, the coordinate system is whack, but again, this is a tutorial