# Load required packages
pacman::p_load(readr, dplyr, grid, gridExtra, ggplot2)

# Load the data
data <- read.csv("precipitation_mean_max_min_temp_1901_2020_en.csv")

# Set Month as factor with correct order
data$Month <- factor(data$Month, levels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                                            "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))

# Set Period as factor with chronological order
data$Period <- factor(data$Period, levels = c("1901-1930", "1931-1960", "1961-1990", "1991-2020"))

# Define custom colours for periods
period_colours <- c("1901-1930" = "#3399ff", "1931-1960" = "#666666", "1961-1990" = "#000000", "1991-2020" = "#cc0033")

# Define caption for all plots
caption <- "Source: Climatic Research Unit (CRU) of University of East Anglia"

# 1. Temperature Range Plot
temp_plot <- ggplot(data, aes(x = Month, group = Period)) +
  geom_ribbon(aes(ymin = `MinTemperature`, ymax = `MaxTemperature`, fill = Period), alpha = 0.175) +
  geom_line(aes(y = `MeanTemperature`, color = Period)) +
  scale_color_manual(values = period_colours) +
  scale_fill_manual(values = period_colours) +
  labs(title = "1. Temperature Range by Month and Period in Vietnam (1901-2020)",
       subtitle = "Shaded areas show range from min to max temperature; lines show mean temperature",
       x = "Month",
       y = "Temperature (°C)", caption = caption,
       color = "Period",
       fill = "Period") +
  theme_minimal() +
  theme(legend.position = "bottom")

# 2. Precipitation Plot
precip_plot <- ggplot(data, aes(x = Month, y = Precipitation, color = Period, group = Period)) +
  geom_line() +
  geom_point(size = 2) +
  scale_color_manual(values = period_colours) +
  labs(title = "2. Precipitation by Month and Period in Vietnam (1901-2020)",
       subtitle = "Monthly average precipitation",
       x = "Month",
       y = "Precipitation (mm)", caption = caption,
       color = "Period") +
  theme_minimal() +
  theme(legend.position = "bottom")

# 3. Annual Mean Temperature Plot
annual_temp <- data %>%
  group_by(Period) %>%
  summarise(Annual_Mean_Temp = mean(`MeanTemperature`))

annual_temp_plot <- ggplot(annual_temp, aes(x = Period, y = Annual_Mean_Temp, group = 1)) +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", color = "red", linetype = "dashed") +
  labs(title = "3. Annual Mean Temperature by Period in Vietnam (1901-2020)",
       subtitle = "Average of monthly mean temperatures over each 30-year period",
       x = "Period",
       y = "Annual Mean Temperature (°C)", caption = caption) +
  theme_minimal()

# 4. Annual Total Precipitation Plot
annual_precip <- data %>%
  group_by(Period) %>%
  summarise(Annual_Precip = sum(Precipitation))

annual_precip_plot <- ggplot(annual_precip, aes(x = Period, y = Annual_Precip, group = 1)) +
  geom_line(linewidth = 1) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", color = "red", linetype = "dashed") +
  labs(title = "4. Annual Total Precipitation by Period in Vietnam (1901-2020)",
       subtitle = "Sum of monthly precipitation over each 30-year period",
       x = "Period",
       y = "Annual Precipitation (mm)", caption = caption) +
  theme_minimal()

# Display the plots

# Combine plots
# grid.arrange(annual_temp_plot, annual_precip_plot, ncol = 2)
# grid.arrange(temp_plot, precip_plot, ncol = 2) 

print(temp_plot)

print(precip_plot)

print(annual_temp_plot)
## `geom_smooth()` using formula = 'y ~ x'

print(annual_precip_plot)
## `geom_smooth()` using formula = 'y ~ x'