Skip to content

Easy Access to Spatial Data for South Africa in R

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md
Notifications You must be signed in to change notification settings

WihanZA/rsa.shapefiles

Repository files navigation

rsa.shapefiles

This package simplifies the process of loading and visualising spatial data for South Africa in R. Shapefiles encompass various administrative levels, such as provinces, districts, municipalities, main places, and subplaces, using Census 2011 demarcations.

The main dataset of interest subplaces data frame is embedded in the package. _id columns represent unique numeric identifiers, while _name columns provide descriptive names. _mdb columns present string identifiers corresponding to the demarcations of the Municipal Demarcation Board of South Africa for provinces, districts, and municipalities.

subplaces is structured hierarchically on the basis of _id values, with the exception of districts. Consider, for example, the subplace “Wemmershoek”:

  • subplace_id: 167007001
  • mainplace_id: 167007
  • municipality_id: 167
  • province_id: 1

Installation

This package requires a working installation of sf.

You can install the development version from this Github repository via the remotes package in R.

# install.packages("remotes")
# install.packages("sf")
remotes::install_github("WihanZA/rsa.shapefiles")
library(rsa.shapefiles)
library(sf)

Examples

Loading and filtering data

rsa.shapefiles::subplaces %>%
  filter(grepl("Stellenbosch", municipality_name)) %>%
  distinct(
    mainplace_name,
    subplace_name
  ) %>%
  head()
## # A tibble: 6 × 2
##   mainplace_name subplace_name  
##   <chr>          <chr>          
## 1 Franschhoek    Wemmershoek    
## 2 Tennantville   Tennantville SP
## 3 Klapmuts       Bennetsville   
## 4 Klapmuts       Klapmuts SP    
## 5 Klapmuts       Weltevrede Park
## 6 Klapmuts       Mandela City

Plotting data

library(ggplot2)

ggplot() +
  theme_void() +
  geom_sf(data = rsa.shapefiles::subplaces, color = "grey50")

Aggregating data with st_union

# From subplaces to provinces
provinces <- rsa.shapefiles::subplaces %>%
  group_by(province_id) %>%
  summarise(
    geometry = st_union(geometry),
    .groups = "drop"
  )

But st_union may take a long time to run, so you can use the pre-aggregated provinces data frame instead, as in the example below.

ggplot() +
  theme_void() +
  # use subplace fill
  geom_sf(
    data = rsa.shapefiles::subplaces,
    aes(fill = stringr::str_sub(subplace_id, 6, 9)),
    color = NA,
    show.legend = FALSE
  ) +
  # use province borders
  geom_sf(
    data = rsa.shapefiles::provinces,
    color = "white",
    fill = NA,
    linewidth = 1
  ) +
  scale_fill_viridis_d(option = "magma", begin = 0.15)

Acknowledgements

About

Easy Access to Spatial Data for South Africa in R

Resources

License

Unknown, MIT licenses found

Licenses found

Unknown
LICENSE
MIT
LICENSE.md

Stars

Watchers

Forks

Languages