Push the knit button!

library(tidyverse) # contains ggplot2, dplyr, tidyr, etc

tuberculosis dataset

tb <- read_csv(here::here("data/TB_notifications_2020-07-01.csv")) %>% 
  dplyr::select(country, iso3, year, new_sp_m04:new_sp_fu) %>%
  pivot_longer(cols=new_sp_m04:new_sp_fu, names_to="sexage", values_to="count") %>%
  mutate(sexage = str_replace(sexage, "new_sp_", "")) %>%
  mutate(sex=substr(sexage, 1, 1), 
         age=substr(sexage, 2, length(sexage))) %>%
  dplyr::select(-sexage) %>%
  filter(!(age %in% c("04", "014", "514", "u"))) %>%
  filter(year > 1996, year < 2013) %>%
  mutate(age_group = factor(age, 
                            labels = c("15-24", "25-34", "35-44", 
                                       "45-54", "55-64", "65-"))) %>%
  dplyr::select(country, year, age_group, sex, count)

# Filter Australia
tb_oz <- tb %>% 
  filter(country == "Australia") 

Exercise 1.1: Stacked bar chart

ggplot(tb_oz, aes(x=year, y=count, fill=sex)) +
  geom_bar(stat="identity") + 
  facet_wrap(~age_group, ncol=6) +
  scale_fill_brewer("Sex", palette="Dark2") 

Exercise 1.2: Side-by-side bar chart

ggplot(tb_oz, aes(x=year, y=count, fill=sex)) +
  geom_bar(stat="identity", position="dodge") + 
  facet_wrap(~age_group, ncol=6) +
  scale_fill_brewer("Sex", palette="Dark2") 

Exercise 1.3: Facetted bar chart

tb_oz %>% 
  ggplot(aes(x=year, y=count, fill=sex)) +
  geom_bar(stat="identity") + 
  facet_grid(sex~age_group) +
  scale_fill_brewer("Sex", palette="Dark2") 

Exercise 1.4: Facetted bar chart for 2012

tb_oz %>% 
  filter(year == 2012) %>%
  ggplot(aes(x=sex, y=count, fill=sex)) +
  geom_bar(stat="identity") + 
  facet_wrap(~age_group, ncol=6) +
  scale_fill_brewer("Sex", palette="Dark2") 

Exercise 1.5: Reverse facetted bar chart for 2012

tb_oz %>% 
  filter(year == 2012) %>%
  ggplot(aes(x=age_group, y=count, fill=age_group)) +
  geom_bar(stat="identity") + 
  facet_wrap(~sex, ncol=6) +
  scale_fill_brewer("Age", palette="Dark2") +
  xlab("Age")

Exercise 1.6: Facetted pie chart for 2012

tb_oz %>% 
  filter(year == 2012) %>%
  ggplot(aes(x=1, y=count, fill=sex)) +
  geom_bar(stat="identity", position="fill") + 
  facet_wrap(~age_group, ncol=6) +
  scale_fill_brewer("", palette="Dark2") +
  ggtitle("Angle") + xlab("") + ylab("") +
  coord_polar(theta = "y")

Exercise 1.7: Reverse facetted pie chart for 2012

tb_oz %>% 
  filter(year == 2012) %>%
  ggplot(aes(x=1, y=count, fill=age_group)) +
  geom_bar(stat="identity", position="fill") + 
  facet_wrap(~sex, ncol=6) +
  scale_fill_brewer("", palette="Dark2") +
  ggtitle("Angle") + xlab("") + ylab("") +
  coord_polar(theta = "y")

Exercise 1.8: Aggregated count bar chart

tb_oz %>%
  group_by(year) %>%
  summarise(count = sum(count)) %>%
  ggplot(aes(x=year, y=count)) +
    geom_bar(stat="identity")

Exercise 1.9: Showing proportions

tb_oz %>% group_by(year, age_group) %>% 
  summarise(p = count[sex=="m"]/sum(count)) %>%
  ggplot(aes(x=year, y=p)) +
    geom_hline(yintercept = 0.50, colour="white", size=2) +
    geom_point() +
    facet_wrap(~age_group, ncol=6) +
    ylab("proportion of males")

Exercise 1.10: Overlaying a model

tb_oz %>% group_by(year, age_group) %>% 
  summarise(p = count[sex=="m"]/sum(count)) %>%
  ggplot(aes(x=year, y=p)) +
    geom_hline(yintercept = 0.50, colour="white", size=2) +
    geom_point() +
    geom_smooth(se=F) +
    facet_wrap(~age_group, ncol=6) +
    ylab("proportion of males")