1. Learn
  2. /
  3. Courses
  4. /
  5. Visualizing Geospatial Data in R

Exercise

tmap, a package that works with sp objects

You've had to learn quite a few new things just to be able to understand and do basic manipulation of these spatial objects defined by sp, but now you get to experience some payoff! There are a number of neat packages that expect spatial data in sp objects and which make working with spatial data easy.

Let's take a look at the tmap package for creating maps. You'll learn more about its philosophy and structure in the next video, but first we want you to see how easy it is to use.

tmap has the qtm() function for quick thematic maps. It follows the ideas of qplot() from ggplot2 but with a couple of important differences. Instead of expecting data in a data frame like ggplot2(), it expects data in a spatial object and uses the argument shp to specify it. Another important difference is that tmap doesn't use non-standard evaluation (see the Writing Functions in R course for more about this), so variables need to be surrounded by quotes when specifying mappings.

Try this example in the console:

library(tmap)
qtm(shp = countries_spdf, fill = "population")

How easy was that!? Can you make a choropleth of another variable contained in countries_spdf: gdp?

Instructions

100 XP

Using the example as a guide, create a choropleth map of the gdp variable using qtm().