Load the packages

library(ggplot2)
library(reshape2)
library(dplyr)
library(car)
library(tidyr)
library(ggpubr)

Load the data

##        ID       Treatment  W1  W2  W3  W4  W5  W6  W7  W8  W9 W10 W11 W12   F1
## 1    Head Distilled_water 228 234 244 252 264 276 280 308 286 302 300 302  8.6
## 2    Neck Distilled_water 244 240 242 256 292 276 276 280 274 278 280 274 35.0
## 3    Back Distilled_water 284 286 292 306 318 322 324 330 346 350 345 342  9.1
## 4    Tail Distilled_water 290 306 318 322 322 330 334 346 344 344 354 354  8.2
## 5 No Mark Distilled_water 256 268 278 290 292 300 302 310 318 322 326 324  8.3
## 6    Head    Bitter_ Kola 272 294 300 310 320 328 260 270 264 278 278 274  7.9
##     F2   F3   F4   F5   F6   F7   F8   F9  F10  F11  F12 Lee  BMI
## 1  9.0  6.4  6.6  7.2  7.3  7.0  7.3  7.5  7.0  8.1  6.5 319 0.68
## 2 23.4 25.9 22.6 23.4 26.1 24.5 23.8 26.9 28.2 28.0 22.1 325 0.69
## 3  9.4  6.7  6.7  7.3  7.4  8.8  6.5  8.2  7.9  8.0  5.8 325 0.74
## 4  6.9  7.1  7.0  7.2  8.2  8.2  8.4  8.4  7.2  7.9  5.6 322 0.73
## 5  7.4  9.2  6.8  6.7  6.8  7.2  6.8  8.0  6.9  7.5  5.9 327 0.73
## 6  7.8  8.0  6.7  7.8  7.3  6.7  6.5  6.2  6.3  6.0  8.1 300 0.45

Visualize weight distribution for treatments in weeks

# Define the shape palette for different treatments
shape_palette <- c(16, 17, 18, 19, 15, 0, 1, 2, 5, 6)

# Define the weeks columns to plot
weight_columns <- colnames(extract_data)[grepl("^W\\d+$", colnames(extract_data))]

# Initialize an empty list to store the plots
plots <- list()

# Loop through each week column and create plots
for (weight in weight_columns) {
  plot <- ggplot(extract_data, aes_string(x = "ID", y = weight, col = "Treatment", shape = "Treatment", fill = "Treatment")) +
    geom_point(size = 2) +
    scale_shape_manual(values = shape_palette) +
    theme_minimal() +
    labs(title = paste("Weight in", weight), x = "ID", y = "Weight") +
   theme(axis.text.x = element_text(angle = 40, hjust = 1)) # Optional: rotate x-axis labels for better readability
  
  # Add the plot to the list
  plots[[weight]] <- plot
}
## Warning: `aes_string()` was deprecated in ggplot2 3.0.0.
## ℹ Please use tidy evaluation idioms with `aes()`.
## ℹ See also `vignette("ggplot2-in-packages")` for more information.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
print(plots)
## $W1
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W2
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W3
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W4
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W5
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W6
## Warning: Removed 3 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W7
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W8
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W9
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W10
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W11
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

## 
## $W12
## Warning: Removed 5 rows containing missing values or values outside the scale range
## (`geom_point()`).

#boxplot

# Reshape the data for plotting
long_data <- melt(extract_data[, 1:14], id.vars = c("ID", "Treatment"), variable.name = "Week", value.name = "Weight")

# Calculate the median weight for each combination of ID and Treatment
median_weights <- long_data %>%
  group_by(ID, Treatment) %>%
  summarise(median_weight = median(Weight, na.rm = TRUE)) %>%
  ungroup()
## `summarise()` has grouped output by 'ID'. You can override using the `.groups`
## argument.
# Add the median_weight to the original dataframe
long_data <- long_data %>%
  left_join(median_weights, by = c("ID", "Treatment"))

# Ensure unique treatment levels within each ID for correct factor reordering
long_data <- long_data %>%
  group_by(ID) %>%
  mutate(Treatment = factor(Treatment, levels = unique(Treatment[order(median_weight)]))) %>%
  ungroup()

# Create the boxplot
ggplot(long_data, aes(x = ID, y = Weight, fill = Treatment)) +
  geom_boxplot() +
  scale_fill_hue(direction = 1) +
  theme_minimal() +
  labs(title = "Weight Distribution by Treatment", x = "ID", y = "Weight") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))
## Warning: Removed 43 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Calculate average weight across 12 weeks

# Calculate the mean weight across the weeks for each row
extract_data <- extract_data %>%
  rowwise() %>%
  mutate(mean_weight = mean(c_across(W1:W7), na.rm = TRUE))

