BatchControl24Nov

Author

Team Protons

library(readxl)
library(here)
here() starts at /Users/cilliankinsella/Downloads/R files october/post 19th/R homework 21OCT26
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
rm(list = objects()) # clear the workspace
graphics.off() # close off any open plots
cat("\f") # clear the console
df <- read_excel(here("~/Downloads/BioReactor_mAb_aligned.xlsx")) |> 
  mutate(time_round = round(time_hrs, 6))
p0 <- ggplot(df, aes(x = time_hrs, y = pH , colour = BatchID))+
  geom_point()+
  geom_line()+ 
  labs(x = "time (hours)",
       y = "pH",
       title = "pH during bioreactor processing")+
  theme_bw(base_size = 12);p0
Warning: Removed 921 rows containing missing values or values outside the scale range
(`geom_point()`).
Warning: Removed 921 rows containing missing values or values outside the scale range
(`geom_line()`).

ggplotly(p0, dynamicTicks = TRUE)
run4_batch <- df  |>  filter(BatchID == "Run 4")
ref_batches <-  df  |>  filter(BatchID != "Run 4")
#Time based mean and +/-3Stdev
time_limits <- ref_batches  |> 
  group_by(time_round) |> 
  summarise(
    cl    = mean(pH),
    sd    = sd(pH),
    ucl   = cl + 3 * sd,
    lcl   = cl - 3 * sd,
  )

# Join limits to batch 4 and flag out-of-control points
Batch4_eval <- run4_batch |>
  left_join(time_limits, by = "time_round")  |> 
  mutate(
    status = case_when(
      pH > ucl ~ "Above UCL",
      pH < lcl ~ "Below LCL",
      TRUE          ~ "In Control"
    ),
    out_of_control = status != "In Control"
  )
# Extract and print all out-of-control time points and values
ooc_points <- Batch4_eval |> 
  filter(out_of_control) |> 
  select(time_hrs, pH, cl, sd, lcl, ucl, status)
  p_gg <- ggplot(Batch4_eval, aes(x = time_hrs)) +
    geom_ribbon(aes(ymin = lcl, ymax = ucl), fill = "#d9e6f2", alpha = 0.6) +
    geom_line(aes(y = cl), colour = "darkgreen", linetype = "dashed", linewidth = 0.8) +
    geom_line(aes(y = ucl), colour = "firebrick3", linetype = "dashed", linewidth = 0.7) +
    geom_line(aes(y = lcl), colour = "firebrick3", linetype = "dashed", linewidth = 0.7) +
    geom_line(aes(y = pH), colour = "black", linewidth = 0.9) +
    geom_point(aes(y = pH, colour = status), size = 2) +
    scale_colour_manual(
      values = c("In Control" = "green3", "Above UCL" = "firebrick1", "Below LCL" = "firebrick1")
    ) +
    labs(
      title = "Time-Based Control Chart for Run 4",
      caption = "Limits mean±3SD calculated from 3 reference runs (all runs except 4)",
      x = "Time (hours)",
      y = "pH"
    ) +
    theme_bw(base_size = 12) +
    theme(legend.position = "bottom")
  p_gg;ggplotly(p_gg, dynamicTicks = TRUE)
Warning: Removed 110 rows containing missing values or values outside the scale range
(`geom_ribbon()`).
Warning: Removed 110 rows containing missing values or values outside the scale range
(`geom_line()`).
Removed 110 rows containing missing values or values outside the scale range
(`geom_line()`).
Removed 110 rows containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 701 rows containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 701 rows containing missing values or values outside the scale range
(`geom_point()`).