A package that uses Raster objects
You saw the tmap package makes visualizing spatial classes in sp easy. The good news is that it works with the raster classes too! You simply pass your Raster___ object as the shp argument to the tm_shape() function, and then add a tm_raster() layer like this:
tm_shape(raster_object) +
tm_raster()
When working with a RasterStack or a RasterBrick object, such as the pop_by_age object you created in the last exercise, you can display one of its layers using the col (short for "color") argument in tm_raster(), surrounding the layer name in quotes.
You'll work with tmap throughout the course, but we also want to show you another package, rasterVis, also designed specifically for visualizing raster objects. There are a few different functions you can use in rasterVis to make plots, but let's just try one of them for now: levelplot().
This exercise is part of the course
Visualizing Geospatial Data in R
Exercise instructions
- Use
tmapto plot thepopobject, by specifyingpopas theshpargument totm_shape()and adding atm_raster()layer. - Use
tmapto plot theunder_1layer ofpop_by_age, aRasterStackobject. - Call the
rasterVisfunctionlevelplot()onpop.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
library(tmap)
# Specify pop as the shp and add a tm_raster() layer
tm_shape(___) +
___
# Plot the under_1 layer in pop_by_age
library(rasterVis)
# Call levelplot() on pop