Canadian Census

A few weeks ago I did a post on the StatCan API

I have since come across the new cancensus package, a wrapper function for CensusMapper API, in beta and not yet available on CRAN . These has been developed by the Vancouver team at MountainMath featuring Jens von Bergmann. His blog, Mountain Doodles is well worth a read - and, usefully, includes his twitter feed

You will need to get an API key to play around with the data. See the github page for instructions

Statistics Canada are in the process of rolling out information from the 2016 census, so the release of this package is particularly timely.

Please note, this is just an example of using the package with raw data and not an in depth analysis of any census information

Load up libraries and API key


# In beta so probably worth running each time
#devtools::install_github("mountainmath/cancensus")
library(cancensus) #0.1.0
library(tidyverse)
library(plotly)
library(leaflet)
library(viridis) 
library(sf) 


# Obtain your own key
 # options(cancensus.api_key = "Your_Census_Key")
 

The Basics

The workhorse at retrieving data is the get_census() function. By default, no spatial data is returned but if you want to draw maps you can return spatial data objects in either sf or sp formats. If you are planning to use this package regularly and/or are downloading lots of data you can obtain the geometry alone via get_census_geometry() and join datasets if and when required


get_census(dataset, level, regions, vectors = c(), geo_format = NA,
  labels = "detailed", use_cache = TRUE, quiet = FALSE,
  api_key = getOption("cancensus.api_key"))

There are quite a few parameters. There are some functions provided in the package to help us understand and use them

Firstly, a list of datasets available

list_census_datasets()
## # A tibble: 3 x 2
##   dataset                description
## *   <chr>                      <chr>
## 1    CA06         2006 Canada Census
## 2    CA11 2011 Canada Census and NHS
## 3    CA16         2016 Canada Census

So for the most recent data alone, we need the “CA16”" dataset

Now for the regions

#Select 2016 census
regions <- list_census_regions("CA16", quiet=TRUE) 

table(regions$level)
## 
##    C   CD  CMA  CSD   PR 
##    1  293   49 5148   13

regions %>%
     DT::datatable(class='compact stripe hover row-border order-column',rownames=FALSE,options= list(paging = TRUE, searching = TRUE,info=FALSE))

There are 5 levels of geography which can be called: country, province, census metropolitan area, census and census sub-district. Please note, that the data grouping within that can be finer - as we will see later

The table makes it easy to select a region of interest. Just use the search facility

Similarly we can set the full set of vectors (tables)

list_census_vectors("CA16", quiet=TRUE) %>%
     DT::datatable(class='compact stripe hover row-border order-column',rownames=FALSE,options= list(paging = TRUE, searching = TRUE,info=FALSE))