Skip to content

Instantly share code, notes, and snippets.

@walkerke
Created February 22, 2018 15:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save walkerke/87cef3c1a95b5bb63763cb6b95364652 to your computer and use it in GitHub Desktop.
Save walkerke/87cef3c1a95b5bb63763cb6b95364652 to your computer and use it in GitHub Desktop.
library(cancensus)
library(tidyverse)
library(sf)
library(tmap)
library(tmaptools)
options(cancensus.api_key = "your key goes here")
montreal <- get_census(dataset = "CA16", regions = list(CMA = "24462"),
vectors = c("v_CA16_1364", "v_CA16_1367"), level = "CT",
geo_format = "sf", labels = "short")
english_dots <- montreal %>%
st_transform(32618) %>%
mutate(english = as.integer(v_CA16_1364 / 200)) %>%
mutate(english = ifelse(is.na(english), 0, english)) %>%
st_sample(., .$english) %>%
st_sf()
url <- "path to your desired basemap tileset"
osm <- read_osm(bb(montreal), type = url)
b1 <- tm_shape(osm) +
tm_raster() +
tm_shape(english_dots) +
tm_dots(alpha = 0.4, col = "red") +
tm_layout(title = "English as first language in the Montreal area") +
tm_credits("1 dot = 200 people. Data source: 2016 Canadian Census via the R cancensus package")
save_tmap(b1, "img/english.jpg")
french_dots <- montreal %>%
st_transform(32618) %>%
mutate(french = as.integer(v_CA16_1367 / 200)) %>%
mutate(french = ifelse(is.na(french), 0, french)) %>%
st_sample(., .$french) %>%
st_sf()
b2 <- tm_shape(osm) +
tm_raster() +
tm_shape(french_dots) +
tm_dots(alpha = 0.4, col = "#1C5BA2") +
tm_layout(title = "French as first language in the Montreal area") +
tm_credits("1 dot = 200 people. Data source: 2016 Canadian Census via the R cancensus package")
save_tmap(b2, "img/french.jpg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment