Created
June 22, 2025 08:44
-
-
Save thinkphp/915d9943aca15a9a3cff963057ea9b1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ============================================================================= | |
# PART 2: RENEWABLE ENERGY TRANSITION (25 points) | |
# ============================================================================= | |
# Load required libraries | |
library(readxl) | |
library(dplyr) | |
library(ggplot2) | |
library(gridExtra) | |
cat("PART 2: RENEWABLE ENERGY TRANSITION\n") | |
cat("===================================\n\n") | |
# Load energy mix data | |
energy_mix <- read_excel("energy_mix.xlsx") | |
# Question 1: Data quality issues | |
cat("Question 1: Missing data analysis\n") | |
missing_analysis <- energy_mix %>% | |
summarise( | |
renewable_missing = round(sum(is.na(renewable_pct)) / n() * 100, 1), | |
nuclear_missing = round(sum(is.na(nuclear_pct)) / n() * 100, 1), | |
coal_missing = round(sum(is.na(coal_pct)) / n() * 100, 1) | |
) | |
cat("Percentage of missing observations:\n") | |
cat("renewable_pct:", missing_analysis$renewable_missing, "%\n") | |
cat("nuclear_pct:", missing_analysis$nuclear_missing, "%\n") | |
cat("coal_pct:", missing_analysis$coal_missing, "%\n\n") | |
# For remaining questions, exclude missing values | |
energy_clean <- energy_mix %>% | |
filter(!is.na(renewable_pct) & !is.na(nuclear_pct) & !is.na(coal_pct)) | |
# Question 2: High renewable adoption by year | |
cat("Question 2: Percentage of high renewable adoption countries by year\n") | |
high_renewable_by_year <- energy_clean %>% | |
filter(year %in% c(2015, 2018, 2021, 2023)) %>% | |
group_by(year) %>% | |
summarise( | |
total_countries = n(), | |
high_renewable_countries = sum(renewable_pct > 50), | |
pct_high_renewable = round(high_renewable_countries / total_countries * 100, 1), | |
.groups = 'drop' | |
) | |
cat("Year | % High Renewable Countries\n") | |
for(i in 1:nrow(high_renewable_by_year)) { | |
cat(high_renewable_by_year$year[i], " | ", high_renewable_by_year$pct_high_renewable[i], "%\n") | |
} | |
cat("\n") | |
# Question 3: Correlation between renewable energy and economic development (2023) | |
cat("Question 3: Renewable energy vs economic development analysis (2023)\n") | |
energy_2023 <- energy_clean %>% | |
filter(year == 2023) %>% | |
filter(!is.na(gdp_per_capita)) | |
# Correlation coefficient | |
correlation_coef <- round(cor(energy_2023$renewable_pct, energy_2023$gdp_per_capita, use = "complete.obs"), 3) | |
cat("Correlation coefficient (renewable % vs GDP per capita):", correlation_coef, "\n") | |
# Divide into terciles | |
energy_2023_terciles <- energy_2023 %>% | |
mutate( | |
gdp_tercile = ntile(gdp_per_capita, 3), | |
income_level = case_when( | |
gdp_tercile == 1 ~ "Low Income", | |
gdp_tercile == 2 ~ "Middle Income", | |
gdp_tercile == 3 ~ "High Income" | |
) | |
) %>% | |
group_by(income_level) %>% | |
summarise(mean_renewable = round(mean(renewable_pct, na.rm = TRUE), 1), .groups = 'drop') %>% | |
arrange(match(income_level, c("Low Income", "Middle Income", "High Income"))) | |
cat("\nMean renewable percentage by income tercile:\n") | |
print(energy_2023_terciles) | |
cat("\n") | |
# Question 4: Stacked bar chart of global energy mix evolution | |
cat("Question 4: Creating stacked bar chart of global energy mix evolution\n") | |
# Calculate global averages for selected years | |
global_energy_mix <- energy_clean %>% | |
filter(year %in% c(2010, 2013, 2016, 2019, 2022, 2023)) %>% | |
group_by(year) %>% | |
summarise( | |
Fossil_Fuels = round(mean(fossil_fuel_pct, na.rm = TRUE), 1), | |
Renewables = round(mean(renewable_pct, na.rm = TRUE), 1), | |
Nuclear = round(mean(nuclear_pct, na.rm = TRUE), 1), | |
.groups = 'drop' | |
) %>% | |
tidyr::pivot_longer(cols = c(Fossil_Fuels, Renewables, Nuclear), | |
names_to = "Energy_Type", values_to = "Percentage") | |
# Create stacked bar chart | |
p2 <- ggplot(global_energy_mix, aes(x = factor(year), y = Percentage, fill = Energy_Type)) + | |
geom_bar(stat = "identity", position = "stack") + | |
scale_y_continuous(limits = c(0, 100), breaks = seq(0, 100, 20)) + | |
scale_fill_manual(values = c("Fossil_Fuels" = "#8B4513", "Renewables" = "#228B22", "Nuclear" = "#4169E1")) + | |
labs( | |
title = "Global Energy Mix Evolution (2010-2023)", | |
x = "Year", | |
y = "Percentage of Global Energy Mix (%)", | |
fill = "Energy Category" | |
) + | |
theme_minimal() + | |
theme(legend.position = "bottom") | |
print(p2) | |
cat("Stacked bar chart created showing energy mix evolution\n\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment