Created
March 27, 2017 00:33
-
-
Save ashleylester/7d35f63fdacd057c319ac80fedf1ee9c to your computer and use it in GitHub Desktop.
More mapping
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rgdal) | |
library(tidyverse) | |
library(scales) | |
la <- readOGR(paste0("Local_Authority_Districts_December_2015_Super_Generalised_Clipped_", | |
"Boundaries_in_Great_Britain/Local_Authority_Districts_December_2015_Super_Generalised_Clipped_", | |
"Boundaries_in_Great_Britain.shp")) | |
# Change the coordinate reference system - it's a bit easier to work with | |
la <- spTransform(la, CRS("+init=epsg:4326")) | |
# spdf contain @data and @polygons (@ = S4 objects). The rownames() in @data correspond | |
# to the IDs in @polygons (yeah, it's fucked up) | |
la@data$id <- rownames(la@data) | |
# fortify() is being deprecated - use broom::tidy() instead | |
la_df <- fortify(la) | |
la_df <- inner_join(la_df, la@data, by = "id") | |
# Data import and tidying | |
med_house_price <- read_csv("med_house_price.csv") | |
# Transform to long format | |
med_house_price <- gather(med_house_price, period, house_price, | |
-`Region code`, -`Region name`, -`Local authority code`, -`Local authority name`) | |
# Change to years | |
med_house_price$period <- substr(med_house_price$period, 4, 8) | |
med_gross_income <- read_csv("med_gross_income.csv") | |
med_gross_income <- gather(med_gross_income, period, median_salary, | |
-`Region code`, -`Region name`, -`Local authority code`, -`Local authority name`) | |
# Reformat totally fucked salary format | |
med_gross_income <- med_gross_income %>% mutate(median_salary = median_salary %>% map_int(function(x) { | |
out <- gsub(",", "", x) | |
out <- gsub(":", "", out) | |
as.integer(out) | |
})) | |
price_salary_ratio <- read_csv("price_salary_ratio.csv") | |
price_salary_ratio <- gather(price_salary_ratio, period, price_salary_ratio, | |
-`Region code`, -`Region name`, -`Local authority code`, -`Local authority name`) | |
# Reformat totally fucked price salary ratio format | |
price_salary_ratio <- price_salary_ratio %>% mutate(price_salary_ratio = price_salary_ratio %>% map_dbl(function(x) { | |
out <- gsub(",", "", x) | |
out <- gsub(":", "", out) | |
out <- as.double(out) | |
out <- ifelse(out >= 15, 15, out) | |
})) | |
price_salary_ratio <- inner_join(med_gross_income, price_salary_ratio) | |
price_salary_ratio <- inner_join(med_house_price, price_salary_ratio) | |
price_salary_ratio$authority <- price_salary_ratio$`Local authority code` | |
la_df$lad15cd <- as.character(la_df$lad15cd) | |
# Combine our data and spatial data frame | |
la_df <- inner_join(la_df, price_salary_ratio, by = c("lad15cd" = "authority")) | |
ggplot(la_df, aes(long, lat)) + | |
geom_polygon(aes(group = group, fill = price_salary_ratio), color = "white", size = 0.2) + | |
coord_quickmap() + | |
scale_fill_gradient2(low = "blue4", mid = "white", high = "red4", | |
midpoint = quantile(price_salary_ratio$price_salary_ratio, na.rm = TRUE, | |
probs = 0.5)) + | |
theme_bw() + | |
labs(title = "Housing Affordability - England & Wales 2016", | |
subtitle = "The ratio of median house price and yearly gross earnings per household", | |
fill = "Price / Salary \nRatio\n") + | |
theme(panel.background = element_rect(fill = 'grey99', colour = 'grey99'), | |
axis.title.x = element_blank(), | |
axis.title.y = element_blank()) | |
la_df_london <- filter(la_df, `Region name` == "London") | |
ggplot(la_df_london, aes(long, lat)) + | |
geom_polygon(aes(group = group, fill = price_salary_ratio), color = "grey10", size = 0.2) + | |
coord_quickmap() + | |
scale_fill_gradient2(low = "blue4", mid = "white", high = "red4", | |
midpoint = quantile(price_salary_ratio$price_salary_ratio, na.rm = TRUE, | |
probs = 0.5)) + | |
theme_bw() + | |
labs(title = "Housing Affordability - England & Wales 2016", | |
subtitle = "The ratio of median house price and yearly gross earnings per household", | |
fill = "Price / Salary \nRatio\n") + | |
theme(panel.background = element_rect(fill = 'grey99'), | |
axis.title.x = element_blank(), | |
axis.title.y = element_blank(), | |
axis.text.x = element_blank(), | |
axis.text.y = element_blank()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment