# Load necessary libraries
library(ggplot2)
library(dplyr)
library(gridExtra)

# Define the file path
file_path <- "D:/Whiplash_Paper_version_2/11_New_Results/Region_1.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
# Assuming you have a vector of annotations for each bar
annotations <- c("ns", "****", "****", "****")  # Text for annotations

p1 <- ggplot(data_filtered, aes(x = Model, y = c_per)) +
  # Boxplot
  geom_boxplot(outlier.shape = NA, fill = "white", color = "red") +
  
  # Add median text
  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) +
  
  # 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 = "(a)", hjust = -0.5, vjust = 1.5, 
           size = 6, color = "black") +
  
  # X and Y scales
  scale_x_discrete(labels = model_labels) +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +
  
  # Labels
  labs(title = "Critical Year Distribution(%)", x = NULL, y = "Percentage") +
  
  # Theme
  theme(
    plot.title = element_text(color = "black", size = 24),
    axis.title.y = element_text(color = "black", size = 24),
    axis.text.x = element_blank(),  # Remove x-axis text
    axis.title.x = element_text(color = "black", size = 14),
    axis.text = element_text(color = "black", size = 24),
    axis.ticks.length = unit(0.1, "cm"),
    axis.ticks = element_line(color = "black"),
    panel.grid = element_blank(),
    panel.border = element_rect(color = "black", fill = NA, size = 1)
  )



# Plot 2
annotations <- c("ns", "*", "**", "***")  # Text annotations for each boxplot

p2 <- ggplot(data_filtered, aes(x = Model, y = w_per)) +
  # Boxplot
  geom_boxplot(outlier.shape = NA, fill = "white", color = "blue") +
  
  # Add median text above each boxplot
  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) +
  
  # 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 = "(b)", hjust = -0.5, vjust = 1.5, 
           size = 6, color = "black") +
  
  # Axes and scales
  scale_x_discrete(labels = model_labels) +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +
  
  # Labels
  labs(title = "Wet Year Distribution(%)", x = NULL, y = NULL) +
  
  # Theme
  theme(
    plot.title = element_text(color = "black", size = 24),  # Title size
    axis.title = element_blank(),  # Remove axis titles
    axis.text.y = element_blank(),  # Hide y-axis tick text
    axis.text.x = element_blank(),  # Hide x-axis tick text
    axis.text = element_text(color = "black", size = 14),  # General text size
    axis.ticks.length = unit(0.1, "cm"),  # Custom tick length
    axis.ticks = element_line(color = "black"),  # Tick color
    panel.grid = element_blank(),  # Remove grid lines
    panel.border = element_rect(color = "black", fill = NA, size = 1)  # Black border
  )

# Plot 3
annotations <- c("****", "****", "****", "****")  # Annotations for each boxplot

p3 <- ggplot(data_filtered, aes(x = Model, y = other_per)) +
  # Boxplot
  geom_boxplot(outlier.shape = NA, fill = "white", color = "purple") +
  
  # Add median text above each boxplot
  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) +
  
  # 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 = "(c)", hjust = -0.5, vjust = 1.5, 
           size = 6, color = "black") +
  
  # Axes and scales
  scale_x_discrete(labels = model_labels) +
  scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, by = 20)) +
  
  # Labels
  labs(title = "Other Year Distribution(%)", x = NULL, y = NULL) +
  
  # Theme
  theme(
    plot.title = element_text(color = "black", size = 24),
    axis.title = element_blank(),  # Remove axis titles
    axis.text.y = element_blank(),  # Hide y-axis tick text
    axis.text.x = element_blank(),  # Hide x-axis tick text
    axis.text = element_text(color = "black", size = 14),
    axis.ticks.length = unit(0.1, "cm"),  # Custom tick length
    axis.ticks = element_line(color = "black"),  # Tick color
    panel.grid = element_blank(),  # Remove grid lines
    panel.border = element_rect(color = "black", fill = NA, size = 1)  # 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_1_Boxplots_rev.png"
#ggsave(filename = output_file, plot = g, width = 16, height = 4, dpi = 900, bg = 'white')
