As described in this previous question, I'm plotting German zip codes. The most granular level is 5-digit, e.g. 10117. I would like to draw boundaries defined on the two digit level around, while also keeping the granularity of the zip codes for coloring.
Here again the first part of my code to color in zip codes:
# for loading our data
library(raster)
library(readr)
library(readxl)
library(sf)
library(dplyr)
# for datasets
library(maps)
library(spData)
# for plotting
library(grid)
library(tmap)
library(viridis)
Get shape files for Germany (link). In Germany zip codes are called Postleitzahlen (PLZ).
germany <- read_sf("data/OSM_PLZ.shp")
Classify PLZs into arbitrary groups for plotting.
germany <- germany %>%
mutate(plz_groups = case_when(
substr(plz, 1, 1) == "1" ~ "Group A",
substr(plz, 2, 2) == "2" ~ "Group B",
TRUE ~ "Group X" # rest
))
Make plot filling by PLZ:
tm_shape(germany) +
tm_fill(col = "plz_groups")
The boundaries I want to draw are the following:
I managed to draw German state boundaries, building on the answer in the other post. But for this I used a different shape file. I haven't found existing shapefiles on the level I'm looking for.
Can I use the granular shapefile I already have and somehow it aggregate up to the 2-digit zip code level?