Boni M. Ale, MD, MSc, MPH
30-09-2022
Modifiable risk factors:
Non-modifiable risk factors:
At this stage we will import both databases and do some data manipulation.
Let’s install and load all required packages here:
After loading the required packages, let’s load each datasets (prevalence data and geospatial data)
Let’s clean our geodata and merge both databases
# Clean geospatial data
clean_benin_dpts_data <- function(x){
x %>%
mutate(
adm1_name = ifelse(adm1_name == "Atakora", "Atacora", adm1_name)
)
}
benin_dpts <- clean_benin_dpts_data(x = benin_dpts_raw)
# Merge prevalence data and geospatial data
merge_data <- function(x, y){
left_join(
x = x %>% select(adm1_name, shape_Leng, Shape_Area),
y = y %>% select(-ic),
by = c("adm1_name" = "department")
) %>%
select(year, dpt = adm1_name, prev, shape_length = shape_Leng, shape_area = Shape_Area)
}
merged <- merge_data(x = benin_dpts, y = prev_data_raw)Let’s compare the changes in prevalence of HTN in 2008 versus 2015 according to administrative regions in Benin
make_prev_map <- function(x, yr){
tm_shape(shp = x %>% filter(yr == year)) +
tm_polygons(col = "prev", breaks = seq(from = 15, to = 40, by = 5)) +
tm_text(text = "dpt", size = 0.5) +
tm_layout(legend.outside = TRUE)
}
prev_map_08 <- make_prev_map(x = merged, yr = 2008)
prev_map_15 <- make_prev_map(x = merged, yr = 2015)
facetted_prev_map <- tmap_arrange(prev_map_08, prev_map_15)Here, we will inset a graph within the map. Let’s see how to do it!
create_inset_map <- function(x, y){
# Get the locations of centroids ----
dpts <- unique(x$dpt)
coords <- x %>%
select(dpt, geometry) %>%
distinct() %>%
st_centroid() %>%
st_coordinates()
dpt_centroids <- bind_cols(dpt = dpts, coords)
# Get maximum prevalence (to set maximum of y-axis)
max_prev <- max(x$prev) |> ceiling()
# Create nested tibble of inset plots and centroid coordinates
nested <- x %>%
st_drop_geometry() %>%
select(year, dpt, prev) %>%
nest(data = -dpt) %>%
mutate(
plots = purrr::map(data, function(d){
ggplot(data = d, mapping = aes(x = year, y = prev)) +
geom_col(mapping = aes(fill = as.factor(year))) +
scale_x_continuous(breaks = c(2008, 2015)) +
scale_y_continuous(limits = c(0, max_prev)) +
scale_fill_manual(breaks = c(2008, 2015), values = c("blue", "red")) +
labs(x = NULL, y = NULL) +
theme_bw() +
theme_transparent() +
theme(
legend.position = "none",
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
}),
width = 0.75,
height = 0.75
) %>%
select(-data) %>%
left_join(dpt_centroids, by = "dpt")
# Create map with inset plots ----
ggplot() +
geom_sf(data = y %>% select(adm1_name, geometry)) +
theme_void() +
geom_subview(
data = nested,
mapping = aes(
x = X,
y = Y,
subview = plots,
width = width,
height = height
)
)
}
inset_map <- create_inset_map(x = merged, y = benin_dpts)Let’s visualise the quantitative changes from 2008 to 2015 using a map with diverging shades
create_diverging_palette_map <- function(x, y){
# calculate the difference in prev of htn between 2015 and 2008
diff_df <- x %>%
st_drop_geometry() %>%
select(year, dpt, prev) %>%
tidyr::pivot_wider(names_from = year, values_from = prev) %>%
mutate(diff = `2015` - `2008`)
# join this difference with the geodata
diff_geo <- left_join(
x = y %>% select(adm1_name, geometry),
y = diff_df %>% select(dpt, diff),
by = c("adm1_name" = "dpt")
)
# create the map
tm_shape(shp = diff_geo) +
tm_fill(
col = "diff",
title = "Change in prevalence",
style = "cont",
palette = "-RdYlGn"
) +
tm_text(text = "adm1_name", size = 0.75) +
tm_borders() +
tm_layout(legend.outside = TRUE)
}