library(readxl)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(writexl)
library(patchwork)

Export_GDP <- read_excel("Export_GDP.xls")
Export_Partner_Share <- read_excel("Export_Partner_Share.xlsx")
Import_Partner_Share <- read_excel("Import_Partner_Share.xlsx")
Inflation_CPI <- read_excel("Inflation_CPI.xls")
Inflation_Deflator <- read_excel("Inflation_Deflator.xls")
RGDP_Growth <- read_excel("RGDP_Growth.xls")
RGDP <- read_excel("RGDP.xls")
Unemployment_Rate <- read_excel("Unemployment_Rate.xls")

highlight_years <- c(1994, 1999, 2004, 2009, 2014, 2019, 2024)

Real GDP

RGDP_long <- RGDP |>
  filter(`Country Name` == "Singapore") |>
  pivot_longer(cols = -1, names_to = "Year", values_to = "RGDP") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(RGDP), Year >= 1994)

highlight_data_RGDP <- RGDP_long |>
  filter(Year %in% highlight_years)

ggplot(RGDP_long, aes(x = Year, y = RGDP)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(data = highlight_data_RGDP, color = "red", size = 3) +
  geom_text(data = highlight_data_RGDP, aes(label = Year), vjust = -1, size = 3.5, color = "red") +
  labs(title = "Singapore Real GDP Over Time", x = "Year", y = "Real GDP (USD)") +
  theme_minimal()


Real GDP Growth Rate

RGDP_Growth_long <- RGDP_Growth |>
  filter(`Country Name` == "Singapore") |>
  pivot_longer(cols = -1, names_to = "Year", values_to = "Growth") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Growth), Year >= 1994)

highlight_data_RGDP_Growth <- RGDP_Growth_long |>
  filter(Year %in% highlight_years)

ggplot(RGDP_Growth_long, aes(x = Year, y = Growth)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(data = highlight_data_RGDP_Growth, color = "red", size = 3) +
  geom_text(data = highlight_data_RGDP_Growth, aes(label = Year), vjust = -1, size = 3.5, color = "red") +
  labs(title = "Singapore Real GDP Growth Rate Over Time", x = "Year", y = "RGDP Growth Rate (%)") +
  theme_minimal()


Unemployment Rate

Unemployment_Rate_long <- Unemployment_Rate |>
  filter(`Country Name` == "Singapore") |>
  pivot_longer(cols = -1, names_to = "Year", values_to = "Unemployment") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Unemployment), Year >= 1994)

highlight_data_Unemployment <- Unemployment_Rate_long |>
  filter(Year %in% highlight_years)

ggplot(Unemployment_Rate_long, aes(x = Year, y = Unemployment)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(data = highlight_data_Unemployment, color = "red", size = 3) +
  geom_text(data = highlight_data_Unemployment, aes(label = Year), vjust = -1, size = 3.5, color = "red") +
  labs(title = "Singapore Unemployment Rate Over Time", x = "Year", y = "Unemployment Rate (%)") +
  theme_minimal()


Exports as % of GDP

Export_GDP_long <- Export_GDP |>
  pivot_longer(cols = -1, names_to = "Year", values_to = "Export_GDP") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Export_GDP))

highlight_data <- Export_GDP_long |>
  filter(Year %in% highlight_years)

ggplot(Export_GDP_long, aes(x = Year, y = Export_GDP)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(data = highlight_data, color = "red", size = 3) +
  geom_text(data = highlight_data, aes(label = Year), vjust = -1, size = 3.5, color = "red") +
  labs(title = "Singapore Exports of Goods and Services (% of GDP)", x = "Year", y = "Export GDP (%)") +
  theme_minimal()


Inflation (CPI)

Inflation_CPI_long <- Inflation_CPI |>
  filter(`Country Name` == "Singapore") |>
  pivot_longer(cols = -1, names_to = "Year", values_to = "CPI") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(CPI), Year >= 1994)

highlight_data_CPI <- Inflation_CPI_long |>
  filter(Year %in% highlight_years)

ggplot(Inflation_CPI_long, aes(x = Year, y = CPI)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(data = highlight_data_CPI, color = "red", size = 3) +
  geom_text(data = highlight_data_CPI, aes(label = Year), vjust = -1, size = 3.5, color = "red") +
  labs(title = "Singapore CPI Inflation Rate Over Time", x = "Year", y = "Inflation Rate (CPI %)") +
  theme_minimal()


Inflation (GDP Deflator)

Inflation_Deflator_long <- Inflation_Deflator |>
  filter(`Country Name` == "Singapore") |>
  pivot_longer(cols = -1, names_to = "Year", values_to = "Deflator") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Deflator), Year >= 1994)

highlight_data_Defl <- Inflation_Deflator_long |>
  filter(Year %in% highlight_years)

ggplot(Inflation_Deflator_long, aes(x = Year, y = Deflator)) +
  geom_line(color = "steelblue", linewidth = 1) +
  geom_point(data = highlight_data_Defl, color = "red", size = 3) +
  geom_text(data = highlight_data_Defl, aes(label = Year), vjust = -1, size = 3.5, color = "red") +
  labs(title = "Singapore Inflation Deflator Rate Over Time", x = "Year", y = "Inflation Rate (Deflator %)") +
  theme_minimal()


Export Partner Share

Export_Partner_long <- Export_Partner_Share |>
  pivot_longer(cols = -c(1:4), names_to = "Year", values_to = "Share") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Share), Share > 0)

top5_countries <- Export_Partner_long |>
  filter(Year %in% highlight_years, `Partner Name` != "World") |>
  group_by(`Partner Name`) |>
  summarise(avg_share = mean(Share, na.rm = TRUE)) |>
  slice_max(avg_share, n = 5) |>
  pull(`Partner Name`)

top5_data <- Export_Partner_long |>
  filter(`Partner Name` %in% top5_countries)

highlight_data <- top5_data |>
  filter(Year %in% highlight_years)

ggplot(top5_data, aes(x = Year, y = Share, color = `Partner Name`)) +
  geom_line(linewidth = 1) +
  geom_point(data = highlight_data, size = 3) +
  geom_text(data = highlight_data, aes(label = Year), vjust = -1, size = 2.8) +
  labs(title = "Singapore Export Partner Share (%) - Top 5 Countries",
       x = "Year", y = "Export Partner Share (%)", color = "Partner Country") +
  theme_minimal()


Export Partner Composition

pie_data <- Export_Partner_Share |>
  pivot_longer(cols = -c(1:4), names_to = "Year", values_to = "Share") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Share), Share > 0,
         `Partner Name` %in% top5_countries,
         Year %in% highlight_years)

available_years <- intersect(highlight_years, unique(pie_data$Year))

plots_list <- lapply(seq_along(available_years), function(i) {
  yr <- available_years[i]
  df <- pie_data |>
    filter(Year == yr) |>
    arrange(desc(Share)) |>
    mutate(pct = Share / sum(Share) * 100,
           label = paste0(round(pct, 1), "%"),
           pos = cumsum(pct) - pct / 2)
  p <- ggplot(df, aes(x = "", y = pct, fill = `Partner Name`)) +
    geom_col(width = 1, color = "white") +
    coord_polar(theta = "y") +
    geom_text(aes(y = pos, label = label), color = "white", size = 3, fontface = "bold") +
    labs(title = as.character(yr), fill = "Country") +
    theme_void() +
    theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 12))
  if (i != length(available_years)) p <- p + theme(legend.position = "none")
  p
})

wrap_plots(plots_list, ncol = 4) +
  plot_annotation(title = "Singapore Export Partner Share - Top 5 Countries") +
  plot_layout(guides = "collect")


Import Partner Share

Import_Partner_long <- Import_Partner_Share |>
  pivot_longer(cols = -c(1:4), names_to = "Year", values_to = "Share") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Share), Share > 0)

top5_import <- Import_Partner_long |>
  filter(Year %in% highlight_years, `Partner Name` != "World") |>
  group_by(`Partner Name`) |>
  summarise(avg_share = mean(Share, na.rm = TRUE)) |>
  slice_max(avg_share, n = 5) |>
  pull(`Partner Name`)

top5_import_data <- Import_Partner_long |>
  filter(`Partner Name` %in% top5_import)

highlight_import <- top5_import_data |>
  filter(Year %in% highlight_years)

ggplot(top5_import_data, aes(x = Year, y = Share, color = `Partner Name`)) +
  geom_line(linewidth = 1) +
  geom_point(data = highlight_import, size = 3) +
  geom_text(data = highlight_import, aes(label = Year), vjust = -1, size = 2.8) +
  labs(title = "Singapore Import Partner Share (%) - Top 5 Countries",
       x = "Year", y = "Import Partner Share (%)", color = "Partner Country") +
  theme_minimal()


Import Partner Composition

pie_import <- Import_Partner_Share |>
  pivot_longer(cols = -c(1:4), names_to = "Year", values_to = "Share") |>
  mutate(Year = as.numeric(Year)) |>
  filter(!is.na(Share), Share > 0,
         `Partner Name` %in% top5_import,
         Year %in% highlight_years)

available_import_years <- intersect(highlight_years, unique(pie_import$Year))

plots_import <- lapply(seq_along(available_import_years), function(i) {
  yr <- available_import_years[i]
  df <- pie_import |>
    filter(Year == yr) |>
    arrange(desc(Share)) |>
    mutate(pct = Share / sum(Share) * 100,
           label = paste0(round(pct, 1), "%"),
           pos = cumsum(pct) - pct / 2)
  p <- ggplot(df, aes(x = "", y = pct, fill = `Partner Name`)) +
    geom_col(width = 1, color = "white") +
    coord_polar(theta = "y") +
    geom_text(aes(y = pos, label = label), color = "white", size = 3, fontface = "bold") +
    labs(title = as.character(yr), fill = "Country") +
    theme_void() +
    theme(plot.title = element_text(hjust = 0.5, face = "bold", size = 12))
  if (i != length(available_import_years)) p <- p + theme(legend.position = "none")
  p
})

wrap_plots(plots_import, ncol = 4) +
  plot_annotation(title = "Singapore Import Partner Share - Top 5 Countries") +
  plot_layout(guides = "collect")