Learn R Programming

mapcan

mapcan is an R package that provides convenient tools for plotting Canadian choropleth maps and choropleth alternatives.

Installing mapcan

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.

Using mapcan

mapcan data are best utilized with ggplot2

library(mapcan)
library(tidyverse)

Tile grid map of Canadian federal electoral ridings

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 
fed2015 <- mapcan::federal_election_results %>%
  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")

Standard choropleth maps

The mapcan() function returns geographic coordinate data frames at census division, federal riding, and provincial levels.

Not interested in the territories? No problem.

Population cartograms

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
census_cartogram_data <- mapcan(boundaries = census,
       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
census_immigrant_share <- mapcan::census_pop2016 %>%
  select(census_division_code, born_outside_canada_share)

# Get population cartogram geograpic data
census_choropleth_data <- mapcan(boundaries = census,
       type = standard)

# Merge together 
census_choropleth_data <- left_join(census_choropleth_data, census_immigrant_share)

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
census_cartogram_data <- mapcan(boundaries = census,
       type = cartogram)

# Merge together 
census_cartogram_data <- left_join(census_cartogram_data, census_immigrant_share)

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.

Copy Link

Version

Install

install.packages('mapcan')

Monthly Downloads

180

Version

0.0.1

License

MIT + file LICENSE

Maintainer

Andrew McCormack

Last Published

May 9th, 2019

Functions in mapcan (0.0.1)

theme_mapcan

Mapcan theme
census_pop2016

Census division population data for 2011 and 2016
quebec_provincial_results

Quebec provincial election results data
riding_binplot

Canadian federal ridings tile plot function
quebec_prov_ridings2018

Quebec provincial ridings geographic data
riding_info

Canadian federal riding population information
province_pop_annual

Annual provincial populations data frame dating back to 1971
provinces_noterr_carto

Provinces and territories cartogram data (territories excluded)
provinces_territories_carto

Provinces and territories cartogram data (territories included)
provinces_territories

Provinces and territories standard geographic data
quebec_riding_bins

Quebec provincial riding bins (used for tile plots)
quebec_riding_hexagons

Quebec provincial riding hexagons (used for hexagonal tile plots)
census_divisions_2016

Census divisions (2016)
census_divisions_2016_noterr_carto

Census divisions cartogram data frame (territories excluded) (2016)
census_divisions_2016_carto

Census divisions cartogram data frame (territories included) (2016)
mapcan

Canadian maps function
%>%

Pipe operator
federal_election_results

Canadian federal election results data
federal_riding_hexagons

Canadian federal riding hexagons (used for hexagonal tile plots)
federal_riding_bins

Canadian federal riding bins (used for tile plots)
federal_ridings

Federal ridings