有没有办法在tmap R中为每个创建的组使用一个边框?

46qrfjad  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(114)

我试着复制这张Map


的数据
不幸的是,这些地区没有任何可用的shapefile,但我知道每个组中的省份。
当我运行这段代码时,我得到:

x <- c("ggmap", "rgdal", "maptools", "dplyr", "tidyr", "tmap", "ggplot2")

lapply(x, library, character.only = TRUE)

setwd("C:\\Users\\Dell\\Desktop\\Regiones de Saud")

library(tidyverse)
library(tmap)
library(dplyr)
library(sf)
library(rgdal)
library(rgeos)
library(lwgeom)

prov <- readOGR("RD_PROV.shp")

prov@data$Region <- NA
prov@data

# Assigning health region to provinces.

#1
prov@data$Region[prov@data$TOPONIMIA %in% c("DISTRITO NACIONAL", "SANTO DOMINGO", "MONTE PLATA")] <- "Region 0"
#2
prov@data$Region[prov@data$TOPONIMIA %in% c("PERAVIA", "SAN CRISTÓBAL", "SAN JOSÉ DE OCOA")] <- "Region I"
#3
prov@data$Region[prov@data$TOPONIMIA %in% c("SANTIAGO", "PUERTO PLATA", "ESPAILLAT")] <- "Region II"
#4
prov@data$Region[prov@data$TOPONIMIA %in% c("DUARTE", "SAMANÁ", "MARÍA TRINIDAD SÁNCHEZ","HERMANAS MIRABAL")] <- "Region III"
# 5
prov@data$Region[prov@data$TOPONIMIA %in% c("INDEPENDENCIA", "BAORUCO", "BARAHONA","PEDERNALES")] <- "Region IV"
# 6
prov@data$Region[prov@data$TOPONIMIA %in% c("LA ALTAGRACIA", "LA ROMANA", "EL SEIBO","SAN PEDRO DE MACORIS","HATO MAYOR")] <- "Region V"

prov@data$Region[prov@data$TOPONIMIA %in% c("AZUA", "SAN JUAN", "ELÍAS PIÑA")] <- "Region VI"

prov@data$Region[prov@data$TOPONIMIA %in% c("DAJABÓN", "MONTE CRISTI", "SANTIAGO RODRIGUEZ","VALVERDE")] <- "Region VII"

prov@data$Region[prov@data$TOPONIMIA %in% c("LA VEGA", "MONSEÑOR NOUEL", "SÁNCHEZ RAMÍREZ")] <- "Region VIII"

tm_shape(prov) + tm_polygons("Region",border.col = "transparent") + tm_text("Region")

字符串

如何消除每个省的内部边界,并为每个组(地区)创建一个外部边界,如上面的白色边界示例所示?
此外,使用tm_text()如何在每个组(区域)中只显示一个标签。
我不知道如何添加zip文件,但省份shapefile的链接是:https://geoportal.iderd.gob.do/geoserver/ows?service=WFS&version=1.0.0&request=GetFeature&typename=geonode%3ARD_PROV&outputFormat=SHAPE-ZIP&srs=EPSG%3A32619&format_options=charset%3AUTF-8
请原谅西班牙语:)

ewm0tg9j

ewm0tg9j1#

这里有一个简单的方法,只使用R包重新创建图。你甚至不需要自己的shapefile。
我们可以像这样获取状态并分配区域:

library(rnaturalearth)
library(tidyverse)

RD <- ne_states(country = "Dominican Republic", returnclass = "sf") %>%
  mutate(Region = paste("Region", c("VII", "VII", "VI", "IV", "IV", "V", "V",
                                    "V", "III", "III", "II", "II", "IV", "VI",
                                    "I", "I", "0", "0", "V", "V", "VII", "VII", 
                                    "VIII", "III", "III", "II", "IV", "VI",
                                    "VIII", "VIII", "I", "0"))) %>%
  select(Region, geometry) %>%
  bind_rows(ne_countries(scale = 50, country = 'Haiti', returnclass = 'sf') %>%
              mutate(Region = 'Haiti') %>%
              select(Region, geometry))

字符串
现在我们可以合并区域并创建一个海洋,如下所示:

Regions <- RD %>%
  group_by(Region) %>% 
  summarise(geometry = sf::st_union(geometry)) %>%
  mutate(center = sf::st_centroid(geometry))
  
ocean <- sf::st_polygon(x = list(cbind(c(-72.1, -68, -68, -72.1, -72.1),
                                       c(17, 17, 20.5, 20.5, 17)))) %>%
  sf::st_sfc(crs = 'WGS84') %>%
  sf::st_as_sf() %>%
  rename(geometry = x) %>%
  sf::st_difference(sf::st_union(RD))


然后用ggplot2绘图

ggplot(Regions) +
  geom_sf(aes(fill = Region), data = RD) +
  geom_sf(fill = NA, color = "gray90", linewidth = 1) +
  geom_sf_text(aes(geometry = center, label = Region), color = "white", 
               size = 4.5, fontface = "bold", 
               hjust = c(1, 0.5, 0.5, 0.2, 0.8, 0.5, 0.5, 0.5, 0.5, 0.5)) +
  scale_fill_manual(values = c("#ddd5c8", "#478c99", "#c0883c", "#a65645", "#8a5394",
                               "#66a14d", "#b1a553", "#af4765", "#599172", 
                               "#5963a5"), guide = 'none') +
  geom_sf(data = ocean, fill = "#a1bfd7", color = "#a1bfd7", linewidth = 1) +
  theme_void() +
  coord_sf(xlim = c(-72.1, -68), ylim = c(17, 20.5)) +
  theme(panel.background = element_rect(fill = "#a1bfd7"))


的数据

相关问题