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

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

Plot state provinces around map colored by German zip codes

问题

我正在尝试创建一个地图,其中我按德国邮政编码着色,并希望将德国各州的边界添加到地图中。

  1. # 用于加载数据的库
  2. library(raster)
  3. library(readr)
  4. library(readxl)
  5. library(sf)
  6. library(dplyr)
  7. # 用于数据集
  8. library(maps)
  9. library(spData)
  10. # 用于绘图
  11. library(grid)
  12. library(tmap)
  13. library(viridis)

获取德国的形状文件(链接)。在德国,邮政编码称为"Postleitzahlen (PLZ)"。

  1. germany <- read_sf("data/OSM_PLZ.shp")

将邮政编码分类为任意分组以进行绘图。

  1. germany <- germany %>%
  2. mutate(plz_groups = case_when(
  3. substr(plz, 1, 1) == "1" ~ "Group A",
  4. substr(plz, 2, 2) == "2" ~ "Group B",
  5. TRUE ~ "Group X" # 剩余的
  6. ))

绘制填充邮政编码的地图:

  1. tm_shape(germany) +
  2. tm_fill(col = "plz_groups")

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

尝试在地图顶部绘制德国各州("Bundesland")的边界:

  1. tm_shape(germany) +
  2. tm_fill(col = "plz_groups") +
  3. 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.

  1. # for loading our data
  2. library(raster)
  3. library(readr)
  4. library(readxl)
  5. library(sf)
  6. library(dplyr)
  7. # for datasets
  8. library(maps)
  9. library(spData)
  10. # for plotting
  11. library(grid)
  12. library(tmap)
  13. library(viridis)

Get shape files for Germany (link). In Germany zip codes are called Postleitzahlen (PLZ).

  1. germany &lt;- read_sf(&quot;data/OSM_PLZ.shp&quot;)

Classify PLZs into arbitrary groups for plotting.

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

Make plot filling by PLZ:

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

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

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

  1. tm_shape(germany) +
  2. tm_fill(col = &quot;plz_groups&quot;) +
  3. 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,它是一个单一的颜色值,用于指定边界线的颜色。

  1. library(sf)
  2. library(dplyr)
  3. library(tmap)
  4. germany <- read_sf("Postleitzahlengebiete_-_OSM/OSM_PLZ.shp")
  5. german_states <- read_sf("Bundesl%C3%A4ndergrenzen_2018/Bundesl&#228;ndergrenzen_2018.shp")
  6. germany <- germany %>%
  7. mutate(plz_groups = case_when(
  8. substr(plz, 1, 1) == "1" ~ "Group A",
  9. substr(plz, 2, 2) == "2" ~ "Group B",
  10. TRUE ~ "Group X" # 剩下的情况
  11. ))
  12. tm_shape(germany) +
  13. tm_fill(col = "plz_groups") +
  14. tm_shape(german_states) +
  15. tm_borders(col = "black")

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

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. &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")

  1. [![enter image description here][1]][1]
  2. [1]: https://i.stack.imgur.com/vXeDN.png
  3. </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:

确定