GeoJSON Version of CBC Quebec Ridings Hex Cartograms with Example Usage in R
This article is originally published at https://rud.is/b
The CBC covered the recent (as of the original post-time on this blog entry) Quebec elections and used a well-crafted hex grid map to display results:
They have a great ‘splainer on why they use this type of map.
V8 package. Since most of the aforementioned work was mouse clicking and ~8 (disjointed) lines of accompanying super-basic R code, there’s not really much to show wrangling-wise1, but I can show an example of using the new GeoJSON file in R and the
library(sf) library(ggplot2) # get the GeoJSON file from: https://gitlab.com/hrbrmstr/quebec-hex-ridings or https://github.com/hrbrmstr/quebec-hex-ridings sf::st_read("quebec-ridings.geojson", quiet = TRUE, stringsAsFactors = FALSE) %>% ggplot() + geom_sf(aes(fill = regionname)) + coord_sf(datum = NA) + ggthemes::scale_fill_tableau(name = NULL, "Tableau 20") + ggthemes::theme_map() + theme(legend.position = "bottom")
And, with a little more ggplot2-tweaking and some
magick, we can even put it in the CBC-styled border:
library(sf) library(magick) library(ggplot2) plt <- image_graph(1488, 1191, bg = "white") sf::st_read("quebec-ridings.geojson", quiet=TRUE, stringsAsFactors=FALSE) %>% ggplot() + geom_sf(aes(fill=regionname)) + coord_sf(datum=NA) + scale_x_continuous(expand=c(0,2)) + scale_y_continuous(expand=c(0,0)) + ggthemes::theme_map() + theme(plot.margin = margin(t=150)) + theme(legend.position = "none") dev.off() # get this bkgrnd img from the repo image_composite(plt, image_read("imgs/background.png")) %>% image_write("imgs/composite-map.png")
You can tweak the border color with
magick as needed and there’s a
background2.png in the
imgs directory in the repo that has the white inset that you can further composite as needed.
With a teensy bit of work you should be able adjust the stroke color via
aes() to separate things as the CBC did.
It’s important to re-state that the CBC made the original polygons for the hexes (well, they made a set of grid points and open source software turned it into a set of SVG paths) and the background images. All I did was some extra bit of wrangling and conversionating2.
1 I can toss a screencast if there’s sufficient interest.
2 Totally not a word.
Please visit source website for post related comments.