# View the dataframe with the mean weight column added
head(extract_data)
## # A tibble: 6 × 29
## # Rowwise: 
##   ID      Treatment     W1    W2    W3    W4    W5    W6    W7    W8    W9   W10
##   <chr>   <chr>      <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
## 1 Head    Distilled…   228   234   244   252   264   276   280   308   286   302
## 2 Neck    Distilled…   244   240   242   256   292   276   276   280   274   278
## 3 Back    Distilled…   284   286   292   306   318   322   324   330   346   350
## 4 Tail    Distilled…   290   306   318   322   322   330   334   346   344   344
## 5 No Mark Distilled…   256   268   278   290   292   300   302   310   318   322
## 6 Head    Bitter_ K…   272   294   300   310   320   328   260   270   264   278
## # ℹ 17 more variables: W11 <int>, W12 <int>, F1 <dbl>, F2 <dbl>, F3 <dbl>,
## #   F4 <dbl>, F5 <dbl>, F6 <dbl>, F7 <dbl>, F8 <dbl>, F9 <dbl>, F10 <dbl>,
## #   F11 <dbl>, F12 <dbl>, Lee <int>, BMI <dbl>, mean_weight <dbl>
shape_palette <- c(16, 17, 18, 19, 15, 0, 1, 2, 5, 6)
# Plot
ggplot(extract_data, aes(x = ID, y = mean_weight, col = Treatment, shape = Treatment, fill = Treatment)) +
  geom_point(size = 3) +
  scale_shape_manual(values = shape_palette)+
  theme_minimal() +
  labs(title = "Mean Weight Distribution in across the weeks by Treatment", x = "ID", y = "Mean Weight")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
        axis.title.x = element_text(size = 12, face = "bold"),
        axis.title.y = element_text(size = 12, face = "bold"),
        plot.title = element_text(size = 14, face = "bold", hjust = 0.5))
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).

Weight Changes Over Time by Treatment

# Reshape the data to long format
long_data <- gather(extract_data, key = "Week", value = "Weight", W1:W12)

# Ensure Week is treated as a factor and ordered correctly
long_data$Week <- factor(long_data$Week, levels = paste0("W", 1:12))

# Define the shape palette
shape_palette <- c(16, 17, 18, 19, 15, 0, 1, 2, 5, 6)

# Get unique IDs
unique_ids <- unique(long_data$ID)

# Loop through each ID to generate and save separate plots
for (id in unique_ids) {
  plot <- ggplot(long_data[long_data$ID == id, ], aes(x = Week, y = Weight, col = Treatment, group = interaction(ID, Treatment), shape = Treatment)) +
    geom_point(size = 2) +
    geom_line(linewidth = 0.4) +
    scale_shape_manual(values = shape_palette) +
    theme_minimal() +
    labs(title = paste("Weight Changes Over Time by Treatment for", id), x = "Week", y = "Weight") +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
          axis.title.x = element_text(size = 12, face = "bold"),
          axis.title.y = element_text(size = 12, face = "bold"),
          plot.title = element_text(size = 14, face = "bold", hjust = 0.5))
  
  # Print the plot
  print(plot)
}

## Warning: Removed 43 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 43 rows containing missing values or values outside the scale range
## (`geom_line()`).

FBS Changes Over Time by Treatment

# Reshape the data for plotting
FBS_long_data <- gather(extract_data, key = "Week", value = "FBS", F1:F12)
# Ensure Week is treated as a factor and ordered correctly
FBS_long_data$Week <- factor(FBS_long_data$Week, levels = paste0("F", 1:12))

# Define the shape palette
shape_palette <- c(16, 17, 18, 19, 15, 0, 1, 2, 5, 6)

# Get unique IDs
FBS_unique_ids <- unique(FBS_long_data$ID)

# Create the plot
ggplot(FBS_long_data) +
  aes(x = Week, y = FBS, colour = Treatment, group = interaction(Treatment, ID)) +
  geom_jitter(size = 1.5) +
  geom_line(aes(group = interaction(Treatment, ID)), linewidth = 0.4) +
  scale_color_hue(direction = 1) +
  theme_minimal() +
  labs(title = "FBS Changes Over Time by Treatment", x = "Week", y = "FBS") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
        axis.title.x = element_text(size = 12, face = "bold"),
        axis.title.y = element_text(size = 12, face = "bold"),
        plot.title = element_text(size = 14, face = "bold", hjust = 0.5)) +
  facet_wrap(vars(ID), scales = "free")
## Warning: Removed 45 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 45 rows containing missing values or values outside the scale range
## (`geom_line()`).

# Loop through each ID to generate and save separate plots
for (id in FBS_unique_ids) {
  plot <- ggplot(FBS_long_data[FBS_long_data$ID == id, ], aes(x = Week, y = FBS, col = Treatment, group = interaction(ID, Treatment), shape = Treatment)) +
    geom_point(size = 2) +
    geom_line(linewidth = 0.4) +
    scale_shape_manual(values = shape_palette) +
    theme_minimal() +
    labs(title = paste("FBS Changes Over Time by Treatment for", id), x = "Week", y = "FBS") +
    theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
          axis.title.x = element_text(size = 12, face = "bold"),
          axis.title.y = element_text(size = 12, face = "bold"),
          plot.title = element_text(size = 14, face = "bold", hjust = 0.5))
  
  # Print the plot
  print(plot)
}

## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).

## Warning: Removed 44 rows containing missing values or values outside the scale range
## (`geom_point()`).
## Warning: Removed 44 rows containing missing values or values outside the scale range
## (`geom_line()`).