almr27
almr27

Reputation: 53

Is there a way to use one border for each created group in tmap R?

I'm trying to replicate this map:

enter image description here

Unfortunately there aren't any available shapefiles for those regions, but I do know the provinces in each group.

When I run this code, I get:

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")

enter image description here

How can I eliminate the inside borders for each province, and create an outside border for each group (region) as in the example above with the white border?

Also, with tm_text() How can I show only one label per group(region).

I don't know how to add zip files, but the link for the provinces shapefile is: 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

Forgive the spanish `:)

Upvotes: 1

Views: 93

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 173793

Here's a short way to recreate the plot using only R packages. You don't even need your own shapefile.

We can get the states and assign regions like this:

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))

Now we can merge the regions and create an ocean like this:

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))

And plot with 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"))

enter image description here

Upvotes: 1

Related Questions