Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. library(rgdal)
  2. library(tidyverse)
  3. library(scales)
  4.  
  5. la <- readOGR(paste0("Local_Authority_Districts_December_2015_Super_Generalised_Clipped_",
  6. "Boundaries_in_Great_Britain/Local_Authority_Districts_December_2015_Super_Generalised_Clipped_",
  7. "Boundaries_in_Great_Britain.shp"))
  8.  
  9. # Change the coordinate reference system - it's a bit easier to work with
  10. la <- spTransform(la, CRS("+init=epsg:4326"))
  11.  
  12. # spdf contain @data and @polygons (@ = S4 objects). The rownames() in @data correspond
  13. # to the IDs in @polygons (yeah, it's fucked up)
  14. la@data$id <- rownames(la@data)
  15.  
  16. # fortify() is being deprecated - use broom::tidy() instead
  17. la_df <- fortify(la)
  18. la_df <- inner_join(la_df, la@data, by = "id")
  19.  
  20. # Data import and tidying
  21. med_house_price <- read_csv("med_house_price.csv")
  22.  
  23. # Transform to long format
  24. med_house_price <- gather(med_house_price, period, house_price,
  25. -`Region code`, -`Region name`, -`Local authority code`, -`Local authority name`)
  26. # Change to years
  27. med_house_price$period <- substr(med_house_price$period, 4, 8)
  28.  
  29. med_gross_income <- read_csv("med_gross_income.csv")
  30. med_gross_income <- gather(med_gross_income, period, median_salary,
  31. -`Region code`, -`Region name`, -`Local authority code`, -`Local authority name`)
  32.  
  33. # Reformat totally fucked salary format
  34. med_gross_income <- med_gross_income %>% mutate(median_salary = median_salary %>% map_int(function(x) {
  35. out <- gsub(",", "", x)
  36. out <- gsub(":", "", out)
  37. as.integer(out)
  38. }))
  39.  
  40. price_salary_ratio <- read_csv("price_salary_ratio.csv")
  41. price_salary_ratio <- gather(price_salary_ratio, period, price_salary_ratio,
  42. -`Region code`, -`Region name`, -`Local authority code`, -`Local authority name`)
  43.  
  44. # Reformat totally fucked price salary ratio format
  45. price_salary_ratio <- price_salary_ratio %>% mutate(price_salary_ratio = price_salary_ratio %>% map_dbl(function(x) {
  46. out <- gsub(",", "", x)
  47. out <- gsub(":", "", out)
  48. out <- as.double(out)
  49. out <- ifelse(out >= 15, 15, out)
  50. }))
  51.  
  52. price_salary_ratio <- inner_join(med_gross_income, price_salary_ratio)
  53. price_salary_ratio <- inner_join(med_house_price, price_salary_ratio)
  54. price_salary_ratio$authority <- price_salary_ratio$`Local authority code`
  55. la_df$lad15cd <- as.character(la_df$lad15cd)
  56.  
  57. # Combine our data and spatial data frame
  58. la_df <- inner_join(la_df, price_salary_ratio, by = c("lad15cd" = "authority"))
  59.  
  60. ggplot(la_df, aes(long, lat)) +
  61. geom_polygon(aes(group = group, fill = price_salary_ratio), color = "white", size = 0.2) +
  62. coord_quickmap() +
  63. scale_fill_gradient2(low = "blue4", mid = "white", high = "red4",
  64. midpoint = quantile(price_salary_ratio$price_salary_ratio, na.rm = TRUE,
  65. probs = 0.5)) +
  66. theme_bw() +
  67. labs(title = "Housing Affordability - England & Wales 2016",
  68. subtitle = "The ratio of median house price and yearly gross earnings per household",
  69. fill = "Price / Salary \nRatio\n") +
  70. theme(panel.background = element_rect(fill = 'grey99', colour = 'grey99'),
  71. axis.title.x = element_blank(),
  72. axis.title.y = element_blank())
  73.  
  74. la_df_london <- filter(la_df, `Region name` == "London")
  75. ggplot(la_df_london, aes(long, lat)) +
  76. geom_polygon(aes(group = group, fill = price_salary_ratio), color = "grey10", size = 0.2) +
  77. coord_quickmap() +
  78. scale_fill_gradient2(low = "blue4", mid = "white", high = "red4",
  79. midpoint = quantile(price_salary_ratio$price_salary_ratio, na.rm = TRUE,
  80. probs = 0.5)) +
  81. theme_bw() +
  82. labs(title = "Housing Affordability - England & Wales 2016",
  83. subtitle = "The ratio of median house price and yearly gross earnings per household",
  84. fill = "Price / Salary \nRatio\n") +
  85. theme(panel.background = element_rect(fill = 'grey99'),
  86. axis.title.x = element_blank(),
  87. axis.title.y = element_blank(),
  88. axis.text.x = element_blank(),
  89. axis.text.y = element_blank())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement