Skip to content

Instantly share code, notes, and snippets.

@RobertMyles
Last active February 5, 2017 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertMyles/d969a3681bccfc089a202478ff3c1306 to your computer and use it in GitHub Desktop.
Save RobertMyles/d969a3681bccfc089a202478ff3c1306 to your computer and use it in GitHub Desktop.
code for tidying up spatial UK data, for a blog post here:
library(rgdal)
library(maptools)
library(dplyr)
library(ggplot2)
library(readr)
library(httr)
library(XML)
library(ggmap)
library(scales)
brex <- read_csv("https://raw.githubusercontent.com/RobertMyles/Various_R-Scripts/master/data/EU-referendum-result-data.csv")
# Fix up place names:
# you need to have these data files in your working directory:
uk.map <- readOGR(dsn = "/Users/robert/Documents/Papers/FGV Case Studies/Brexit_Case_Study/GBR_adm_shp",
layer = "GBR_adm2", verbose = FALSE)
uk.map@data$NAME_2 <- as.character(uk.map@data$NAME_2)
uk.map@data$NAME_2[56] <- "City of London"
uk.map@data$NAME_2[140] <- "Aberdeen City"
uk.map@data$NAME_2[145] <- "Dundee City"
uk.map@data$NAME_2[150] <- "City of Edinburgh"
uk.map@data$NAME_2[154] <- "Glasgow City"
uk.map@data$NAME_2[159] <- "North Ayrshire"
uk.map@data$NAME_2[162] <- "Perth and Kinross"
uk.map@data$NAME_2[171] <- "Isle of Anglesey"
uk.map@data$NAME_2[188] <- "Rhondda Cynon Taf"
url <- "https://en.wikipedia.org/wiki/List_of_English_districts"
tables <- GET(url)
tables <- readHTMLTable(rawToChar(tables$content))
n.rows <- unlist(lapply(tables, function(t) dim(t)[1]))
districts <- tables[[which.max(n.rows)]]
names(districts) <- c("Name", "Website", "Population2015", "Type",
"CeremonialCounty")
districts$Name <- gsub("&", "and", districts$Name)
districts$Name[133] <- "Kingston upon Hull"
districts$id <- NA
districts$Leave <- 0
districts$Remain <- 0
districts$Valid <- 0
wEngland <- which(uk.map@data$NAME_1 == "England")
for(i in wEngland ) {
if(uk.map$TYPE_2[i] == "Administrative County" | uk.map$TYPE_2[i] == "Metropolitan County" |
uk.map$TYPE_2[i] == "County" | uk.map$TYPE_2[i] == "Metropolitan Borough (city)") {
match <- grep(uk.map$NAME_2[i], districts$CeremonialCounty)
for(j in match) if(is.na(districts$id[j]) ) districts$id[j] <- uk.map$ID_2[i]
} else {
match <- match(uk.map$NAME_2[i], districts$Name)
districts$id[match] <- uk.map$ID_2[i]
}
}
for(i in 1:nrow(districts) ) {
match <- match(districts$Name[i], brex$Area)
if(is.na(match) ) match <- grep(districts$Name[i], brex$Area)
districts$Leave[i] <- brex$Leave[match]
districts$Remain[i] <- brex$Remain[match]
districts$Valid[i] <- brex$Valid_Votes[match]
}
england <- with(districts, data.frame(Name = uk.map@data$NAME_2[wEngland],
Leave = tapply(Leave, id, sum, na.rm = TRUE),
Remain = tapply(Remain, id, sum, na.rm = TRUE),
Valid = tapply(Valid, id, sum, na.rm = TRUE),
Country = "England",
id = uk.map@data$ID_2[wEngland]))
wIreland <- which(uk.map@data$NAME_1 == "Northern Ireland")
ireland <- with(brex, data.frame(Name = uk.map@data$NAME_2[wIreland],
Country = "N. Ireland",
Leave = rep(Leave[grep("N",Region_Code)], each = length(wIreland) ),
Remain = rep(Remain[grep("N",Region_Code)], each = length(wIreland) ),
Valid = rep(Valid_Votes[grep("N",Region_Code)], each = length(wIreland) ),
id = uk.map@data$ID_2[wIreland]))
scotland <- with(brex, data.frame(Name = Area[grep("S",Region_Code)],
Leave = Leave[grep("S",Region_Code)],
Country = "Scotland",
Remain = Remain[grep("S",Region_Code)],
Valid = Valid_Votes[grep("S",Region_Code)],
id = rep(NA,length(grep("S",Region_Code)) ) ) )
for(i in 1:nrow(scotland) ) {
match <- match(scotland$Name[i],uk.map@data$NAME_2)
scotland$id[i] <- uk.map@data$ID_2[match]
}
wales <- with(brex, data.frame(Name = Area[grep("W",Region_Code)],
Leave = Leave[grep("W",Region_Code)],
Country = "Wales",
Remain = Remain[grep("W",Region_Code)],
Valid = Valid_Votes[grep("W",Region_Code)],
id = length(grep("W",Region_Code) ) ) )
for(i in 1:nrow(wales) ) {
match <- match(wales$Name[i],uk.map@data$NAME_2)
wales$id[i] <- uk.map@data$ID_2[match]
}
cities.name2 <- c("Blackburn, UK", "Port Talbot, UK", "Northampton, UK", "London, UK")
cities.coordinates <- geocode(cities.name2, messaging = FALSE)
cities.lon <- cities.coordinates$lon
cities.lat <- cities.coordinates$lat
uk <- rbind(england, ireland, scotland, wales)
uk$pct_Leave <- 100*uk$Leave/uk$Valid
uk$pct_Remain <- 100*uk$Remain/uk$Valid
uk.points <- fortify(uk.map, region = "ID_2")
uk$id <- as.character(uk$id)
uk.plot <- left_join(uk.points,uk)
uk.plot$Import_shock <- NA
One <- c("Cumbria", "Lancashire", "Northamptonshire", "Leicestershire", "Leicester", "Blackburn with Darwen", "Manchester", "Rutland")
Two <- c("Telford and Wrekin", "Derbyshire", "West Midlands", "Stoke-on-Trent", "Shropshire", "Staffordshire", "Nottinghamshire", "Derby", "Nottingham", "West Yorkshire")
Three <- c("Herefordshire", "Worcestershire", "Essex", "Warwickshire", "Southend-on-Sea", "Thurrock", "Havering")
Four <- c("Bridgend", "Isle of Anglesey", "Gwynedd", "Ceredigion", "Carmarthenshire", "Pembrokeshire", "Swansea", "Rhondda Cynon Taf", "Neath Port Talbot", "Caerphilly", "Merthyr Tydfil", "Torfaen", "Somerset", "Dorset", "Isle of Wight", "Hampshire", "Conwy", "Suffolk", "Durham", "Tyne and Wear", "Denbighshire", "Cambridgeshire", "Luton", "Hertfordshire", "Bedfordshire", "Berkshire", "Poole", "Southampton", "Portsmouth", "Redcar and Cleveland", "Stockton-on-Tees", "Darlington", "Hartlepool", "Bornemouth", "Blaenau Gwent")
Five <- c("Northumberland", "Norfolk", "South Yorkshire", "Enfield", "Brent")
Six <- c("South Ayrshire", "North Ayshire", "Dumfries and Galloway", "South Lanarkshire", "North Lanarkshire", "East Ayrshire", "Renfrewshire", "West Dunbartonshire", "East Renfrewshire", "Glasgow City", "East Dunbartonshire", "Inverclyde", "Lincolnshire", "West Sussex", "East Sussex", "Brighton and Hove", "Surrey", "Oxfordshire", "Buckinghamshire", "Milton Keynes", "Peterborough", "Sutton", "Croydon")
Seven <- c("Angus", "Dundee City", "Scottish Borders", "East Lothian", "Midlothian", "Fife", "Perth and Kinross", "City of Edinburgh", "Stirling", "Falkirk", "West Lothian", "Clackmannanshire", "Powys", "Cheshire", "Vale of Glamorgan", "Newport", "Cardiff", "Warrington", "Halton", "Flintshire", "Wrexham", "Monmouthshire", "Bromley", "Hillingdon", "Harrow")
Eight <- c("Devon", "East Riding of Yorkshire", "Kent", "Merseyside", "North East Lincolnshire", "Swindon", "Wiltshire", "Gloucestershire", "South Gloucestershire", "North Somerset", "Torbay", "Bath and North East Somerset", "Bristol", "Kingston upon Hull", "North Lincolnshire", "Medway")
Nine <- c("Highlands", "Argyll and Bute", "North Yorkshire", "Cornwall", "Eilean Siar")
uk.plot1 <- uk.plot %>%
filter(Name != "Shetland Islands") %>%
filter(Country != "N. Ireland") %>%
mutate(Import_shock = if_else(Name %in% One, 1, if_else(Name %in% Two, 2, if_else(Name %in% Three, 3, if_else(Name %in% Four, 4, if_else(Name %in% Five, 5, if_else(Name %in% Six, 6, if_else(Name %in% Seven, 7, if_else(Name %in% Eight, 8, if_else(Name %in% Nine, 9, 9)))))))))) %>%
filter(long > -10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment