diff --git a/code/data.R b/code/01-data.R similarity index 87% rename from code/data.R rename to code/01-data.R index 38db698..45f14ba 100644 --- a/code/data.R +++ b/code/01-data.R @@ -1,25 +1,52 @@ # --------------------------------------------------------------------------------------- # DEGURBA POSTCODE AREAS -# Sascha Göbel -# Script for packages +# Sascha Goebel +# Script for downloading and processing data # June 2021 # --------------------------------------------------------------------------------------- -# imports ------------------------------------------------------------------------------- -cat(underline("IMPORTS")," - '' +# downloads and imports ----------------------------------------------------------------- +cat(underline("DOWNLOADS AND IMPORTS")," + './data/cntr_polygons/NUTS_RG_01M_2021_3035_LEVL_0.shp' + './data/pop_grid/JRC_1K_POP_2018.tif' + './data/fua_polygons/URAU_RG_01M_2018_3035_FUA.shp' + './data/postcode_areas/PLZO_PLZ.shp' + './data/postcode_areas/OSM_PLZ_072019.shp' + './data/postcode_areas/codigos_postales.shp' + './data/postcode_areas/codes_postaux_region.shp' + './data/postcode_areas/Sectors.shp' ") # exports ------------------------------------------------------------------------------- cat(underline("EXPORTS")," - '' + './data/cntr_polygons/ch_cntr_polygon.shp' + './data/cntr_polygons/de_cntr_polygon.shp' + './data/cntr_polygons/es_cntr_polygon.shp' + './data/cntr_polygons/fr_cntr_polygon.shp' + './data/cntr_polygons/uk_cntr_polygon.shp' + './data/pop_grid/ch_pop_grid.tif' + './data/pop_grid/de_pop_grid.tif' + './data/pop_grid/es_pop_grid.tif' + './data/pop_grid/fr_pop_grid.tif' + './data/pop_grid/uk_pop_grid.tif' + './data/fua_polygons/ch_fua_polygons.shp' + './data/fua_polygons/de_fua_polygons.shp' + './data/fua_polygons/es_fua_polygons.shp' + './data/fua_polygons/fr_fua_polygons.shp' + './data/fua_polygons/uk_fua_polygons.shp' + './data/postcode_areas/ch_postcode_polygons.shp' + './data/postcode_areas/de_postcode_polygons.shp' + './data/postcode_areas/es_postcode_polygons.shp' + './data/postcode_areas/fr_postcode_polygons.shp' + './data/postcode_areas/uk_postcode_polygons.shp' ") # content ------------------------------------------------------------------------------- cat(underline("CONTENT")," - DOWNLOAD POPULATION GRID, BOUNDARY, FUNCTIONAL URBAN AREA, AND POSTCODE DATA - PROCESS BOUNDARY, POPULATION GRID, FUNCTIONAL URBAN AREA, AND POSTCODE DATA + Line 53 - PREPARATIONS + Line 73 - DOWNLOAD POPULATION GRID, BOUNDARY, FUNCTIONAL URBAN AREA, AND POSTCODE DATA + Line 189 - PROCESS BOUNDARY, POPULATION GRID, FUNCTIONAL URBAN AREA, AND POSTCODE DATA ") @@ -29,7 +56,7 @@ cat(underline("CONTENT")," rm(list=ls(all=TRUE)) # set working directory ----------------------------------------------------------------- -setwd("D://projects/rude/") +setwd("degurba-postcode-areas") # install and load packages ------------------------------------------------------------- source("./code/packages.R") diff --git a/code/degurba.R b/code/02-degurba.R similarity index 77% rename from code/degurba.R rename to code/02-degurba.R index a0bc112..6664793 100644 --- a/code/degurba.R +++ b/code/02-degurba.R @@ -1,6 +1,6 @@ # --------------------------------------------------------------------------------------- # DEGURBA POSTCODE AREAS -# Sascha Göbel +# Sascha Goebel # Script for applying degurba to grid cells and postcode areas # June 2021 # --------------------------------------------------------------------------------------- @@ -8,19 +8,39 @@ # imports ------------------------------------------------------------------------------- cat(underline("IMPORTS")," - '' + './data/pop_grid/ch_pop_grid.tif' + './data/pop_grid/de_pop_grid.tif' + './data/pop_grid/es_pop_grid.tif' + './data/pop_grid/fr_pop_grid.tif' + './data/pop_grid/uk_pop_grid.tif' + './data/fua_polygons/ch_fua_polygons.shp' + './data/fua_polygons/de_fua_polygons.shp' + './data/fua_polygons/es_fua_polygons.shp' + './data/fua_polygons/fr_fua_polygons.shp' + './data/fua_polygons/uk_fua_polygons.shp' + './data/postcode_areas/ch_postcode_polygons.shp' + './data/postcode_areas/de_postcode_polygons.shp' + './data/postcode_areas/es_postcode_polygons.shp' + './data/postcode_areas/fr_postcode_polygons.shp' + './data/postcode_areas/uk_postcode_polygons.shp' ") # exports ------------------------------------------------------------------------------- cat(underline("EXPORTS")," - '' + './data/grid_classifications' + './data/postcode_classifications' + './data/postcode_degurba_lists/ch_postcode_degurba' + './data/postcode_degurba_lists/de_postcode_degurba' + './data/postcode_degurba_lists/es_postcode_degurba' + './data/postcode_degurba_lists/fr_postcode_degurba' + './data/postcode_degurba_lists/uk_postcode_degurba' ") # content ------------------------------------------------------------------------------- cat(underline("CONTENT")," - PREPARATIONS - CLASSIFY GRID CELLS - CLASSIFY POSTCODE AREAS + Line 47 - PREPARATIONS + Line 60 - CLASSIFY GRID CELLS + Line 88 - CLASSIFY POSTCODE AREAS ") @@ -30,14 +50,13 @@ cat(underline("CONTENT")," rm(list=ls(all=TRUE)) # set working directory ----------------------------------------------------------------- -setwd("D://projects/rude/") +setwd("degurba-postcode-areas") # install and load packages ------------------------------------------------------------- source("./code/packages.R") source("./code/functions.R") - #### CLASSIFY GRID CELLS ================================================================ # import country-specific population grids ---------------------------------------------- @@ -63,17 +82,7 @@ grid_classifications <- grid_classifications %>% purrr::map2(.x = grid_classifications, .f = raster::addLayer) # save grid classifications ------------------------------------------------------------- -path <- file.path("./data","grid_classifications") -if (!dir.exists(path)) { - path %>% - dir.create(recursive = TRUE) -} -file_names <- paste0(c("ch", "de", "es", "fr", "uk"), "_grid_classifications.grd") -if (any(!file.exists(paste0(path, "/", file_names)))) { - grid_classifications %>% - purrr::map2(.y = paste0(path, "/", file_names), - .f = raster::writeRaster) -} +saveRDS(grid_classifications, "./data/grid_classifications") #### CLASSIFY POSTCODE AREAS ============================================================ @@ -92,17 +101,7 @@ postcode_classifications <- grid_classifications %>% purrr::map2(.y = postcode_polygons, .f = cbind) # save postcode classifications --------------------------------------------------------- -path <- file.path("./data","postcode_classifications") -if (!dir.exists(path)) { - path %>% - dir.create(recursive = TRUE) -} -file_names <- paste0(c("ch", "de", "es", "fr", "uk"), "_postcode_classifications") -if (any(!file.exists(paste0(path, "/", file_names)))) { - postcode_classifications %>% - purrr::map2(.y = paste0(path, "/", file_names), - .f = saveRDS) -} +saveRDS(postcode_classifications, "./data/postcode_classifications") # extract classified postcode lists ----------------------------------------------------- ch_postcode_degurba <- postcode_classifications[[1]] %>% diff --git a/code/03-maps.R b/code/03-maps.R new file mode 100644 index 0000000..26aa191 --- /dev/null +++ b/code/03-maps.R @@ -0,0 +1,776 @@ +# --------------------------------------------------------------------------------------- +# DEGURBA POSTCODE AREAS +# Sascha Goebel +# Script for maps +# June 2021 +# --------------------------------------------------------------------------------------- + + +# imports ------------------------------------------------------------------------------- +cat(underline("IMPORTS")," + './data/cntr_polygons/ch_cntr_polygon.shp' + './data/cntr_polygons/de_cntr_polygon.shp' + './data/cntr_polygons/es_cntr_polygon.shp' + './data/cntr_polygons/fr_cntr_polygon.shp' + './data/cntr_polygons/uk_cntr_polygon.shp' + './data/grid_classifications' + './data/postcode_classifications' + ") + +# exports ------------------------------------------------------------------------------- +cat(underline("EXPORTS")," + './maps/pop_grid_map.png' + './maps/degurba_l1_grid_map.png' + './maps/degurba_l1_grid_fua_map.png' + './maps/degurba_l2_grid_map.png' + './maps/degurba_l2_grid_fua_map.png' + './maps/degurba_l1_postcode_map.png' + './maps/degurba_l1_postcode_fua_map.png' + './maps/ch_degurba_l1_postcode_map.png' + './maps/de_degurba_l1_postcode_map.png' + './maps/es_degurba_l1_postcode_map.png' + './maps/fr_degurba_l1_postcode_map.png' + './maps/uk_degurba_l1_postcode_map.png' + './maps/ch_degurba_l1_postcode_fua_map.png' + './maps/de_degurba_l1_postcode_fua_map.png' + './maps/es_degurba_l1_postcode_fua_map.png' + './maps/fr_degurba_l1_postcode_fua_map.png' + './maps/uk_degurba_l1_postcode_fua_map.png' + './maps/degurba_l2_postcode_map.png' + './maps/degurba_l2_postcode_fua_map.png' + './maps/ch_degurba_l2_postcode_map.png' + './maps/de_degurba_l2_postcode_map.png' + './maps/es_degurba_l2_postcode_map.png' + './maps/fr_degurba_l2_postcode_map.png' + './maps/uk_degurba_l2_postcode_map.png' + './maps/ch_degurba_l2_postcode_fua_map.png' + './maps/de_degurba_l2_postcode_fua_map.png' + './maps/es_degurba_l2_postcode_fua_map.png' + './maps/fr_degurba_l2_postcode_fua_map.png' + './maps/uk_degurba_l2_postcode_fua_map.png' + ") + +# content ------------------------------------------------------------------------------- +cat(underline("CONTENT")," + Line 65 - PREPARATIONS + Line 83 - SPECIFY REGIONS + Line 145 - POPULATION DENSITY GRID MAP + Line 176 - DEGURBA LEVEL 1 GRID MAPS + Line 238 - DEGURBA LEVEL 2 GRID MAPS + Line 292 - DEGURBA LEVEL 1 POSTCODE MAPS + Line 544 - DEGURBA LEVEL 2 POSTCODE MAPS + ") + + +#### PREPARATIONS ======================================================================= + +# clear workspace ----------------------------------------------------------------------- +rm(list=ls(all=TRUE)) + +# set working directory ----------------------------------------------------------------- +setwd("degurba-postcode-areas") + +# install and load packages ------------------------------------------------------------- +source("./code/packages.R") +source("./code/functions.R") + +# get fonts ----------------------------------------------------------------------------- +# download 'new computer modern' font and convert to true type +# font_import() # adjust path if fonts are not imported +# loadfonts(device = "win") + + +#### SPECIFY REGIONS ==================================================================== + +# import boundary polygons for countries ------------------------------------------------ +ch_cntr_polygon <- sf::st_read("data/cntr_polygons/ch_cntr_polygon.shp") +de_cntr_polygon <- sf::st_read("data/cntr_polygons/de_cntr_polygon.shp") +es_cntr_polygon <- sf::st_read("data/cntr_polygons/es_cntr_polygon.shp") %>% + dplyr::filter(CNTR_CODE == "ES") +fr_cntr_polygon <- sf::st_read("data/cntr_polygons/fr_cntr_polygon.shp") +uk_cntr_polygon <- sf::st_read("data/cntr_polygons/uk_cntr_polygon.shp") +# remove northern ireland (no postcode areas available) +uk_cntr_polygon <- st_polygon(uk_cntr_polygon$geometry[[1]][[200]]) %>% + st_combine() %>% + st_union %>% + st_set_crs(value = 3035) %>% + st_difference(uk_cntr_polygon, .) + +# set location coordinates for countries and example region ----------------------------- +ch_region_locations <- maps::world.cities %>% + filter(country.etc == "Switzerland" & pop >= 60000) %>% + dplyr::select(city = name, lat = lat, lng = long, pop) %>% + st_as_sf(coords = c('lng', 'lat')) %>% + st_set_crs(value = 4326) %>% + st_transform(3035) +de_region_locations <- maps::world.cities %>% + filter(country.etc == "Germany" & pop >= 300000) %>% + dplyr::select(city = name, lat = lat, lng = long, pop) %>% + st_as_sf(coords = c('lng', 'lat')) %>% + st_set_crs(value = 4326) %>% + st_transform(3035) +es_region_locations <- maps::world.cities %>% + filter(country.etc == "Spain" & pop >= 200000) %>% + dplyr::select(city = name, lat = lat, lng = long, pop) %>% + st_as_sf(coords = c('lng', 'lat')) %>% + st_set_crs(value = 4326) %>% + st_transform(3035) +fr_region_locations <- maps::world.cities %>% + filter(country.etc == "France" & pop >= 150000) %>% + dplyr::select(city = name, lat = lat, lng = long, pop) %>% + st_as_sf(coords = c('lng', 'lat')) %>% + st_set_crs(value = 4326) %>% + st_transform(3035) +uk_region_locations <- maps::world.cities %>% + filter(country.etc == "UK" & pop >= 300000) %>% + dplyr::select(city = name, lat = lat, lng = long, pop) %>% + st_as_sf(coords = c('lng', 'lat')) %>% + st_set_crs(value = 4326) %>% + st_transform(3035) +de_region_locations_ex <- maps::world.cities %>% + filter(country.etc == "Germany" & pop >= 35000) %>% + dplyr::select(city = name, lat = lat, lng = long, pop) %>% + st_as_sf(coords = c('lng', 'lat')) %>% + st_set_crs(value = 4326) %>% + st_transform(3035) + +# specify bounding box for example region ----------------------------------------------- +de_region <- sf::st_bbox(de_cntr_polygon) +de_region <- c(de_region[1]+110000, + de_region[2]+200000, + de_region[3]-390000, + de_region[4]-590000) + + +#### POPULATION DENSITY GRID MAP ======================================================== + +# import population grid and crop to country boundary ----------------------------------- +de_pop_grid <- readRDS("./data/grid_classifications")[[2]]$pop1sqkm +crs(de_pop_grid) <- "EPSG:3035" +de_pop_grid <- de_pop_grid %>% + raster::crop(de_cntr_polygon) %>% + raster::mask(de_cntr_polygon) + +# build map for example region ---------------------------------------------------------- +pop_grid_map <- tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_pop_grid, raster.downsample = FALSE) + + tm_raster(breaks = c(1, 150, 300, 1500, 5000, 35000), title = "Population density \n(1 sq km)", + palette = "YlOrRd") + + tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = pop_grid_map, filename = "./maps/pop_grid_map.png", dpi = 300) + + +#### DEGURBA LEVEL 1 GRID MAPS ========================================================== + +# import grid-level classification and fua and crop to boundary ------------------------- +de_degurba_l1_grid <- readRDS("./data/grid_classifications")[[2]]$classification_l1 +crs(de_degurba_l1_grid) <- "EPSG:3035" +de_degurba_l1_grid <- de_degurba_l1_grid %>% + raster::crop(de_cntr_polygon) %>% + raster::mask(de_cntr_polygon) +de_degurba_fua_grid <- readRDS("./data/grid_classifications")[[2]]$fua +crs(de_degurba_fua_grid) <- "EPSG:3035" +de_degurba_fua_grid <- de_degurba_fua_grid %>% + raster::crop(de_cntr_polygon) %>% + raster::mask(de_cntr_polygon) +de_degurba_fua_grid <- ratify(de_degurba_fua_grid$fua) +values(de_degurba_fua_grid) <- ifelse(de_degurba_fua_grid$fua[] == 1, "TRUE", + NA) %>% + factor(levels = c("TRUE")) + +# build map for example region without functional urban areas --------------------------- +degurba_l1_grid_map <- tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_degurba_l1_grid$classification_l1, raster.downsample = FALSE) + + tm_raster(title = "DEGURBA\nLevel 1", palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l1_grid_map, filename = "./maps/degurba_l1_grid_map.png", dpi = 300) + +# build map for example region with functional urban areas ------------------------------ +degurba_l1_grid_fua_map <- tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_degurba_l1_grid$classification_l1, raster.downsample = FALSE) + + tm_raster(title = "DEGURBA\nLevel 1", palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(de_degurba_fua_grid$fua, raster.downsample = FALSE) + + tm_raster(palette = "red", colorNA = NULL, showNA = FALSE, alpha = 0.25, + title = "Functional urban area") + + tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l1_grid_fua_map, filename = "./maps/degurba_l1_grid_fua_map.png", dpi = 300) + + +#### DEGURBA LEVEL 2 GRID MAPS ========================================================== + +# import grid-level classification and fua grid and crop to boundary -------------------- +de_degurba_l2_grid <- readRDS("./data/grid_classifications")[[2]]$classification_l2 +crs(de_degurba_l2_grid) <- "EPSG:3035" +de_degurba_l2_grid <- de_degurba_l2_grid %>% + raster::crop(de_cntr_polygon) %>% + raster::mask(de_cntr_polygon) + +# build map for example region without functional urban areas --------------------------- +degurba_l2_grid_map <- tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_degurba_l2_grid$classification_l2, raster.downsample = FALSE) + + tm_raster(title = "DEGURBA\nLevel 2", palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l2_grid_map, filename = "./maps/degurba_l2_grid_map.png", dpi = 300) + +# build map for example region with functional urban areas ------------------------------ +degurba_l2_grid_fua_map <- tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_degurba_l2_grid$classification_l2, raster.downsample = FALSE) + + tm_raster(title = "DEGURBA\nLevel 2", palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(de_degurba_fua_grid$fua, raster.downsample = FALSE) + + tm_raster(palette = "red", colorNA = NULL, showNA = FALSE, alpha = 0.25, + title = "Functional urban area") + + tm_shape(de_cntr_polygon, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +degurba_l2_grid_fua_map +tmap_save(tm = degurba_l2_grid_fua_map, filename = "./maps/degurba_l2_grid_fua_map.png", dpi = 300) + + +#### DEGURBA LEVEL 1 POSTCODE MAPS ====================================================== + +# import postcode-level classification for countries ------------------------------------ +ch_degurba_postcode <- readRDS("./data/postcode_classifications")[[1]] %>% + sf::st_as_sf() %>% + dplyr::select(degurba_l1, degurba_l2, fua) +de_degurba_postcode <- readRDS("./data/postcode_classifications")[[2]] %>% + sf::st_as_sf() %>% + dplyr::select(degurba_l1, degurba_l2, fua) +es_degurba_postcode <- readRDS("./data/postcode_classifications")[[3]] %>% + sf::st_as_sf() %>% + dplyr::select(degurba_l1, degurba_l2, fua) +fr_degurba_postcode <- readRDS("./data/postcode_classifications")[[4]] %>% + sf::st_as_sf() %>% + dplyr::select(degurba_l1, degurba_l2, fua) +uk_degurba_postcode <- readRDS("./data/postcode_classifications")[[5]] %>% + sf::st_as_sf() %>% + dplyr::select(degurba_l1, degurba_l2, fua) + +# build map for example region without functional urban areas --------------------------- +degurba_l1_postcode_map <- tm_shape(de_degurba_postcode, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l1_postcode_map, filename = "./maps/degurba_l1_postcode_map.png", dpi = 300) + +# build map for example region with functional urban areas ------------------------------ +degurba_l1_postcode_fua_map <- tm_shape(de_degurba_postcode, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(de_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l1_postcode_fua_map, filename = "./maps/degurba_l1_postcode_fua_map.png", dpi = 300) + +# build map for all countries without functional urban areas ---------------------------- +ch_degurba_l1_postcode_map <- tm_shape(ch_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(ch_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(ch_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = ch_degurba_l1_postcode_map, filename = "./maps/ch_degurba_l1_postcode_map.png", dpi = 300) + +de_degurba_l1_postcode_map <- tm_shape(de_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(de_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = de_degurba_l1_postcode_map, filename = "./maps/de_degurba_l1_postcode_map.png", dpi = 300) + +es_degurba_l1_postcode_map <- tm_shape(es_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(es_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(es_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("right", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = es_degurba_l1_postcode_map, filename = "./maps/es_degurba_l1_postcode_map.png", dpi = 300) + +fr_degurba_l1_postcode_map <- tm_shape(fr_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(fr_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(fr_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = fr_degurba_l1_postcode_map, filename = "./maps/fr_degurba_l1_postcode_map.png", dpi = 300) + +uk_degurba_l1_postcode_map <- tm_shape(uk_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(uk_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(uk_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, xmod = -0.5, ymod = 0.6, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.width = 1, + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = uk_degurba_l1_postcode_map, filename = "./maps/uk_degurba_l1_postcode_map.png", dpi = 300) + +# build map for all countries with functional urban areas ------------------------------- +ch_degurba_l1_postcode_fua_map <- tm_shape(ch_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(ch_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(ch_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(ch_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = ch_degurba_l1_postcode_fua_map, filename = "./maps/ch_degurba_l1_postcode_fua_map.png", dpi = 300) + +de_degurba_l1_postcode_fua_map <- tm_shape(de_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(de_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(de_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = de_degurba_l1_postcode_fua_map, filename = "./maps/de_degurba_l1_postcode_fua_map.png", dpi = 300) + +es_degurba_l1_postcode_fua_map <- tm_shape(es_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(es_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(es_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(es_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("right", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = es_degurba_l1_postcode_fua_map, filename = "./maps/es_degurba_l1_postcode_fua_map.png", dpi = 300) + +fr_degurba_l1_postcode_fua_map <- tm_shape(fr_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(fr_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(fr_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(fr_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = fr_degurba_l1_postcode_fua_map, filename = "./maps/fr_degurba_l1_postcode_fua_map.png", dpi = 300) + +uk_degurba_l1_postcode_fua_map <- tm_shape(uk_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l1", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 1", + palette = c("#ee0300", "#ffae00", "#72b872")) + + tm_shape(uk_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(uk_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(uk_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, xmod = -0.5, ymod = 0.6, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.width = 1, + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = uk_degurba_l1_postcode_fua_map, filename = "./maps/uk_degurba_l1_postcode_fua_map.png", dpi = 300) + + +#### DEGURBA LEVEL 2 POSTCODE MAP ======================================================= + +# build map for example region without functional urban areas --------------------------- +degurba_l2_postcode_map <- tm_shape(de_degurba_postcode, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l2_postcode_map, filename = "./maps/degurba_l2_postcode_map.png", dpi = 300) + +# build map for example region with functional urban areas ------------------------------ +degurba_l2_postcode_fua_map <- tm_shape(de_degurba_postcode, bbox = de_region) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(de_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2) + + tm_shape(de_region_locations_ex) + + tm_dots(col = "black", alpha = 0.5, size = 0.25) + + tm_text(text = "city", col = "black", fontface = "bold", size = 1.2, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("LEFT", "TOP"), + legend.title.fontface = "bold", + legend.text.size = 1, + legend.title.size = 1.5, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.bg.color = "white", + legend.bg.alpha = 0.75) +tmap_save(tm = degurba_l2_postcode_fua_map, filename = "./maps/degurba_l2_postcode_fua_map.png", dpi = 300) + +# build map for all countries without functional urban areas ---------------------------- +ch_degurba_l2_postcode_map <- tm_shape(ch_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(ch_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(ch_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = ch_degurba_l2_postcode_map, filename = "./maps/ch_degurba_l2_postcode_map.png", dpi = 300) + +de_degurba_l2_postcode_map <- tm_shape(de_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(de_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = de_degurba_l2_postcode_map, filename = "./maps/de_degurba_l2_postcode_map.png", dpi = 300) + +es_degurba_l2_postcode_map <- tm_shape(es_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(es_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(es_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("right", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = es_degurba_l2_postcode_map, filename = "./maps/es_degurba_l2_postcode_map.png", dpi = 300) + +fr_degurba_l2_postcode_map <- tm_shape(fr_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(fr_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(fr_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = fr_degurba_l2_postcode_map, filename = "./maps/fr_degurba_l2_postcode_map.png", dpi = 300) + +uk_degurba_l2_postcode_map <- tm_shape(uk_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(uk_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(uk_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, xmod = -0.5, ymod = 0.6, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.width = 1, + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = uk_degurba_l2_postcode_map, filename = "./maps/uk_degurba_l2_postcode_map.png", dpi = 300) + +# build map for all countries with functional urban areas ------------------------------- +ch_degurba_l2_postcode_fua_map <- tm_shape(ch_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(ch_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(ch_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(ch_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = ch_degurba_l2_postcode_fua_map, filename = "./maps/ch_degurba_l2_postcode_fua_map.png", dpi = 300) + +de_degurba_l2_postcode_fua_map <- tm_shape(de_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(de_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(de_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(de_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = de_degurba_l2_postcode_fua_map, filename = "./maps/de_degurba_l2_postcode_fua_map.png", dpi = 300) + +es_degurba_l2_postcode_fua_map <- tm_shape(es_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(es_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(es_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(es_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("right", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = es_degurba_l2_postcode_fua_map, filename = "./maps/es_degurba_l2_postcode_fua_map.png", dpi = 300) + +fr_degurba_l2_postcode_fua_map <- tm_shape(fr_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(fr_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(fr_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(fr_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, auto.placement = TRUE, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "bottom"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = fr_degurba_l2_postcode_fua_map, filename = "./maps/fr_degurba_l2_postcode_fua_map.png", dpi = 300) + +uk_degurba_l2_postcode_fua_map <- tm_shape(uk_degurba_postcode) + + tm_borders(lwd = 0.2) + + tm_fill(col = "degurba_l2", showNA = FALSE, colorNA = NULL, title = "DEGURBA\nLevel 2", + palette = c("#ee0300", "#632f2f", "#a1705a", "#ffae00", "#356335", "#72b872", "#c6deb1")) + + tm_shape(uk_degurba_postcode) + + tm_fill(col = "fua", showNA = FALSE, colorNA = NULL, title = "Functional urban area", + palette = "red", labels = "TRUE", alpha = 0.25) + + tm_shape(uk_cntr_polygon) + + tm_borders(lwd = 0.2, col = "black") + + tm_shape(uk_region_locations) + + tm_dots(col = "black", alpha = 0.25, size = 0.25) + + tm_text(text = "city", fontface = "bold",col = "white", size = 0.8, xmod = -0.5, ymod = 0.6, fontfamily = "NewComputerModern10") + + tm_layout(legend.position = c("left", "top"), + legend.text.size = 0.8, + legend.title.fontfamily = "NewComputerModern10", + legend.text.fontfamily = "NewComputerModern10", + legend.width = 1, + inner.margins = c(0.05, 0.05, 0.05, 0.05), + bg.color = "gray") +tmap_save(tm = uk_degurba_l2_postcode_fua_map, filename = "./maps/uk_degurba_l2_postcode_fua_map.png", dpi = 300) diff --git a/code/functions.R b/code/functions.R index 7a3f165..51b6bc6 100644 --- a/code/functions.R +++ b/code/functions.R @@ -1,6 +1,6 @@ # --------------------------------------------------------------------------------------- # DEGURBA POSTCODE AREAS -# Sascha Göbel +# Sascha Goebel # Script for functions # June 2021 # --------------------------------------------------------------------------------------- @@ -8,15 +8,15 @@ # content ------------------------------------------------------------------------------- cat(underline("FUNCTIONS")," - drop_clusters - get_adjacent_cells - get_contiguous_cells - majority_rule - get_grid_classification_l1 - get_grid_classification_l2 - get_fua - get_spatial_classification_l1 - get_spatial_classification_l2 + Line 23 - drop_clusters + Line 33 - get_adjacent_cells + Line 53 - get_contiguous_cells + Line 76 - majority_rule + Line 103 - get_grid_classification_l1 + Line 198 - get_grid_classification_l2 + Line 319 - get_fua + Line 349 - get_spatial_classification_l1 + Line 400 - get_spatial_classification_l2 ") @@ -316,7 +316,7 @@ get_grid_classification_l2 <- function(pop_grid, uninhabited_na = TRUE) { } -#### get_fua_layer ====================================================================== +#### get_fua ============================================================================ get_fua <- function(pop_grid, fua_polygon) { @@ -376,10 +376,11 @@ get_spatial_classification_l1 <- function(grid_classification, polygons, fua = F rural_grid_pop = sum(pop1sqkm[rural_grid == 1]*coverage_fraction[rural_grid == 1], na.rm = TRUE), fua_pop = eval(fua_expr), fua_metro_pop = eval(fua_metro_expr)) %>% + dplyr::mutate(total_pop = ifelse(total_pop == 0, NA, total_pop)) %>% dplyr::mutate(degurba_l1 = factor(case_when(urban_centre_pop >= total_pop*0.5 ~ "cities", urban_centre_pop < total_pop*0.5 & rural_grid_pop <= total_pop*0.5 ~ "towns and semi-dense areas", - rural_grid_pop > total_pop*0.5 ~ "rural areas"), + rural_grid_pop > total_pop*0.5 | is.na(total_pop) ~ "rural areas"), levels = c("cities", "towns and semi-dense areas", "rural areas"))) if (fua == TRUE) { polygon_values <- polygon_values %>% @@ -433,7 +434,10 @@ get_spatial_classification_l2 <- function(grid_classification, spatial_classific degurba_l1 == "rural areas" & low_density_rural_cells_pop > rural_cluster_pop & low_density_rural_cells_pop > + very_low_density_rural_cells_pop ~ "dispersed rural areas", degurba_l1 == "rural areas" & - very_low_density_rural_cells_pop > rural_cluster_pop & very_low_density_rural_cells_pop > low_density_rural_cells_pop ~ "mostly uninhabited rural areas"))) + very_low_density_rural_cells_pop > rural_cluster_pop & very_low_density_rural_cells_pop > low_density_rural_cells_pop ~ "mostly uninhabited rural areas"), + levels = c("cities", "dense towns", "semi-dense towns", "suburban areas", "villages", "dispersed rural areas", "mostly uninhabited rural areas"))) %>% + dplyr::mutate(degurba_l2 = tidyr::replace_na(degurba_l2, "mostly uninhabited rural areas")) + if ("fua" %in% colnames(polygon_values)) { polygon_values <- polygon_values %>% dplyr::select(degurba_l1, degurba_l2, fua, fua_metro) @@ -442,4 +446,4 @@ get_spatial_classification_l2 <- function(grid_classification, spatial_classific dplyr::select(degurba_l1, degurba_l2) } return(polygon_values) -} \ No newline at end of file +} diff --git a/code/packages.R b/code/packages.R index 1bfaf51..8382100 100644 --- a/code/packages.R +++ b/code/packages.R @@ -1,6 +1,6 @@ # --------------------------------------------------------------------------------------- # DEGURBA POSTCODE AREAS -# Sascha Göbel +# Sascha Goebel # Script for packages # June 2021 # --------------------------------------------------------------------------------------- @@ -12,15 +12,16 @@ suppressWarnings(if (!require("pacman")) install.packages("pacman")) # load packages and install if not installed -------------------------------------------- -pacman::p_load(dplyr, +pacman::p_load(crayon, + dplyr, + exactextractr, + extrafont, magrittr, purrr, - crayon, - extrafont, + raster, sf, + tidyr, tmap, - raster, - exactextractr, install = TRUE, update = FALSE)