Last active
May 21, 2020 21:01
-
-
Save thanhleviet/9d82e5a5bdca47a1142a73db7d3292a9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#Required libraries | |
library(tidyverse) | |
library(rgdal) | |
library(rgeos) | |
library(broom) | |
library(maps) | |
#Postcode spatial data | |
#You need postcode map here https://www.opendoorlogistics.com/wp-content/uploads/Data/UK-postcode-boundaries-Jan-2015.zip | |
england <- readOGR( | |
dsn= "./osm/postcode/Distribution/" , | |
layer="Districts", | |
verbose=FALSE | |
) | |
#Get aggregated data as shown by Nabil | |
samples <- read_delim("sample_postcode.tsv", delim = " ", col_names = F) %>% | |
select(pc = X2, count = X1) | |
england@data<- left_join(england@data, samples, by = c("name" = "pc")) | |
england@data$pid <- getSpPPolygonsIDSlots(england) | |
has_data <- subset(england, !is.na(count)) | |
bbox_has_data <- rgeos::bbox2SP(bbox = bbox(has_data), proj4string = CRS(proj4string(england))) | |
#Clip area of interest | |
aoi <- gIntersection(england,bbox_has_data, byid = T) | |
#Get polygon ID | |
aoi_id <- getSpPPolygonsIDSlots(aoi) | |
#Create polygon dataframe | |
aoi_df <- data.frame(id = aoi_id, row.names = aoi_id) | |
#Create SpatialPolygonDataFrame | |
aoi_spdf <- SpatialPolygonsDataFrame(aoi, aoi_df) | |
aoi_spdf@data <- aoi_spdf@data %>% | |
mutate(id = gsub(" 1", "", id)) | |
#Tidy Spatial data for ggplot | |
new_data_fortify <- broom::tidy(aoi_spdf, region = "id") %>% arrange() | |
#Tidy spatial data has samples | |
tidy_has_data <- fortify(has_data, region="name") %>% | |
arrange() | |
#Categorise data | |
has_data@data$samples <- cut(has_data@data$count, c(0,1,5,10,20,30,40,50,60,70,80,90,10000)) | |
#Create centroid label | |
has_data_map <- map(has_data, plot=FALSE, fill = TRUE) | |
has_data_centroids <- maps:::apply.polygon(has_data_map, maps:::centroid.polygon) | |
has_data_centroids_matrix <- Reduce(rbind, has_data_centroids) | |
dimnames(has_data_centroids_matrix) <- list(gsub("[^,]*,", "", names(has_data_centroids)), | |
c("long", "lat")) | |
postcode_label <- as.data.frame(has_data_centroids_matrix) | |
postcode_label$label <- row.names(postcode_label) %>% gsub(":[0-9]","",.) | |
#Plot map | |
ggplot(tidy_has_data) + | |
geom_map(aes(long, lat, map_id = order), map = new_data_fortify, fill = "grey70", color = "#000000", size = 0.15) + | |
geom_map(aes(long, lat, map_id = id), map = tidy_has_data, fill = "#ffffff", color = "#000000", size = 0.15) + | |
geom_map(data = has_data@data, aes(fill = samples, map_id = name), map = tidy_has_data, color = "white", size = 0.15) + | |
geom_text(data=postcode_label, aes(x = long, y = lat, label=label), size = 2.5, color = "black") + | |
scale_fill_viridis(na.value='#404040', discrete = TRUE, option="viridis") + | |
ggtitle("Samples by post code") + | |
theme(legend.position="right") + | |
theme_void() | |
ggsave("sample_postcode.png", width = 15, height = 15, units = "cm") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment