## ----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-monthly-data, eval=TRUE, echo=TRUE-------------------------------
# Create monthly data (as used in tests)
monthly_data <- epiviz::lab_data %>%
  group_by(specimen_month = lubridate::floor_date(specimen_date, 'month')) %>%
  summarise(detections = n()) %>%
  ungroup()

## ----basic-point-chart, fig.cap="Monthly detections with point markers across 2023.", fig.alt="Scatter plot showing the number of detections per month between January 2023 and December 2023."----
point_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = monthly_data,
    x = "specimen_month",        # Date variable for x-axis
    y = "detections",           # Count variable for y-axis
    point_colours = "#007C91",  # Color for points
    point_size = 3,             # Size of points
    x_limit_min = "2023-01-01", # X-axis minimum
    x_limit_max = "2023-12-31", # X-axis maximum
    chart_title = "Detections per Month 2023",
    x_axis_title = "Month of detection",
    y_axis_title = "Number of detections",
    x_axis_date_breaks = "2 months"  # Show every 2 months
  )
)

## ----interactive-point-chart, fig.cap="Interactive monthly detections with hover detail.", fig.alt="Interactive scatter plot of monthly detections allowing hover to view values."----
point_chart(
  dynamic = TRUE,   # Create interactive plotly chart
  params = list(
    df = monthly_data,
    x = "specimen_month",
    y = "detections",
    point_colours = "#007C91",
    point_size = 3,
    x_limit_min = "2022-01-01",
    x_limit_max = "2023-12-31",
    chart_title = "Detections per Month 2022-2023",
    x_axis_title = "Month of detection",
    y_axis_title = "Number of detections",
    x_axis_date_breaks = "2 months"
  )
)

## ----prepare-grouped-point-data, eval=TRUE, echo=TRUE-------------------------
# Create grouped data with confidence intervals (as used in tests)
set.seed(123)
grouped_data <- epiviz::lab_data %>%
  group_by(specimen_month = lubridate::floor_date(specimen_date, 'month'),
           organism_species_name) %>%
  summarise(detections = n()) %>%
  ungroup() %>%
  mutate(
    offset = sample(10:50, n(), replace = TRUE),
    lower_limit = pmax(detections - offset, 0),
    upper_limit = detections + offset
  ) %>%
  select(-offset)

## ----grouped-point-chart, fig.cap="Grouped monthly detections with confidence ribbons.", fig.alt="Scatter plot with different colours for organism species and translucent ribbons showing confidence intervals across monthly detections."----
point_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = grouped_data,
    x = "specimen_month",
    y = "detections",
    group_var = "organism_species_name",  # Group by organism type
    point_colours = c("#007C91", "#8A1B61", "#FF7F32"),  # Colors for each group
    point_size = 3,
    x_limit_min = "2022-01-01",
    x_limit_max = "2023-12-31",
    chart_title = "Detections per Month 2022-2023",
    x_axis_title = "Month of detection",
    y_axis_title = "Number of detections",
    x_axis_date_breaks = "2 months",
    y_axis_break_labels = seq(0, 600, 100),  # Custom y-axis breaks
    x_axis_label_angle = 45,                 # Rotate x-axis labels
    # Confidence interval parameters
    ci = "ribbon",                           # Use ribbon for confidence intervals
    ci_lower = "lower_limit",                # Lower confidence limit column
    ci_upper = "upper_limit",                # Upper confidence limit column
    ci_colours = c("#007C91", "#8A1B61", "#FF7F32")  # Colors for confidence ribbons
  )
)

## ----threshold-point-chart, fig.cap="Grouped monthly detections with threshold lines.", fig.alt="Scatter plot with grouped monthly detections, confidence ribbons, and horizontal threshold lines highlighting key levels."----
point_chart(
  dynamic = FALSE,  # Create static ggplot chart
  params = list(
    df = grouped_data,
    x = "specimen_month",
    y = "detections",
    group_var = "organism_species_name",
    point_colours = c("#007C91", "#8A1B61", "#FF7F32"),
    point_size = 3,
    x_limit_min = "2022-01-01",
    x_limit_max = "2023-12-31",
    chart_title = "Detections per Month 2022-2023",
    x_axis_title = "Month of detection",
    y_axis_title = "Number of detections",
    x_axis_date_breaks = "2 months",
    y_axis_break_labels = seq(0, 600, 100),
    x_axis_label_angle = 45,
    ci = "ribbon",
    ci_lower = "lower_limit",
    ci_upper = "upper_limit",
    ci_colours = c("#007C91", "#8A1B61", "#FF7F32"),
    # Threshold lines
    hline = c(450, 550),                    # Multiple threshold lines
    hline_colour = c("blue", "red"),        # Colors for each line
    hline_label = c("threshold 1", "threshold 2"),  # Labels for lines
    hline_label_colour = c("blue", "red")   # Label colors
  )
)

## ----interactive-grouped-point-chart, fig.cap="Interactive grouped point chart with confidence ribbons and thresholds.", fig.alt="Interactive scatter plot showing grouped monthly detections with ribbons and threshold lines."----
point_chart(
  dynamic = TRUE,   # Create interactive plotly chart
  params = list(
    df = grouped_data,
    x = "specimen_month",
    y = "detections",
    group_var = "organism_species_name",
    point_colours = c("#007C91", "#8A1B61", "#FF7F32"),
    point_size = 3,
    x_limit_min = "2022-01-01",
    x_limit_max = "2023-12-31",
    chart_title = "Detections per Month 2022-2023",
    x_axis_title = "Month of detection",
    y_axis_title = "Number of detections",
    x_axis_date_breaks = "2 months",
    y_axis_break_labels = seq(0, 600, 100),
    x_axis_label_angle = 45,
    ci = "ribbon",
    ci_lower = "lower_limit",
    ci_upper = "upper_limit",
    ci_colours = c("#007C91", "#8A1B61", "#FF7F32"),
    hline = c(450, 550),
    hline_colour = c("blue", "red"),
    hline_label = c("threshold 1", "threshold 2"),
    hline_label_colour = c("blue", "red")
  )
)

