# Load necessary libraries
library(ggplot2)
library(dplyr)
library(gridExtra)

# Define the file path
file_path <- "D:/Whiplash_Paper_version_2/11_New_Results/Region_3.csv"

# Load the data from the CSV file
data <- read.csv(file_path)

# Filter out the 'Hist' category
data_filtered <- data %>% filter(Model != "Hist")

# Ensure the 'Model' column is a factor with the correct order, excluding 'Hist'
data_filtered$Model <- factor(data_filtered$Model, levels = c("rcp45_nf", "rcp45_ff", "rcp85_nf", "rcp85_ff"))

# Create a mapping for the custom labels
model_labels <- c("rcp45_nf" = "RCP 4.5 (NF)",
                  "rcp45_ff" = "RCP 4.5 (FF)",
                  "rcp85_nf" = "RCP 8.5 (NF)",
                  "rcp85_ff" = "RCP 8.5 (FF)")

# Set the base theme to use Times New Roman
theme_set(theme_minimal(base_family = "Times New Roman"))

# Plot 1
annotations <- c("****", "****", "****", "****")  # Text for annotations
p1 <- ggplot(data_filtered, aes(x = Model, y = c_per)) +
  geom_boxplot(outlier.shape = NA, fill = "white", color = "red") +
  geom_text(data = data_filtered %>% group_by(Model) %>% summarize(median = median(c_per)),
            aes(x = Model, y = median + 1, label = round(median, 1)), 
            color = "black", size = 6, vjust = -0.5) +  # Increase size to 6
  # Add horizontal line at y = 15
  geom_hline(yintercept = 15, color = "red", linetype = "dashed", size = 1) +
  
  # Add annotations at y = 65 for each bar
  annotate("text", x = 1:length(unique(data_filtered$Model)), 
           y = 90, label = annotations, color = "black", size = 6) +
  
  annotate("text", x = -Inf, y = Inf, label = "(g)", hjust = -0.5, vjust = 1.5, 
           size = 6, color = "black") +
  
  scale_x_discrete(labels = model_labels) +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +
  labs(title = " ", x = NULL, y = "Percentage", size = 14) +
  theme(
    plot.title = element_text(color = "black",size=18),
    axis.title.y = element_text(color = "black", size = 24),
    axis.text.x = element_blank(),# Increase y-axis label text size to 16
    axis.title.x = element_text(color = "black", size = 14),  # Keep x-axis label text size or adjust as needed
    axis.text = element_text(color = "black", size = 24),  # Increase y-axis tick label font size
    axis.ticks.length = unit(0.1, "cm"),  # Set custom tick length for both axes
    axis.ticks = element_line(color = "black"),  # Ensure tick marks are black and visible
    panel.grid = element_blank(),  # Remove grid lines
    panel.border = element_rect(color = "black", fill = NA, size = 1)  # Add black border
  )


# Plot 2
annotations <- c("****", "****", "****", "****")   # Text annotations for each boxplot
p2 <- ggplot(data_filtered, aes(x = Model, y = w_per)) +
  geom_boxplot(outlier.shape = NA, fill = "white", color = "blue") +
  geom_text(data = data_filtered %>% group_by(Model) %>% summarize(median = median(w_per)),
            aes(x = Model, y = median + 1, label = round(median, 1)), 
            color = "black", size = 6, vjust = -0.5) +  # Increase size to 6
  # Add horizontal line at y = 30
  geom_hline(yintercept = 30, color = "blue", linetype = "dashed", size = 1) +
  
  # Add annotations at y = 65 for each boxplot
  annotate("text", x = 1:length(annotations), y = 90, 
           label = annotations, color = "black", size = 6, vjust = 0) +
  annotate("text", x = -Inf, y = Inf, label = "(h)", hjust = -0.5, vjust = 1.5, 
           size = 6, color = "black") +
  
  scale_x_discrete(labels = model_labels) +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) + 
  labs(title = " ", x = NULL, y = NULL) +
  theme(
    plot.title = element_text(color = "black",size=18),
    axis.title = element_blank(),  # Remove y-axis title
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),# Hide y-axis text (tick labels)
    axis.text = element_text(color = "black", size = 14),  # Increase y-axis tick label font size
    axis.ticks.length = unit(0.1, "cm"),  # Set custom tick length for x-axis
    axis.ticks = element_line(color = "black"),  # Keep the tick marks
    panel.grid = element_blank(),  # Remove grid lines
    panel.border = element_rect(color = "black", fill = NA, size = 1)  # Add black border
  )

# Plot 3
annotations <- c("****", "****", "****", "****")  # Text annotations for each boxplot
p3 <- ggplot(data_filtered, aes(x = Model, y = other_per)) +
  geom_boxplot(outlier.shape = NA, fill = "white", color = "purple") +
  geom_text(data = data_filtered %>% group_by(Model) %>% summarize(median = median(other_per)),
            aes(x = Model, y = median + 1, label = round(median, 1)), 
            color = "black", size = 6, vjust = -0.5) +  # Increase size to 6
  # Add horizontal line at y = 55
  geom_hline(yintercept = 55, color = "purple", linetype = "dashed", size = 1) +
  
  # Add annotations at y = 65 for each boxplot
  annotate("text", x = 1:length(annotations), y = 90, 
           label = annotations, color = "black", size = 6, vjust = 0) +
  annotate("text", x = -Inf, y = Inf, label = "(i)", hjust = -0.5, vjust = 1.5, 
           size = 6, color = "black") +
  
  scale_x_discrete(labels = model_labels) +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +  
  labs(title = " ", x = NULL, y = NULL) +
  theme(
    plot.title = element_text(color = "black",size=18),
    axis.title = element_blank(),  # Remove y-axis title
    axis.text.y = element_blank(),
    axis.text.x = element_blank(),# Hide y-axis text (tick labels)
    axis.text = element_text(color = "black", size = 14),  # Increase y-axis tick label font size
    axis.ticks.length = unit(0.1, "cm"),  # Set custom tick length for x-axis
    axis.ticks = element_line(color = "black"),  # Keep the tick marks
    panel.grid = element_blank(),  # Remove grid lines
    panel.border = element_rect(color = "black", fill = NA, size = 1)  # Add black border
  )

# Combine the plots into a single row
g <- grid.arrange(p1, p2, p3, nrow = 1)

# Save the plot
output_file <- "D:/Whiplash_Paper_version_2/11_New_Results/Region_3_Boxplots_rev.png"
ggsave(filename = output_file, plot = g, width = 16, height = 4, dpi = 900, bg = 'white')
