We demonstrate how to import data from the CensusMapper API. The code for this notbook can be found on GitHub.

To work with the CensusMapper API we need to specify our API key. We load the API key in the environment variable “CM_API_KEY”, which we assume was set prior to running the script. We could also specify it directly in the code, but than we have to be careful not to expose our API key when sharing our code.

library(cancensus)
# set environment variable via 
# Sys.setenv(CM_API_KEY='<your API key>')
# or
# cancensus.set_api_key('<your API key>')

To tie into the CensusMapper API we utilize the “censusmapper” library. As an example we choose the “occupied dwellings by structural type of dwelling” data from 2016, CT level, for the Vancouver CMA. The CensusMapper convenience function expects the year (2016), aggregation level (CT), geographies (CMA 59993), and CensusMapper internal variable names as input. We also need to speficy the CensusMapper API key for access. Lastly we may specify the “geo” flag, if it is set to TRUE we will also load the spatial data.

#set the data vectors we are interested in
vectors=c("v_CA16_408","v_CA16_409","v_CA16_410","v_CA16_412","v_CA16_413","v_CA16_414","v_CA16_415","v_CA16_416","v_CA16_417")
#set the regions we are intereseted in
regions_string='{"CMA":["59933"]}'
#load the data
census_data <- cancensus.load(dataset='CA16',level='CT',regions=regions_string,vectors=vectors)

Downloading: 3.2 kB     
Downloading: 3.2 kB     
Downloading: 7.3 kB     
Downloading: 7.3 kB     
Downloading: 11 kB     
Downloading: 11 kB     
Downloading: 13 kB     
Downloading: 13 kB     
Downloading: 13 kB     
Downloading: 13 kB     
Downloading: 13 kB     
Downloading: 13 kB     

Mapping

We compute a the percentage of single detached dwellings out of all occupied dwelling units and map it.

census_data$sd <- census_data$`v_CA16_409: Single-detached house` / census_data$`v_CA16_408: Occupied private dwellings by structural type of dwelling data`
library(tmap)
tm_shape(census_data) +
  tm_polygons("sd", style="quantile", title="Single Detached Homes")