Solutions

Here is a possible solution for the practical

Return to practical

0)$~$Load required packages

# Packages required
library(tidyverse);library(sf);library(tmap);

1$~$Load the health centres csv file.

# Read in health centre data
hc <- read_csv('data/haiti/haiti-healthsites.csv')

2$~$Load the spatial admin level 2 dataset.

# Load spatial data
adm2 <- st_read('data/haiti/adm2.shp', stringsAsFactors = F)

3$~$Turn the health centres data into a spatial dataset.

# Make health centre a simple feature spatial dataframe
hc <- st_as_sf(hc, coords = c("x", "y"), crs = 4326)

4$~$Project both datasets in UTM. Struggling to find the ESPG code? Try this handy website

hc_utm <-  st_transform(hc, crs = 32618 )

adm2_utm <- st_transform(adm2, crs = 32618)

5$~$Create a 5km buffer around the health services.

buff_5k <- st_buffer(hc_utm, dist = 5000)

6$~$Transform the 5km buffer into a single polygon to improve computing.

# Make the buffer into a single shape to improve computation
buff_5k_single <- st_union(buff_5k)

7$~$Transform the adm2 into a single polygon to improve computing.

# Make adm2 into a single shape to improve computation
adm2_single <- st_union(adm2_utm)

8$~$Calculate the difference between the 5km buffer and the admin polygon.

diff_5k <- st_difference(adm2_single, buff_5k_single)

9$~$Visualise the areas which are further than 5km from a health facility.

tm_shape(adm2_utm) +  # define adm2_utm as the first shape
  tm_polygons() +     # plot adm2_utm as polygons
  tm_shape(diff_5k) + # define the diff_5k as the next shape
  tm_polygons(col = "darkred") + # plot as polygons and colour darkred
  tm_shape(hc_utm) + # define hs_utm as the next shape
  tm_dots() +        # plot the healt centres as points
  tm_add_legend(type = "fill", labels = "Greater than 5km from health centre",
                col = "darkred") + # define the legend for buffer
  tm_add_legend(type = "symbol", labels = "Health centre",
                col = "black", shape = 16, size = 0.5) + # define the legend for the health centres
  tm_layout(legend.text.size = 1, legend.position = c("left", "top")) + # format legend
  tm_scale_bar(position = c("left", "center"), size = 1) # add a scale bar
## Warning: The argument size of tm_scale_bar is deprecated. It has been renamed
## to text.size
## Legend labels were too wide. The labels have been resized to 0.55. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

In one block

Note steps 3 and 4 could be chained together

# Packages required
library(tidyverse);library(sf);library(tmap);

# Read data
hc <- read_csv('data/haiti/haiti-healthsites.csv')
adm2 <- st_read('data/haiti/adm2.shp', stringsAsFactors = F)

# Project data
hc_utm <- hc %>% 
  st_as_sf(coords = c("x", "y"), crs = 4326) %>% 
  st_transform(crs = 32618 )
adm2_utm <- st_transform(adm2, crs = 32618)

# Spatial manipulation
buff_5k <- st_buffer(hc_utm, dist = 5000)
buff_5k_single <- st_union(buff_5k)
adm2_single <- st_union(adm2_utm)
diff_5k <- st_difference(adm2_single, buff_5k_single)

# Spatial visualisation
tm_shape(adm2_utm) +  # define adm2_utm as the first shape
  tm_polygons() +     # plot adm2_utm as polygons
tm_shape(diff_5k) + # define the diff_5k as the next shape
  tm_polygons(col = "darkred") + # plot as polygons and colour darkred
tm_shape(hc_utm) + # define hs_utm as the next shape
  tm_dots() +        # plot the health centres as points
  tm_add_legend(type = "fill", labels = "Greater than 5km from health centre",
                col = "darkred") + # define the legend for buffer
  tm_add_legend(type = "symbol", labels = "Health centre",
                col = "black", shape = 16, size = 0.5) + # define the legend for the health centres
  tm_layout(legend.text.size = 1, legend.position = c("left", "top")) + # format legend
  tm_scale_bar(position = c("left", "center"), size = 1) # add a scale bar

Return to practical