---
title: "Plotting the Veterinary Examples"
format:
  html:
    toc: true
    number-sections: true
    css: styles.css
execute:
  warning: false
  message: false
---

<!--
%\VignetteIndexEntry{Plotting the Veterinary Examples}
%\VignetteEngine{quarto::html}
%\VignetteEncoding{UTF-8}
-->

```{r}
#| include: false
library(methods)
if (requireNamespace("lme4", quietly = TRUE)) library(lme4)
if (requireNamespace("lmerTest", quietly = TRUE)) library(lmerTest)
load(file.path("..", "data", "ex124.RData"))
load(file.path("..", "data", "ex125.RData"))
load(file.path("..", "data", "ex33.RData"))
load(file.path("..", "data", "ex31.RData"))
```

## Plotting Principles

The original book uses simple black-and-white plots. In R, `ggplot2` can
recreate the same scientific comparisons with clearer labels and reproducible
code. The goal is not decorative plotting; it is to show design structure,
grouping, and model implications.

## Split-Plot Packed Cell Volume Data

```{r}
if (requireNamespace("collapse", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) {
  ex124_plot <-
    ex124 |>
    collapse::fmutate(herd = factor(herd))

  ggplot2::ggplot(
    ex124_plot,
    ggplot2::aes(x = herd, y = PCVdif, shape = dose, color = drug)
  ) +
    ggplot2::geom_point(size = 2.4, position = ggplot2::position_jitter(width = 0.08)) +
    ggplot2::facet_wrap(~ drug, nrow = 1) +
    ggplot2::labs(
      x = "Herd",
      y = "Change in packed cell volume",
      shape = "Dose",
      color = "Drug",
      title = "Packed cell volume response by herd, drug, and dose"
    ) +
    ggplot2::theme_minimal()
}
```

## Region, Drug, and Dose Means

```{r}
if (requireNamespace("ggplot2", quietly = TRUE)) {
  ggplot2::ggplot(
    ex125,
    ggplot2::aes(x = dose, y = Pcv, group = Drug, color = Drug)
  ) +
    ggplot2::stat_summary(fun = mean, geom = "line") +
    ggplot2::stat_summary(fun = mean, geom = "point", size = 2.6) +
    ggplot2::facet_wrap(~ Region) +
    ggplot2::labs(
      x = "Dose",
      y = "Mean packed cell volume",
      title = "Mean response by region, drug, and dose"
    ) +
    ggplot2::theme_minimal()
}
```

## Longitudinal PCV Data

```{r}
if (requireNamespace("ggplot2", quietly = TRUE)) {
  ggplot2::ggplot(
    ex33,
    ggplot2::aes(x = time, y = PCV, group = animal_id, color = breed)
  ) +
    ggplot2::geom_line(linewidth = 0.5) +
    ggplot2::geom_point(size = 1.4) +
    ggplot2::labs(
      x = "Time",
      y = "Packed cell volume",
      color = "Breed",
      title = "Longitudinal packed cell volume by animal"
    ) +
    ggplot2::theme_minimal()
}
```

## Model-Based Lines for Example 3.1

The book displays fitted lines for Example 3.1 Model 3. The packaged `ex31`
data can be plotted with simple group-specific linear fits to inspect the same
relationship between baseline PCV and one-month PCV.

```{r}
if (requireNamespace("collapse", quietly = TRUE) && requireNamespace("ggplot2", quietly = TRUE)) {
  ex31_plot <-
    ex31 |>
    collapse::fmutate(
      treatment = ifelse(drug == "BERENIL", paste(drug, dose), as.character(drug))
    )

  ggplot2::ggplot(
    ex31_plot,
    ggplot2::aes(x = PCV1, y = PCV2, color = treatment)
  ) +
    ggplot2::geom_point(size = 2) +
    ggplot2::geom_smooth(method = "lm", se = FALSE, linewidth = 0.8) +
    ggplot2::labs(
      x = "PCV at treatment",
      y = "PCV one month later",
      color = "Treatment",
      title = "Relationship between baseline and follow-up PCV"
    ) +
    ggplot2::theme_minimal()
}
```
