A more complicated object
The raster package provides the RasterLayer
object, but also a couple of more complicated objects: RasterStack
and RasterBrick
. These two objects are designed for storing many rasters, all of the same extents and dimension (a.k.a. multi-band, or multi-layer rasters).
You can think of RasterLayer
like a matrix, but RasterStack
and RasterBrick
objects are more like three dimensional arrays. One additional thing you need to know to handle them is how to specify a particular layer.
You can use $
or [[
subsetting on a RasterStack
or RasterBrick
to grab one layer and return a new RasterLayer
object. For example, if x
is a RasterStack
, x$layer_name
or x[["layer_name"]]
will return a RasterLayer
with only the layer called layer_name
in it.
Let's look at a RasterStack
object called pop_by_age
that covers the same area as pop
but now contains layers for population broken into few different age groups.
This exercise is part of the course
Visualizing Geospatial Data in R
Exercise instructions
- Print
pop_by_age
. Can you see the names of all the layers? - Subset out the
under_1
layer using[[
subsetting. - Plot the
under_1
layer by passing your code from the previous instruction toplot()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Print pop_by_age
# Subset out the under_1 layer using [[
# Plot the under_1 layer