I taught my Data Visualization seminar in Philadelphia this past Friday and Saturday. It covers most of the content of my book, including a unit on making maps. The examples in the book are from the United States. But what about other places? Two of the participants were from Canada, and so here’s an example that walks through the process of grabbing a shapefile and converting it to a simple-features object for use in R. A self-contained R project with this code is available on GitHub.
Mapping data (and, more importantly, converting map data into a format that we can tidily use) requires a bit more heavy lifting behind the scenes than the core tidyverse
libraries provide. We start by (if necessary) installing the sf
library, which will also bring a number of dependencies with it. We’ll also install a few packages that provide some functions we might use in the conversion and mapping process.
1 |
|
Then we load the packages, along with others we’ve already installed (as part of the seminar).
1 |
|
We make a function, theme_map()
, that will be our ggplot theme. It consists mostly in turning off pieces of the plot (like axis text and so on) that we don’t need.
1 |
|
Next we need to get the actual map data. This is often the hardest bit. In the case of Canada, their central statistics agency provides map shape files that we can use. The Shape File format (.shp
) is the most widely-used standard for maps. We’ll need to grab the files we want and then convert them to a format the tidyverse can use. You won’t be able to run the code in the next few chunks unless the files are downloaded where the code expects them to be.
So, get the data from Statistics Canada. We’re going to use this link: http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/bound-limit-2011-eng.cfm. From the linked page, choose as your options ArcGIS .shp file, then—for example—Census divisions and cartographic boundaries. You’ll then download a zip file. Expand this zip file into the directory in your working folder named ‘data’. Then we can import the shapefile using one of rgdal
’s workhorse functions, readOGR
.
1 |
|
Note the options here. This information is contained in the documentation for the shape files. Now we’re going to convert this object to GeoJSON format and simplify the polygons. These steps will take a little while to run:
1 |
|
Then we save the resulting GeoJSON file, which we can now work with directly from here on out:
1 |
|
We only need to do the importing, converting, and thinning once. If you already have a GeoJSON file, you can just start here.
Read the GeoJSON file back in as an sf
object. The .geojson
file is included in the repository, so you can load the libraries listed above and then start from here if you want.
1 |
|
Notice the proj4string
there in the metadata. We’re going to change that to Canada’s favorite projection, the Lambert Conformal Conic projection. See this discussion for details.
1 |
|
Finally, and just because I don’t have any census-district-level data to hand at the moment, we make a vector of repeated colors to fill in the map, for decoration only, if you want to color all the census divisions.
1 |
|
Instead, we’ll just map the fill to PRUID
, i.e. the province level. But try mapping fill
to CDUID
instead (the census district ID), and see what happens.
1 |
|
Map of Canada, colored by Province, showing 2011 Census District boundaries.
And there we have it. A map of Canada with census-division boundaries that you can join data to, in the same way as described in the “Draw Maps” chapter of Data Visualization: A Practical Introduction.
Related