---
title: "Plotting Lag Fit"
author: "Maks Necki"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Plotting lags}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

This vignette describes the *plot_lag_fit function*, which is used to plot a single growth curve along with the calculated lag and the rationale for lag calculation.

## The plot_lag_fit Function
### Introduction
The plot_lag_fit function creates a plot for a provided growth curve using data generated by the Calculate.Lag function. The plot includes the growth curve, calculated lag, and additional information related to the lag calculation method.

### Usage
The function takes the following parameters:

- **data_new**: A data frame generated by the Calculate.Lag function with required columns such as "time," "biomass," "tangent.point," "predicted.data," "threshold," "N0," "second.deriv.b," "line.intercept," and "line.slope."
- **log10_transform**: If set to TRUE, it plots the y-axis (biomass) on a log10 scale.
- **print_lag_info**: If set to TRUE, it prints the lag length on the graph.
The function returns a ggplot object with the growth curve and other plot elements.

### Examples
```{r eval = FALSE}
# Load required libraries
library(dplyr)

# Generate example data using Calculate.Lag function
set.seed(123)
time <- 1:10
biomass <- c(0.1, 0.3, 0.7, 1.5, 3.0, 5.0, 8.0, 12.0, 18.0, 25.0)
tangent.point <- c(0.3, 0.5, 0.9, 2.0, 4.0, 6.0, 9.0, 12.0, 17.0, 24.0)
predicted.data <- c(0.1, 0.4, 0.8, 1.6, 3.2, 6.0, 8.8, 12.5, 18.2, 25.1)
threshold <- c(0.3, 0.8, 1.3, 2.3, 4.3, 7.0, 10.2, 15.0, 21.0, 28.0)
N0 <- c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
second.deriv.b <- c(0.02, 0.04, 0.09, 0.2, 0.4, 0.6, 0.9, 1.2, 1.7, 2.4)
line.intercept <- c(0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1)
line.slope <- c(0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2)

data_new <- data.frame(
  time = time,
  biomass = biomass,
  tangent.point = tangent.point,
  predicted.data = predicted.data,
  threshold = threshold,
  N0 = N0,
  second.deriv.b = second.deriv.b,
  line.intercept = line.intercept,
  line.slope = line.slope
)

# Plot the growth curve with lag information
plot <- plot_lag_fit(data_new, print_lag_info = TRUE, log10_transform = TRUE)

# Print the plot
print(plot)
```
### Conclusion
The plot_lag_fit function is useful for visualizing a growth curve and the calculated lag, providing valuable insights into bacterial growth analysis.