我正在尝试创建一张地图,在其中按德国邮政编码进行着色,并且我想添加德国各州作为边界。
# 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)
获取德国的形状文件(link https://opendata-esri-de.opendata.arcgis.com/datasets/5b203df4357844c8a6715d7d411a8341_0/explore?location=51.099785%2C10.454062%2C7.47)。在德国,邮政编码称为 Postleitzahlen (PLZ)。
germany <- read_sf("data/OSM_PLZ.shp")
将 PLZ 分为任意组以进行绘图。
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
))
由PLZ进行绘图填充:
tm_shape(germany) +
tm_fill(col = "plz_groups")
尝试在顶部绘制德国各州(“Bundesland”)的边界:
tm_shape(germany) +
tm_fill(col = "plz_groups") +
tm_borders(col = "bundesland")
col2rgb(col, alpha = TRUE) 中的错误:“bundesland”中的 ungültiger Farbname