Get startedGet started for free

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

View Course

Exercise instructions

  • Use tmap to plot the pop object, by specifying pop as the shp argument to tm_shape() and adding a tm_raster() layer.
  • Use tmap to plot the under_1 layer of pop_by_age, a RasterStack object.
  • Call the rasterVis function levelplot() on pop.

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
Edit and Run Code