This vignette demonstrates how to pull in under construction data from CMHC using the cmhc package, link it with geographic data from CensusMapper using the cancensus package and map the under construction data. ## CMHC data

library(dplyr)
#devtools::install_github("mountainmath/cmhc")
library(cmhc)

cma="Montreal"
year=2021
month=07


census_cma=census_geography_list[[cma]]
cma_header=substr(census_cma, nchar(census_cma)-2,nchar(census_cma))

#get all under construction data for Vancouver and pad CT GeoUIDs.
under_construction <- get_cmhc(
  cmhc_snapshot_params(
    table_id = cmhc_table_list["Scss Under Construction CT"],
    breakdown_geography_type="CT",
    geography_id = cmhc_geography_list[[cma]],
    year = year,
    month = month))

under_construction <- under_construction %>%
  mutate(GeoUID = cmhc_geo_uid_for_ct(cma_header,X1))

Geographic data

library(cancensus)
library(ggplot2)
library(sf)
library(RColorBrewer)
#options(cancensus.api_key='your API key')
geos <- get_census(dataset = 'CA16', regions=list(CMA=census_cma),level='CT',geo_format='sf')

Joining the data

geos <- inner_join(geos,under_construction, by="GeoUID") %>%
  sf::st_sf()

Graph

bg_color="#c0c0c0"
theme_opts<-list(theme(panel.grid.minor = element_blank(),
                       #panel.grid.major = element_blank(), #bug, not working
                       panel.grid.major = element_line(colour = bg_color),
                       panel.background = element_rect(fill = bg_color, colour = NA),
                       plot.background = element_rect(fill=bg_color, size=1,linetype="solid"),
                       axis.line = element_blank(),
                       axis.text.x = element_blank(),
                       axis.text.y = element_blank(),
                       axis.ticks = element_blank(),
                       axis.title.x = element_blank(),
                       axis.title.y = element_blank()))

After defining a basic theme we can go ahead and map the data.

breaks=c(-Inf,1,25,50,100,250,500,750,1000,2000,Inf)
labels <- c(paste0("0 - ",breaks[2]))
for(i in 2:(length(breaks)-2)){
  labels[i] = paste(breaks[i],breaks[i+1], sep=" - ")
}
labels[length(breaks)-1]=paste0("Over ",breaks[length(breaks)-1])
#colors=c("darkred",(RColorBrewer::brewer.pal(length(labels)-1,"YlGnBu")))
labels=factor(labels, levels=labels)
colors=setNames(c("#808080",RColorBrewer::brewer.pal(length(labels)-1,"PiYG")),labels)
#colors=factor(as.character(colors),levels=as.character(colors))

# categorize the numbers under contruction
geos$categories <- geos$All %>% cut(breaks=breaks, labels=labels)
total=sum(geos$All)

ggplot(geos) +
  geom_sf(aes(fill = categories), size = 0.05) +
  scale_fill_manual(labels=labels, values=colors, name = "# Units") +
  ggtitle(paste0(cma, " CMA Under Construction ",month,"-",year," (",prettyNum(total,big.mark = ",")," total)")) +
  theme_opts