## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  echo = TRUE,
  eval = TRUE,
  fig.width = 8,
  fig.height = 6,
  warning = FALSE,
  message = FALSE
)

## ----load-libraries, eval=TRUE, echo=TRUE-------------------------------------
library(epiviz)
library(dplyr)
library(lubridate)

## ----prepare-line-list, eval=TRUE, echo=TRUE----------------------------------
line_list_pyramid_data <- epiviz::lab_data %>%
  filter(
    organism_species_name == "STAPHYLOCOCCUS AUREUS",
    specimen_date >= as.Date("2023-01-01"),
    specimen_date <= as.Date("2023-12-31"),
    !is.na(date_of_birth),
    !is.na(sex)
  ) %>%
  mutate(
    sex_clean = case_when(
      toupper(sex) %in% c("M", "MALE") ~ "Male",
      toupper(sex) %in% c("F", "FEMALE") ~ "Female",
      TRUE ~ NA_character_
    )
  ) %>%
  filter(!is.na(sex_clean))

## ----static-line-list-pyramid, fig.width=8, fig.height=6, fig.cap="Age-sex pyramid for Staphylococcus aureus detections between January and December 2023.", fig.alt="Back-to-back horizontal bars showing male counts to the left and female counts to the right for each age group from 0-4 up to 85+."----
age_sex_pyramid(
  dynamic = FALSE,
  params = list(
    df = line_list_pyramid_data,
    var_map = list(
      dob_var = "date_of_birth",
      sex_var = "sex_clean"
    ),
    grouped = FALSE,
    mf_colours = c("#440154", "#2196F3"),
    x_breaks = 6,
    x_axis_title = "Number of detections",
    y_axis_title = "Age group (years)",
    chart_title = "Static age-sex pyramid",
    age_calc_refdate = as.Date("2023-12-31")
  )
)

## ----prepare-grouped-pyramid-data, eval=TRUE, echo=TRUE-----------------------
grouped_pyramid_data <- line_list_pyramid_data %>%
  mutate(
    age_years = floor(time_length(interval(date_of_birth, as.Date("2023-12-31")), "years")),
    age_band = cut(
      age_years,
      breaks = c(0, 5, 15, 25, 35, 45, 55, 65, 75, 85, Inf),
      right = FALSE,
      labels = c("0-4", "5-14", "15-24", "25-34", "35-44",
                 "45-54", "55-64", "65-74", "75-84", "85+")
    )
  ) %>%
  filter(!is.na(age_band)) %>%
  count(age_band, sex_clean, name = "val") %>%
  rename(sex_mf = sex_clean) %>%
  mutate(
    lower_ci = if_else(val == 0, 0, qchisq(0.025, 2 * val) / 2),
    upper_ci = qchisq(0.975, 2 * (val + 1)) / 2
  )

## ----interactive-grouped-pyramid, fig.width=8, fig.height=6, fig.cap="Interactive age-sex pyramid with 95% confidence intervals for Staphylococcus aureus detections in 2023.", fig.alt="Interactive back-to-back bar chart with error bars showing male and female counts across age bands."----
age_sex_pyramid(
  dynamic = TRUE,
  params = list(
    df = grouped_pyramid_data,
    var_map = list(
      age_group_var = "age_band",
      sex_var = "sex_mf",
      value_var = "val",
      ci_lower = "lower_ci",
      ci_upper = "upper_ci"
    ),
    grouped = TRUE,
    ci = "errorbar",
    mf_colours = c("pink", "blue"),
    x_breaks = 5,
    chart_title = "Interactive grouped pyramid with CI",
    x_axis_title = "Number of detections",
    y_axis_title = "Age group (years)",
    legend_title = "Sex"
  )
)

