mapcan
is an R package that provides convenient tools
for plotting Canadian choropleth maps and choropleth alternatives.
library(devtools)
install_github("mccormackandrew/mapcan", build_vignettes = TRUE)
I suggest that you pass the build_vignettes = TRUE
to
install_github()
. The vignettes provide detailed guides on
how mapcan
’s functions operate.
mapcan
data are best utilized with
ggplot2
library(mapcan)
library(tidyverse)
riding_binplot()
can be used to create tile cartograms
at federal and provincial riding levels (note that only Quebec
provincial ridings are supported right now). Here is an example:
# Load data that will be plotted with riding_binplot
<- mapcan::federal_election_results %>%
fed2015 filter(election_year == 2015)
riding_binplot(riding_data = fed2015,
# Use the party (winning party) varibale from fed2015
value_col = party,
# Arrange by value_col within provinces
arrange = TRUE,
# party is a categorical variable
continuous = FALSE) +
# Change the colours to match the parties' colours
scale_fill_manual(name = "Party",
values = c("mediumturquoise", "blue", "springgreen3", "red", "orange")) +
# mapcan ggplot theme removes axis labels, background grid, and other unnecessary elements when plotting maps
theme_mapcan() +
ggtitle("Tile grid map of 2015 federal election results")
Perhaps you are averse to squares. That is ok. Try hexagons instead:
riding_binplot(riding_data = fed2015,
# Use the party (winning party) varibale from fed2015
value_col = party,
# Arrange by value_col within provinces
arrange = TRUE,
# party is a categorical variable
continuous = FALSE,
shape = "hexagon") +
# Change the colours to match the parties' colours
scale_fill_manual(name = "Party",
values = c("mediumturquoise", "blue", "springgreen3", "red", "orange")) +
# mapcan ggplot theme removes axis labels, background grid, and other unnecessary elements when plotting maps
theme_mapcan() +
ggtitle("Hex tile map of 2015 federal election results")
Perhaps you are interested in provincial election results, not federal election results. That is also ok. Try plotting the Quebec 2018 provincial election results:
# Load data that will be plotted with riding_binplot
riding_binplot(quebec_provincial_results,
value_col = party,
riding_col = riding_code,
continuous = FALSE,
provincial = TRUE,
province = QC,
shape = "hexagon") +
theme_mapcan() +
scale_fill_manual(name = "Winning party",
values = c("deepskyblue1", "red","royalblue4", "orange")) +
ggtitle("Hex tile map of 2018 Quebec election results")
The mapcan()
function returns geographic coordinate data
frames at census division, federal riding, and provincial levels.
Not interested in the territories? No problem.
mapcan()
can also be used to plot population cartograms.
Based on the geographic distribution of Canadians (most Canadians live
near the US border and very few live in the north), these maps are
highly distorted. Shading the census divisions by population size shows
how the cartogram inflates divisions with larger populations
(i.e. Vancouver, Edmonton, Calgary, Toronto, and Montreal all become
larger).
# Census population data is included in the geographic data frame that mapcan() returns
<- mapcan(boundaries = census,
census_cartogram_data type = cartogram)
ggplot(census_cartogram_data, aes(long, lat, group = group, fill = population_2016)) +
geom_polygon() +
scale_fill_viridis_c() +
theme_mapcan() +
coord_fixed() +
ggtitle("Population cartogram of census division populations")
Let’s plot the share of individuals born outside of Canada in each census division as a standard choropleth map then as a population cartogram.
# Get census born outside of Canada data to use with geographic data
<- mapcan::census_pop2016 %>%
census_immigrant_share select(census_division_code, born_outside_canada_share)
# Get population cartogram geograpic data
<- mapcan(boundaries = census,
census_choropleth_data type = standard)
# Merge together
<- left_join(census_choropleth_data, census_immigrant_share)
census_choropleth_data
ggplot(census_choropleth_data, aes(long, lat, group = group, fill = born_outside_canada_share)) +
geom_polygon() +
scale_fill_viridis_c(name = "Share of population born \noutside of Canada") +
theme_mapcan() +
coord_fixed() +
ggtitle("Population cartogram of foerign born population by census division")
# Get population cartogram geograpic data
<- mapcan(boundaries = census,
census_cartogram_data type = cartogram)
# Merge together
<- left_join(census_cartogram_data, census_immigrant_share)
census_cartogram_data
ggplot(census_cartogram_data, aes(long, lat, group = group, fill = born_outside_canada_share)) +
geom_polygon() +
scale_fill_viridis_c(name = "Share of population born \noutside of Canada") +
theme_mapcan() +
coord_fixed() +
ggtitle("Population cartogram of foerign born population by census division")
Comparing these two maps, it is clear that the standard choropleth map visually understates the share of the population that is foreign born in Canada.