pacman::p_load(pacman, tidyverse, reshape2)

# Load the data
World_Energy_By_Region_1965_to_2023 <- read_csv("World_Energy_By_Country_And_Region_1965_to_2023.csv")
## Rows: 111 Columns: 60
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (60): Country, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 197...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# List of regions
regions <- c("Total North America", "Total S. & Cent. America", "Total Europe", "Total CIS", "Total Middle East", "Total Africa", "Total Asia Pacific", "Total World")

# Initialise an empty list to store plots for each region
plots_list <- list()

# Function to create plots for a given region
create_plots <- function(region_name, region_data) {
  
  # Remove the word 'Total' from the region name
  clean_region_name <- str_replace(region_name, "Total ", "")
  
  # Change the data to long format
  data_long <- melt(region_data, id.vars = "Country", variable.name = "Year", value.name = "Energy_EJ")
  
  # Convert Year to numeric
  data_long$Year <- as.numeric(as.character(data_long$Year))
  
  # Line plot of total energy consumption over the years for the region
  line_plot <- ggplot(data_long, aes(x = Year, y = Energy_EJ, color = Country)) +
    geom_line(linewidth = 1) +
    geom_point() +
    labs(title = paste("Total Energy Consumption:", clean_region_name, "(1965-2023)"),
         x = "Year",
         y = "Energy Consumption (Exajoules)") +
    theme_minimal()
  
  # Heatmap of energy consumption distribution over the years and for the region
  heatmap_plot <- ggplot(data_long, aes(x = Year, y = Country, fill = Energy_EJ)) +
    geom_tile(color = "white") +
    scale_fill_viridis_c(option = "C") +
    labs(title = paste("Heatmap of Energy Consumption:", clean_region_name, "(1965-2023)"),
         x = "Year",
         y = "Region",
         fill = "Energy (Exajoules)") +
    theme_minimal()
  
  list(line_plot = line_plot, heatmap_plot = heatmap_plot)
}

# Iterate through the regions and create plots
for (region in regions) {
  # Extract the region data
  region_data <- World_Energy_By_Region_1965_to_2023 %>%
    filter(str_detect(Country, str_replace(region, "Total ", "")))
  
  # Add the total row for the region
  region_data <- bind_rows(region_data, World_Energy_By_Region_1965_to_2023 %>% filter(Country == region))
  
  # Convert character columns to numeric, excluding the 'Country' column
  region_data <- region_data %>%
    mutate(across(-Country, ~parse_number(., na = c("-", "^"))))
  
  # Replace NA values with zero or handle them as needed
  region_data <- region_data %>%
    mutate(across(-Country, ~ifelse(is.na(.), 0, .)))
  
  # Create and store plots for the region
  plots_list[[region]] <- create_plots(region, region_data)
}

# Suppress warnings and print all plots
suppressWarnings({
  for (region_plots in plots_list) {
    print(region_plots$line_plot)
    print(region_plots$heatmap_plot)
  }
})