绘制地图上的德国邮政编码区域,并以颜色区分各州。

huangapple go评论74阅读模式
英文:

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 &lt;- read_sf(&quot;data/OSM_PLZ.shp&quot;)

Classify PLZs into arbitrary groups for plotting.

germany &lt;- germany %&gt;% 
  mutate(plz_groups = case_when(
    substr(plz, 1, 1) == &quot;1&quot; ~ &quot;Group A&quot;,
    substr(plz, 2, 2) == &quot;2&quot; ~ &quot;Group B&quot;,    
    TRUE ~ &quot;Group X&quot; # rest
  ))

Make plot filling by PLZ:

tm_shape(germany) +
  tm_fill(col = &quot;plz_groups&quot;) 

绘制地图上的德国邮政编码区域,并以颜色区分各州。

Try to plot the boundaries of the German states ("Bundesland") on top:

tm_shape(germany) +
  tm_fill(col = &quot;plz_groups&quot;) +
  tm_borders(col = &quot;bundesland&quot;)

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&#228;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&#228;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:

&gt; 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>



huangapple
  • 本文由 发表于 2023年2月14日 03:32:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75440442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定