## ----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-basic-data, eval=TRUE, echo=TRUE---------------------------------
# Aggregate detections by region for a specific time period (as used in tests)
regional_summary <- epiviz::lab_data %>%
  filter(
    specimen_date >= as.Date("2023-01-01"),
    specimen_date <= as.Date("2023-12-31")
  ) %>%
  group_by(region) %>%
  summarise(detections = n()) %>%
  ungroup()

## ----basic-col-chart, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE---------
col_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = regional_summary,
    x = "region",           # Categorical variable for x-axis
    y = "detections",       # Numeric variable for y-axis
    fill_colours = "#007C91",  # Single color for all bars
    chart_title = "Laboratory Detections by Region 2023",
    x_axis_title = "Region",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -45  # Rotate labels for readability
  )
)

## ----prepare-grouped-data, eval=TRUE, echo=TRUE-------------------------------
# Aggregate by region and organism species (as used in tests)
region_organism_summary <- epiviz::lab_data %>%
  filter(
    specimen_date >= as.Date("2023-01-01"),
    specimen_date <= as.Date("2023-12-31")
  ) %>%
  group_by(region, organism_species_name) %>%
  summarise(detections = n()) %>%
  ungroup()

## ----chunk-1, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE-----------------
col_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = region_organism_summary,
    x = "region",                    # Primary grouping variable
    y = "detections",                # Value variable
    group_var = "organism_species_name",  # Secondary grouping variable
    group_var_barmode = "stack",     # Stack bars within each group
    fill_colours = c("KLEBSIELLA PNEUMONIAE" = "#007C91",
                     "STAPHYLOCOCCUS AUREUS" = "#8A1B61",
                     "PSEUDOMONAS AERUGINOSA" = "#FF7F32"),  # Named color mapping
    chart_title = "Laboratory Detections by Region \nand Species 2023",
    chart_footer = "This chart has been created using simulated data.",
    x_axis_title = "Region",
    y_axis_title = "Number of detections",
    legend_title = "Organism species",
    x_axis_label_angle = -45
  )
)

## ----chunk-2, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE-----------------
col_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = regional_summary,
    x = "region",
    y = "detections",
    fill_colours = "#007C91",
    chart_title = "Laboratory Detections by Region 2023",
    x_axis_title = "Region",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -45,
    bar_labels = "detections",      # Show values on bars
    bar_labels_pos = "bar_base"     # Position labels at base of bars
  )
)

## ----chunk-3, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE-----------------
# Use a shorter time period for case boxes demonstration
case_box_data <- epiviz::lab_data %>%
  filter(
    specimen_date >= as.Date("2023-01-01"),
    specimen_date <= as.Date("2023-01-07")  # One week for case boxes
  ) %>%
  group_by(region, organism_species_name) %>%
  summarise(detections = n()) %>%
  ungroup()

## ----chunk-4, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE-----------------
col_chart(
  dynamic = TRUE,   # Create interactive plotly chart
  params = list(
    df = case_box_data,
    x = "region",
    y = "detections",
    group_var = "organism_species_name",
    group_var_barmode = "stack",
    fill_colours = c("KLEBSIELLA PNEUMONIAE" = "#007C91",
                     "STAPHYLOCOCCUS AUREUS" = "#8A1B61",
                     "PSEUDOMONAS AERUGINOSA" = "#FF7F32"),
    case_boxes = TRUE,              # Enable case boxes
    chart_title = "Laboratory Detections by Region \nand Species (Week 1, 2023)",
    chart_footer = "This chart has been created using simulated data.",
    x_axis_title = "Region",
    y_axis_title = "Number of detections",
    legend_title = "Organism species",
    x_axis_label_angle = -45
  )
)

## ----chunk-5, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE-----------------
col_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = region_organism_summary,
    x = "region",
    y = "detections",
    group_var = "organism_species_name",
    group_var_barmode = "stack",
    fill_colours = c("KLEBSIELLA PNEUMONIAE" = "#007C91",
                     "STAPHYLOCOCCUS AUREUS" = "#8A1B61",
                     "PSEUDOMONAS AERUGINOSA" = "#FF7F32"),
    # Threshold lines
    hline = c(1000, 2000),          # Multiple threshold lines
    hline_colour = c("orange", "red"),  # Colors for each line
    hline_label = c("Alert level", "Outbreak threshold"),  # Labels for lines
    hline_label_colour = c("orange", "red"),  # Label colors
    hline_type = c("dashed", "solid"),  # Line types
    hline_width = c(1, 2),          # Line widths
    chart_title = "Laboratory Detections by Region \nand Species 2023",
    chart_footer = "This chart has been created using simulated data.",
    x_axis_title = "Region",
    y_axis_title = "Number of detections",
    legend_title = "Organism species",
    x_axis_label_angle = -45
  )
)

## ----chunk-6, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE-----------------
# Create weekly time series data
weekly_series <- epiviz::lab_data %>%
  filter(
    specimen_date >= as.Date("2023-01-01"),
    specimen_date <= as.Date("2023-03-31")
  ) %>%
  mutate(
    specimen_week = floor_date(specimen_date, "week", week_start = 1)  # Monday start
  ) %>%
  count(specimen_week, name = "detections")

## ----chunk-7, fig.width=8, fig.height=6, eval=TRUE, echo=TRUE, fig.cap="Weekly laboratory detections between January and March 2023.", fig.alt="Column chart showing the number of laboratory detections for each ISO week between January and March 2023."----
col_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = weekly_series,
    x = "specimen_week",        # Date variable for x-axis
    y = "detections",           # Count variable
    x_time_series = TRUE,       # Indicate this is time series data
    time_period = "iso_year_week",  # Aggregation period
    fill_colours = "#007C91",
    chart_title = "Weekly laboratory detections (Q1 2023)",
    x_axis_title = "Week",
    y_axis_title = "Number of detections",
    x_axis_label_angle = -45,
    # Custom styling for time series
    x_axis_date_breaks = "2 weeks",  # Show every 2 weeks
    x_axis_date_labels = "%b %d"     # Format: Jan 01
  )
)

