
## ----message = F, warning = F, echo = F----------------------------------
library(getTBinR)
library(knitr)
library(tidyverse)
library(gganimate)
library(ggfortify)
library(rgdal)
library(rmapshaper)
setwd("~/Desktop/Coursera/Maps/")
## ----message=FALSE, warning=FALSE, include=FALSE, cache = T--------------
# read in data from getTBinR.
# need to massage some country names to make them
# consistent in TB file and in shapefile
tb_africa <- get_tb_burden() %>%
filter(g_whoregion %in% c("Africa", "Eastern Mediterranean")) %>%
mutate(country = str_replace(country,
"Côte d'Ivoire",
"Cote d`Ivoire")) %>%
mutate(country = str_replace(country,
"Congo",
"Congo-Brazzaville")) %>%
mutate(country = str_replace(country,
"Democratic Republic of the Congo-Brazzaville",
"Democratic Republic of Congo")) %>%
mutate(country = str_replace(country,
"United Republic of Tanzania",
"Tanzania"))
# read in shapefile. This came from www.maplibrary.org
# it is reduced in size, otherwise my laptop would struggle with over 4M points
africa <- readOGR(dsn = "Africa", layer = "Africa")
africa <- ms_simplify(africa, keep = 0.01)
africa$id <- row.names(africa)
africa_df <- fortify(africa)
africa_df <- left_join(africa_df, africa@data)
africa_df <- africa_df %>% rename(country = COUNTRY)
africa_df <- left_join(africa_df, tb_africa)
## ----message = F, warning = F, echo = F, cache = T-----------------------
# the plot. transition_states does the fun stuff
p <- ggplot(data = africa_df, # the input data
aes(x = long, y = lat, fill = e_mort_100k, group = group)) +
geom_polygon() + # plot the countries
geom_path(colour = "black", lwd = 0.05) + # country borders
coord_equal() +
scale_fill_gradient(name = "TB Incidence",
low = "cadetblue1",
high = "navyblue") +
transition_states(year, transition_length = 1, state_length = 30) +
labs(title = "Estimated Incidence of Tuberculosis in Africa\n2000-2017",
subtitle = "Year: {closest_state}",
caption = "Source: World Health Organisation") +
theme(axis.text = element_blank(), # change the theme options
axis.title = element_blank(), # remove axis titles
axis.ticks = element_blank(),
plot.title = element_text(hjust = 0.5,
size = 18,
colour = "firebrick4",
face = "bold"),
plot.subtitle = element_text(hjust = 0.5,
size = 20,
colour = "firebrick4"),
panel.background = element_blank()) # remove axis ticks
## ----message = F, warning = F, echo = F, cache = T-----------------------
animate(p, nframes = 100)
## ----code = readLines(knitr::purl(knitr::current_input(), documentation = 1)), echo = T, eval = F, cache = T----
## #code appendix