diff --git a/cartograms/hex-cartograms.Rmd b/cartograms/hex-cartograms.Rmd new file mode 100644 index 0000000000000000000000000000000000000000..cd381f95fa3e4c4bae7cdfe8c6573c9731fe3cdb --- /dev/null +++ b/cartograms/hex-cartograms.Rmd @@ -0,0 +1,123 @@ +--- +title: "Non-contiguous hex-cartograms" +subtitle: "Local Authority and Middle-layer Super Output Area exmaples" +author: "Tom Rushby" +date: "25/06/2021" +output: html_document +--- + +```{r setup, include=FALSE} +knitr::opts_chunk$set(echo = TRUE) +``` + +```{r requiredPackages} +library(curl) +library(sf) +library(tidyverse) +library(ggplot2) +``` + + +## Introduction + +This example follows an example (and re-uses code) by [@VictimOfMaths](https://github.com/VictimOfMaths/Maps/blob/master/WFHCartogram.R) using hex-map data from [House of Commons Library](https://github.com/houseofcommonslibrary/uk-hex-cartograms-noncontiguous/) by [@carlbaker](https://twitter.com/carlbaker). + +The first example we will look at is plotting a non-contiguous hex cartogram of Local Authority areas. + +## Local Authority areas + +```{r downloadHexMaps} +ltla <- tempfile() +source <- ("https://github.com/houseofcommonslibrary/uk-hex-cartograms-noncontiguous/raw/main/geopackages/LocalAuthorities-lowertier.gpkg") +ltla <- curl_download(url=source, destfile=ltla, quiet=FALSE, mode="wb") +``` + +The file downloaded is a GeoPackage (.gpkg) file, an SQLite Database container (see http://www.geopackage.org/guidance/getting-started.html for more info.). We can examine the layers contained using the `st_layers()` command from the sf package. + +```{r examineGeoLayers} +st_layers(ltla) +``` + +Next we extract the layers we want using the `st_read` command. [Alternative methods](https://olalladiaz.net/blog/2018/11/02/working-with-gpkg-r/) are available. + +```{r extractGeoLayers} +Background <- st_read(ltla, layer="7 Background") + +Areas <- st_read(ltla, layer="4 LTLA-2019") + +Groups <- st_read(ltla, layer="2 Groups") + +Group_labels <- st_read(ltla, layer="1 Group labels") %>% + mutate(just=if_else(LabelPosit=="Left", 0, 1)) +``` + +And finally we can make a plot ... note the `Groups` geometry provides County-level grouping of local authorities and an outline (appears bold in the plot below). `Areas` provides the local authority outlines. + +```{r plotLocalAuthorities} +ggplot()+ + geom_sf(data=Background %>% filter(Name!="Ireland"), aes(geometry=geom)) + + geom_sf(data=Areas %>% filter(RegionNation != "Northern Ireland"), + aes(geometry=geom), colour="Black", size=0.2)+ + geom_sf(data=Groups %>% filter(RegionNation!="Northern Ireland"), + aes(geometry=geom), fill=NA, colour="Black") + + geom_sf_text(data=Group_labels, aes(geometry=geom, label=Group.labe, + hjust=just), size=rel(2.4), colour="Black") + + theme_void() + + theme(plot.title=element_text(face="bold", size=rel(1.5)), + legend.position="top") + + labs(title="Local Authorities Hex-Cartogram", + caption="Cartogram by House of Commons Library\nPlot by @tom_rushby") +``` + +## Middle-layer Super Output Areas + +Load GeoPackage file ... this time Middle-layer Super Output Areas (MSOAs) in England and Wales only. + +```{r} +msoa <- tempfile() +source <- ("https://github.com/houseofcommonslibrary/uk-hex-cartograms-noncontiguous/raw/main/geopackages/MSOA.gpkg") +msoa <- curl_download(url = source, destfile = msoa, quiet = FALSE, mode = "wb") +``` + +List layers ... + +```{r} +st_layers(msoa) +``` + +Extract layers ... + +```{r} +background_msoa <- st_read(msoa, layer="5 Background") +msoa_la_outlines <- st_read(msoa, layer="3 Local authority outlines (2019)") +msoa_data <- st_read(msoa, layer="4 MSOA hex") + +msoa_groups <- st_read(msoa, layer="2 Groups") + +msoa_group_labels <- st_read(msoa, layer="1 Group labels") %>% + mutate(just=if_else(LabelPosit=="Left", 0, 1)) +``` + +As with the local authority example, `msoa_groups` and `msoa_la_outlines` geometries provide County- and Local Authority-level grouping/outlines (black in the plot below). `msoa_data` provides the MSOA outlines (in blue). + +```{r} +ggplot()+ + geom_sf(data=background_msoa %>% filter(Name!="Ireland"), aes(geometry=geom))+ + geom_sf(data=msoa_data %>% filter(RegionNation!="Northern Ireland"), + aes(geometry=geom), fill=NA, colour="Blue") + + geom_sf(data=msoa_la_outlines %>% filter(RegionNation!="Northern Ireland"), + aes(geometry=geom), fill=NA, colour="Black") + + geom_sf(data=msoa_groups %>% filter(RegionNation!="Northern Ireland"), + aes(geometry=geom), fill=NA, colour="Black") + + geom_sf_text(data=msoa_group_labels, aes(geometry=geom, label=Group.labe, + hjust=just), size=rel(2.4), colour="Black") + + theme_void() + + theme(plot.title=element_text(face="bold", size=rel(1.5)), + legend.position="top") + + guides(fill = guide_colorbar(title.position = 'top', title.hjust = .5, + barwidth = unit(20, 'lines'), barheight = unit(.5, 'lines'))) + + labs(title="MSOA Hex-Cartogram", + caption="Cartogram by House of Commons Library\nPlot by @tom_rushby") +``` + +