Videos
Factsheet
First, a small vent. I think it’s ridiculous that postal code datasets are proprietary. This should be open data, like every other administrative boundary in the country. Canada Post trying to turn a profit is gross. It straddles a strange line between a crown corporation and a private organization…
Secondly, I’m curious how y’all get your postal code datasets? I work for a small municipality. We currently have a pretty convoluted workflow that takes a land ownership table, cross-references the owner’s full address with the parcel address to extract the postal code to the parcel, remove outliers like owners who don’t live in their house, dissolve the parcel boundaries together where the code matches…There must be a better way?
As I understand it, the postal code lookup website also has functions to block you from simply running a script to scrape their data en masse, so I know that’s not an option…
This is a partial answer dealing with cleaning up your postal code regions. Suppose you use a convex hull to get a bunch of vertices that define polygons.
library(polyclip)
library(ggplot2)
canada <- data.frame(x = c(0,1,1,0),
y = c(0,0,1,1))
postcode1 <- data.frame(x = c(0.5, 1.5, 1.5, 0.8),
y = c(0.5, 0.8, 1.5, 1.5))
postcode2 <- data.frame(x = c(0.25, 0.75, 0.75, 0.25),
y = c(0.25, 0.25, 0.75, 0.75))
ggplot() +
geom_polygon(data = canada, aes(x,y), fill = "lightblue", color = "blue", alpha = 0.5) +
geom_polygon(data = postcode1, aes(x,y), fill = "lightgreen", color = "green", alpha = 0.5) +
geom_polygon(data = postcode2, aes(x,y), fill = "pink", color = "red", alpha = 0.5)
You can see that the convex hulls of the postal code regions overlap each other, and one of them extends outside of the country. The polyclip package lets you take intersections, unions, xor and setdiff of the regions bounded by the vertices.
clip1 <- polyclip(postcode1, canada, "intersect")[[1]] |> as.data.frame()
clip2 <- polyclip(postcode2, clip1, "minus")[[1]] |> as.data.frame()
ggplot() +
geom_polygon(data = canada, aes(x,y), fill = "lightblue", color = "blue", alpha = 0.5) +
geom_polygon(data = clip1, aes(x,y), fill = "lightgreen", color = "green", alpha = 0.5) +
geom_polygon(data = clip2, aes(x,y), fill = "pink", color = "red", alpha = 0.5)
Happy clipping!
Readapted from a former adaptation made for Brazil. Colorful, apparently display right mapping for all Canada. Please consider this would be a simple example, nothing robust or elegant:
library(maptools)
library(RColorBrewer)
library(ggmap)
library(plyr)
area <- readShapePoly("canada.shp")
area.points <- fortify(area)
loc="Toronto"
mapImage <- get_map(location=loc,
color="color",
zoom=10,
maptype="roadmap")
extents <- geocode(loc, output="more")
area.zoom <- subset(area.points,
extents$west <= long & long <= extents$east &
extents$south <= lat & lat <= extents$north)
ggmap(mapImage) +
geom_polygon(aes(x = long,
y = lat,
group = group,
fill = id),
data = area.zoom,
color = 'white',
#fill = 'black',
alpha = 0.4) +
coord_map(projection="mercator")+
labs(x = "Longitude",
y = "Latitude")
The MAJOR limitation is you have to change Loc. I am trying to understand how I would have full country coverage. I considered first to a conditional pointing to a vector of Loc, then I would loop while it sweeps through the Loc vectors.
I did not like this because the graphic seems challenged. Need to dig deeper.