## ----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)

## ----basic-epi-curve, fig.cap="Monthly epi curve with a three-month rolling average.", fig.alt="Column chart showing monthly laboratory detections with an overlaid three-month rolling average line."----
epi_curve(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = epiviz::lab_data,
    date_var = "specimen_date",     # Date variable in the dataset
    date_start = "2020-01-01",      # Start date for the curve
    date_end = "2023-12-31",        # End date for the curve
    time_period = "year_month",     # Monthly aggregation
    fill_colours = "#007C91",       # Color for bars
    # Rolling average parameters
    rolling_average_line = TRUE,                    # Include rolling average line
    rolling_average_line_lookback = 3,             # 3-month rolling average
    rolling_average_line_legend_label = "3-month rolling average",  # Legend label
    chart_title = "Laboratory Detections per Month",
    x_axis_title = "Year - Month",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -90
  )
)

## ----grouped-epi-curve, fig.cap="Stacked weekly epi curve by organism species with seven-week rolling average.", fig.alt="Stacked column chart showing weekly laboratory detections by organism species, with an overlaid seven-week rolling average line."----
# Define custom week breaks for better x-axis display
week_seq <- seq(as.Date("2021-01-01"), as.Date("2022-05-31"), by = '2 week')
week_breaks <- paste0(lubridate::isoyear(week_seq), '-W', lubridate::isoweek(week_seq))

epi_curve(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = epiviz::lab_data,
    date_var = "specimen_date",
    date_start = "2021-01-01",
    date_end = "2022-05-31",
    time_period = "iso_year_week",  # Weekly aggregation using ISO weeks
    group_var = "organism_species_name",  # Group by organism type
    group_var_barmode = "stack",    # Stack bars to show total and composition
    fill_colours = c("KLEBSIELLA PNEUMONIAE" = "#007C91",
                     "STAPHYLOCOCCUS AUREUS" = "#8A1B61",
                     "PSEUDOMONAS AERUGINOSA" = "#FF7F32"),  # Named color mapping
    rolling_average_line = TRUE,
    rolling_average_line_legend_label = "7-week rolling average",
    chart_title = "Laboratory detections by species \n 2021-01 - 2022-05",
    chart_footer = "This chart has been created using simulated data.",
    x_axis_title = "Year - ISO Week",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -90,
    x_axis_break_labels = week_breaks,  # Custom week labels
    y_axis_break_labels = seq(0, 250, 20),  # Custom y-axis breaks
    chart_title_colour = "#007C91",
    chart_footer_colour = "#007C91"
  )
)

## ----detailed-daily-epi-curve, fig.cap="Daily epi curve with stacked bars, case boxes, cumulative line, and threshold indicator.", fig.alt="Stacked daily column chart between June and July 2021 showing laboratory detections by species, annotated with case boxes, a seven-day rolling average line, and a cumulative total line."----
epi_curve(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = epiviz::lab_data,
    date_var = "specimen_date",
    date_start = "2021-06-01",
    date_end = "2021-07-31",
    time_period = "day",            # Daily aggregation for detailed analysis
    group_var = "organism_species_name",
    group_var_barmode = "stack",
    fill_colours = c("#007C91", "#8A1B61", "#FF7F32"),
    # Advanced features
    case_boxes = TRUE,              # Enable case boxes
    rolling_average_line = TRUE,
    rolling_average_line_legend_label = "7-day rolling average",
    cumulative_sum_line = TRUE,     # Add cumulative line
    # Threshold line
    hline = c(35),                  # Threshold value
    hline_label = "Threshold",
    hline_width = 0.5,
    hline_colour = "orange",
    hline_label_colour = "orange",
    hline_type = "dotdash",
    # Styling
    chart_title = "Laboratory detections by species \n June - July 2021",
    chart_title_colour = "#007C91",
    legend_title = "Detected organisms",
    legend_pos = "right",
    y_limit_max = 40,               # Set y-axis maximum
    x_axis_break_labels = as.character(seq(as.Date("2021-06-01"),
                                           as.Date("2021-07-31"),
                                           by = '2 days')),  # Every 2 days
    y_axis_break_labels = seq(0, 40, 5),  # Every 5 units
    x_axis_title = "Date",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -90,
    y_axis_label_angle = 90
  )
)

## ----prepare-pre-aggregated-data, eval=TRUE, echo=TRUE------------------------
# Create pre-aggregated data by date
pre_agg_data <- epiviz::lab_data %>%
  group_by(specimen_date) %>%
  summarise(detections = n()) %>%
  ungroup()

## ----pre-aggregated-epi-curve, fig.cap="Weekly epi curve from pre-aggregated counts with rolling average and cumulative total.", fig.alt="Column chart summarising weekly detections from pre-aggregated data with an overlaid three-week rolling average line and an orange cumulative total line."----
epi_curve(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = pre_agg_data,
    y = "detections",               # Specify the count column
    date_var = "specimen_date",
    date_start = "2021-10-01",
    date_end = "2022-03-31",
    time_period = "iso_year_week",  # Weekly aggregation
    # Rolling and cumulative lines
    rolling_average_line = TRUE,
    rolling_average_line_lookback = 3,
    rolling_average_line_legend_label = "3-week rolling average",
    rolling_average_line_colour = "#007C91",
    rolling_average_line_width = 1.5,
    cumulative_sum_line = TRUE,     # Add cumulative line
    cumulative_sum_line_colour = "orange",
    chart_title = "Laboratory Detections by Region \nWinter 2021-22",
    chart_title_colour = "#007C91",
    legend_pos = "right",
    y_axis_break_labels = seq(0, 300, 50),
    x_axis_title = "ISO Week",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -90,
    hover_labels = "<b>Week:</b> %{x}<br><b>Count:</b> %{y}"  # Custom hover text
  )
)

## ----interactive-epi-curve, fig.cap="Interactive stacked epi curve with weekly aggregation and rolling average.", fig.alt="Interactive stacked bar chart of weekly laboratory detections by organism species, including a seven-week rolling average line."----
epi_curve(
  dynamic = TRUE,   # Create interactive plotly chart
  params = list(
    df = epiviz::lab_data,
    date_var = "specimen_date",
    date_start = "2021-01-01",
    date_end = "2022-05-31",
    time_period = "iso_year_week",
    group_var = "organism_species_name",
    group_var_barmode = "stack",
    fill_colours = c("KLEBSIELLA PNEUMONIAE" = "#007C91",
                     "STAPHYLOCOCCUS AUREUS" = "#8A1B61",
                     "PSEUDOMONAS AERUGINOSA" = "#FF7F32"),
    rolling_average_line = TRUE,
    rolling_average_line_legend_label = "7-week rolling average",
    chart_title = "Laboratory detections by species \n 2021-01 - 2022-05",
    chart_footer = "This chart has been created using simulated data.",
    x_axis_title = "Year - ISO Week",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -90,
    x_axis_break_labels = week_breaks,
    y_axis_break_labels = seq(0, 250, 20),
    chart_title_colour = "#007C91",
    chart_footer_colour = "#007C91"
  )
)

