Skip to content

Instantly share code, notes, and snippets.

@seabbs
Last active March 7, 2018 13:42
Show Gist options
  • Save seabbs/b75ae4c7a94382bec641991255b25ac0 to your computer and use it in GitHub Desktop.
Save seabbs/b75ae4c7a94382bec641991255b25ac0 to your computer and use it in GitHub Desktop.
Exploring distributions of TB incidence rates over time by region
## Get required packages, managed via pacman
if (!require(pacman)) install.packages("pacman"); library(pacman)
p_load("getTBinR")
p_load("tidyverse")
p_load("viridis")
p_load("hrbrthemes")
p_load("ggridges")
p_load_gh("thomasp85/patchwork", dependencies = TRUE)
tb <- get_tb_burden()
dict <- get_data_dict()
## Get number of countries with no TB incidence and a global summary
zero_inc <- tb %>%
filter(year > 2007) %>%
filter(e_inc_100k == 0) %>%
group_by(g_whoregion, year) %>%
summarise(n = n()) %>%
mutate(Region = g_whoregion)
zero_inc <- zero_inc %>%
bind_rows( tb %>%
filter(year > 2007) %>%
filter(e_inc_100k == 0) %>%
group_by(year) %>%
summarise(n = n()) %>%
mutate(Region = "Global")) %>%
mutate(Region = Region %>%
factor(levels = c("Global", unique(tb$g_whoregion)))) %>%
mutate(type = case_when(Region %in% "Global" ~ "Globally",
TRUE ~ "By Region"))
## Plot number of zero TB incidence countries over time
plot_zero_countries <- zero_inc %>%
ggplot(aes(x = year, y = n, col = Region)) +
geom_line(size = 1.1) +
geom_point(size = 1.5) +
scale_color_viridis(discrete = TRUE, option = "plasma", direction = -1, end = 0.7) +
scale_y_continuous(breaks= seq(0, 15, 1), minor_breaks = NULL) +
theme_minimal() +
theme(legend.position = "bottom") +
guides(col = guide_legend(nrow = 2)) +
facet_wrap(~type, dir = "v") +
labs(x = "Year",
y = "Number of countries with zero TB incidence rates",
caption = "@seabbs Source: World Health Organisation",
title = "Number of TB Free Countries",
subtitle = "By region and globally, 2008 to 2016")
## Distribution of TB incidence rates by country stratified by region and year
## Countries with zero incidence rates have been excluded.
plot_nonzero_countries <- tb %>%
filter(year > 2007) %>%
filter(e_inc_100k > 0) %>%
ggplot(aes(y = g_whoregion, x = e_inc_100k, fill = g_whoregion)) +
geom_density_ridges() +
scale_x_log10() +
scale_fill_viridis(discrete = TRUE, option = "plasma", direction = -1, end = 0.9) +
theme_minimal() +
theme(legend.position = "none") +
facet_wrap(~year) +
labs(x = search_data_dict("e_inc_100k")$definition %>%
paste0(" (log scale)"),
y = "Region",
caption = "Countries with zero TB incidence have been excluded.",
title = "Tuberculosis (TB) Incidence Rates by Region",
subtitle = "Distribution of country level TB incidence rates, 2008 to 2016")
## Make combined plot
storyboard <- plot_nonzero_countries +
plot_zero_countries +
plot_layout(ncol = 2, widths = c(7, 3))
storyboard
ggsave("storyboard_country_dist_incidnce_rates.png",
storyboard, width = 16, height = 12, dpi = 330)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment