For this project, I wanted to compare how a cartogram compares to a flat chloropleth map when visualizing the UK Brexit vote results. The weight for the distortion is given by total number of registered voters in each local authority (county).
All credit for the instructions and data go to Crime Mapping and Spatial Data Analysis using R by Juanjo Medina and Reka Solymosi.
Code can be found below.
library(readr)
library(tidyverse)
library(janitor)
library(skimr)
library(sf)
library(tmap)
library(tmaptools)
library(DCluster)
library(geodaData)
library(RColorBrewer)
library(hexbin)
library(cartogram)
library(gridExtra)
eu_ref = read_csv("https://raw.githubusercontent.com/maczokni/crime_mapping/master/data/EU-referendum-result-data.csv")
las <- st_read("england_lad_2011_gen.shp")
eu_sf <- left_join(las, eu_ref, by = c("name" = "Area"))
eu_sf = st_set_crs(eu_sf, 27700)
eu_cartogram <- cartogram_cont(eu_sf, "Electorate", itermax = 5)
map = ggplot() +
geom_sf(data = eu_sf, aes(fill = Pct_Leave)) +
theme_void() +
scale_fill_gradientn(colors = brewer.pal(9, "YlGnBu"))
cartogram = ggplot() +
geom_sf(data = eu_cartogram, aes(fill = Pct_Leave)) +
theme_void() +
scale_fill_gradientn(colors = brewer.pal(9, "YlGnBu"))
grid.arrange(map, cartogram, ncol=2)