英文:
Plot state provinces around map colored by German zip codes
问题
我正在尝试创建一个地图,其中我按德国邮政编码着色,并希望将德国各州的边界添加到地图中。
# 用于加载数据的库
library(raster)
library(readr)
library(readxl)
library(sf)
library(dplyr)
# 用于数据集
library(maps)
library(spData)
# 用于绘图
library(grid)
library(tmap)
library(viridis)
获取德国的形状文件(链接)。在德国,邮政编码称为"Postleitzahlen (PLZ)"。
germany <- read_sf("data/OSM_PLZ.shp")
将邮政编码分类为任意分组以进行绘图。
germany <- germany %>%
mutate(plz_groups = case_when(
substr(plz, 1, 1) == "1" ~ "Group A",
substr(plz, 2, 2) == "2" ~ "Group B",
TRUE ~ "Group X" # 剩余的
))
绘制填充邮政编码的地图:
tm_shape(germany) +
tm_fill(col = "plz_groups")
尝试在地图顶部绘制德国各州("Bundesland")的边界:
tm_shape(germany) +
tm_fill(col = "plz_groups") +
tm_borders(col = "bundesland")
Error in col2rgb(col, alpha = TRUE) : 无效的颜色名称"bundesland"
英文:
I'm trying to create a map, where I color by German zip codes and I would like to add German states as boundaries.
# 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")
Try to plot the boundaries of the German states ("Bundesland") on top:
tm_shape(germany) +
tm_fill(col = "plz_groups") +
tm_borders(col = "bundesland")
Error in col2rgb(col, alpha = TRUE) : ungültiger Farbname in 'bundesland'
答案1
得分: 3
为了在顶部绘制德国各州的边界,首先需要获取德国各州的形状文件,例如从ESRI。然后,您可以通过使用tm_shape
添加该形状文件,然后通过使用tm_borders
绘制边界,其中col
参数用于设置边界线的颜色。根据文档:
对于
tm_borders
,它是一个单一的颜色值,用于指定边界线的颜色。
library(sf)
library(dplyr)
library(tmap)
germany <- read_sf("Postleitzahlengebiete_-_OSM/OSM_PLZ.shp")
german_states <- read_sf("Bundesl%C3%A4ndergrenzen_2018/Bundesländergrenzen_2018.shp")
germany <- germany %>%
mutate(plz_groups = case_when(
substr(plz, 1, 1) == "1" ~ "Group A",
substr(plz, 2, 2) == "2" ~ "Group B",
TRUE ~ "Group X" # 剩下的情况
))
tm_shape(germany) +
tm_fill(col = "plz_groups") +
tm_shape(german_states) +
tm_borders(col = "black")
<details>
<summary>英文:</summary>
To draw the boundaries of the German states on top you first have to get a shape file for the German states, e.g. from [ESRI](https://opendata-esri-de.opendata.arcgis.com/datasets/esri-de-content::bundesländergrenzen-2018/explore?location=50.910218%2C10.454033%2C7.00). Afterwards you could add that shape file via `tm_shape` and draw the boundaries via `tm_borders ` where the `col` argument is used to set the color for the border lines. From the docs:
> For tm_borders, it is a single color value that specifies the border line color.
library(sf)
library(dplyr)
library(tmap)
germany <- read_sf("Postleitzahlengebiete_-_OSM/OSM_PLZ.shp")
german_states <- read_sf("Bundesl%C3%A4ndergrenzen_2018/Bundesländergrenzen_2018.shp")
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
))
tm_shape(germany) +
tm_fill(col = "plz_groups") +
tm_shape(german_states) +
tm_borders(col = "black")
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/vXeDN.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论