library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.0 ✔ purrr 1.0.1
## ✔ tibble 3.1.8 ✔ dplyr 1.1.0
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.1.3 ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(readxl)
data1<-read.csv("E:/JHU/Second Semester/Sustainable Finance/Final project/Data/world energy statistics.csv")
data1 <- data1[,1:(ncol(data1)-2)]
data1 <- subset(data1, select = -c(1, 3, 5, 7))
data1
# Summarize total energy production by product and year
data1 <- data1 %>%
filter(Time != 2021)
total_production_by_product <- data1 %>%
filter(Flow == "Production") %>%
group_by(Product, Time) %>%
summarise(Total_Production = sum(Value, na.rm = TRUE)) %>%
arrange(Product, Time)
## `summarise()` has grouped output by 'Product'. You can override using the
## `.groups` argument.
# Bar plot of total energy production by product across years
ggplot(total_production_by_product, aes(x = Time, y = Total_Production, fill = Product)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Total Energy Production by Product",
x = "Year",
y = "Total Production",
fill = "Product") +
theme_minimal()+
scale_x_continuous(breaks = seq(min(data1$Time), max(data1$Time), by = 1))

# Line plot of energy production trends by product
ggplot(total_production_by_product, aes(x = Time, y = Total_Production, color = Product, group = Product)) +
geom_line(size = 1) +
geom_point(size = 2) +
labs(title = "Energy Production Trends by Product",
x = "Year",
y = "Total Production",
color = "Product") +
theme_minimal()+
scale_x_continuous(breaks = seq(min(data1$Time), max(data1$Time), by = 1))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.

ggplot(total_production_by_product, aes(x = Time, y = Total_Production, fill = Product)) +
geom_area(position = "stack") +
labs(title = "Energy Production by Product",
x = "Year",
y = "Total Production",
fill = "Product") +
theme_minimal()+
scale_x_continuous(breaks = seq(min(data1$Time), max(data1$Time), by = 1))

total_production_by_year <- total_production_by_product %>%
group_by(Time) %>%
summarise(Total_Production = sum(Total_Production, na.rm = TRUE))
percentage_production_by_product <- merge(total_production_by_product, total_production_by_year, by = "Time") %>%
mutate(Percentage = (Total_Production.x / Total_Production.y) * 100)
ggplot(percentage_production_by_product, aes(x = Time, y = Percentage, fill = Product)) +
geom_bar(stat = "identity") +
labs(title = "Proportion of Total Energy Production by Product",
x = "Year",
y = "Percentage of Total Production",
fill = "Product") +
theme_minimal()+
scale_x_continuous(breaks = seq(min(data1$Time), max(data1$Time), by = 1))

# Install required packages
# install.packages("rworldmap")
library(rworldmap)
## Warning: 程辑包'rworldmap'是用R版本4.2.3 来建造的
## 载入需要的程辑包:sp
## Warning: 程辑包'sp'是用R版本4.2.3 来建造的
## ### Welcome to rworldmap ###
## For a short introduction type : vignette('rworldmap')
library(countrycode)
## Warning: 程辑包'countrycode'是用R版本4.2.3 来建造的
year <- 2019 # Replace with desired year
product_name <- "Natural gas (TJ-gross)" # Replace with desired product name
energy_data_by_flow <- data1 %>%
filter(Time == year, Product == product_name) %>%
group_by(Country, Flow) %>%
summarise(Total = sum(Value, na.rm = TRUE)) %>%
mutate(Code = countrycode(Country, "country.name", "iso3c"))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `Code = countrycode(Country, "country.name", "iso3c")`.
## ℹ In group 31: `Country = "Republic of Turkiye"`.
## Caused by warning in `countrycode_convert()`:
## ! Some values were not matched unambiguously: Republic of Turkiye
## ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 1 remaining warning.
# Function to create maps for each flow type
create_map <- function(flow_type) {
map_data <- energy_data_by_flow %>%
filter(Flow == flow_type) %>%
select(Code, Total)
world_map <- joinCountryData2Map(map_data, joinCode = "ISO3", nameJoinColumn = "Code")
map <- mapCountryData(world_map, nameColumnToPlot = "Total",
mapTitle = paste(flow_type, "of", product_name, "in", year),
catMethod = "fixedWidth",
colourPalette = "heat",
addLegend = TRUE)
return(map)
}
# Create maps for Production, Imports, and Exports
prod_map <- create_map("Production")
## Adding missing grouping variables: `Country`
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data

imports_map <- create_map("Imports")
## Adding missing grouping variables: `Country`
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data

exports_map <- create_map("Exports")
## Adding missing grouping variables: `Country`
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data

# Display maps
par(mfrow = c(1, 3))
prod_map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] 0 5316120 10632240 15948360 21264481 26580601 31896721 37212841
##
## $plottedData
## [1] NA NA NA NA NA
## [6] NA NA NA 5486983.346 35818.714
## [11] NA NA 135.800 NA NA
## [16] NA NA NA NA NA
## [21] NA NA NA NA NA
## [26] NA NA 7385471.000 0.000 59691.831
## [31] NA NA NA NA NA
## [36] 486361.660 0.000 NA NA NA
## [41] 8026.465 203459.838 NA 128600.424 NA
## [46] NA NA NA NA 5406.000
## [51] 0.000 NA 0.000 NA NA
## [56] 667.553 NA 1565217.000 NA NA
## [61] NA NA NA NA 444.732
## [66] NA NA NA NA NA
## [71] NA 61786.000 NA NA 99717.695
## [76] NA NA 0.000 336263.261 182876.813
## [81] NA NA 97324.893 NA NA
## [86] NA NA 9990.244 NA NA
## [91] NA NA NA NA NA
## [96] NA 0.000 0.000 0.000 NA
## [101] NA NA 1275644.000 NA NA
## [106] NA NA NA NA NA
## [111] NA NA NA NA NA
## [116] NA NA 1113863.711 4692660.632 NA
## [121] 185498.059 NA NA NA NA
## [126] NA NA 159408.050 NA NA
## [131] 0.000 NA NA 381054.431 NA
## [136] NA NA NA NA NA
## [141] NA NA NA NA NA
## [146] NA NA 4795.000 273.284 0.000
## [151] NA NA NA NA NA
## [156] NA NA NA NA NA
## [161] NA NA NA NA NA
## [166] NA 37212841.000 NA NA NA
## [171] NA NA NA NA NA
## [176] NA NA NA NA NA
## [181] NA NA NA NA NA
## [186] NA NA NA NA NA
## [191] NA NA NA NA NA
## [196] NA NA NA NA NA
## [201] NA NA NA NA NA
## [206] NA NA NA NA NA
## [211] NA NA NA NA NA
## [216] NA NA NA NA NA
## [221] NA NA NA NA NA
## [226] NA NA NA NA NA
## [231] NA NA NA NA NA
## [236] NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
imports_map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] 0.0 597923.8 1195847.5 1793771.3 2391695.0 2989618.8 3587542.5
## [8] 4185466.3
##
## $plottedData
## [1] NA NA NA NA NA NA
## [7] NA NA 201980.000 547204.000 NA NA
## [13] 894029.000 NA NA NA NA NA
## [19] NA NA NA NA NA NA
## [25] NA NA NA 1017915.000 136237.633 187447.338
## [31] NA NA NA NA NA 6072.350
## [37] 0.000 NA NA NA 365476.390 3520264.903
## [43] NA 46506.113 NA NA NA NA
## [49] NA 1506011.000 18642.000 NA 99553.000 NA
## [55] NA 2275007.484 NA 1840221.000 NA NA
## [61] NA NA NA NA 207357.660 NA
## [67] NA NA NA NA NA 724785.000
## [73] NA NA 112418.229 NA NA 0.000
## [79] 28993.800 2707586.218 NA NA 4185466.273 NA
## [85] NA NA NA 2241984.804 NA NA
## [91] NA NA NA NA NA NA
## [97] 106877.000 31822.352 51343.214 NA NA NA
## [103] 1908243.000 NA NA NA NA NA
## [109] NA NA NA NA NA NA
## [115] NA NA NA 1976603.951 1057.288 NA
## [121] 0.000 NA NA NA NA NA
## [127] NA 672988.156 NA NA 245614.868 NA
## [133] NA 99372.600 NA NA NA NA
## [139] NA NA NA NA NA NA
## [145] NA NA NA 259722.000 34026.579 45694.733
## [151] NA NA NA NA NA NA
## [157] NA NA NA NA NA NA
## [163] NA NA NA NA 2949766.984 NA
## [169] NA NA NA NA NA NA
## [175] NA NA NA NA NA NA
## [181] NA NA NA NA NA NA
## [187] NA NA NA NA NA NA
## [193] NA NA NA NA NA NA
## [199] NA NA NA NA NA NA
## [205] NA NA NA NA NA NA
## [211] NA NA NA NA NA NA
## [217] NA NA NA NA NA NA
## [223] NA NA NA NA NA NA
## [229] NA NA NA NA NA NA
## [235] NA NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
exports_map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] -5099577 -4371066 -3642555 -2914044 -2185533 -1457022 -728511 0
##
## $plottedData
## [1] NA NA NA NA NA
## [6] NA NA NA -4095656.568 -108491.126
## [11] NA NA -174003.600 NA NA
## [16] NA NA NA NA NA
## [21] NA NA NA NA NA
## [26] NA NA -2991778.000 0.000 0.000
## [31] NA NA NA NA NA
## [36] 0.000 0.000 NA NA NA
## [41] 0.000 0.000 NA -54935.694 NA
## [46] NA NA NA NA -45514.000
## [51] 0.000 NA 0.000 NA NA
## [56] -450534.089 NA -325420.000 NA NA
## [61] NA NA NA NA -634.153
## [66] NA NA NA NA NA
## [71] NA -271148.000 NA NA 0.000
## [76] NA NA 0.000 -7057.084 -12399.265
## [81] NA NA 0.000 NA NA
## [86] NA NA 0.000 NA NA
## [91] NA NA NA NA NA
## [96] NA -20175.000 0.000 0.000 NA
## [101] NA NA -436.000 NA NA
## [106] NA NA NA NA NA
## [111] NA NA NA NA NA
## [116] NA NA -1589052.336 -4462084.721 NA
## [121] 0.000 NA NA NA NA
## [126] NA NA -51161.118 NA NA
## [131] 0.000 NA NA -487.027 NA
## [136] NA NA NA NA NA
## [141] NA NA NA NA NA
## [146] NA NA 0.000 -79.413 -1038.985
## [151] NA NA NA NA NA
## [156] NA NA NA NA NA
## [161] NA NA NA NA NA
## [166] NA -5099577.177 NA NA NA
## [171] NA NA NA NA NA
## [176] NA NA NA NA NA
## [181] NA NA NA NA NA
## [186] NA NA NA NA NA
## [191] NA NA NA NA NA
## [196] NA NA NA NA NA
## [201] NA NA NA NA NA
## [206] NA NA NA NA NA
## [211] NA NA NA NA NA
## [216] NA NA NA NA NA
## [221] NA NA NA NA NA
## [226] NA NA NA NA NA
## [231] NA NA NA NA NA
## [236] NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
# Install required packages
# install.packages("rworldmap")
library(rworldmap)
year <- 2020 # Replace with desired year
product_name <- "Natural gas (TJ-gross)" # Replace with desired product name
energy_data_by_flow <- data1 %>%
filter(Time == year, Product == product_name) %>%
group_by(Country, Flow) %>%
summarise(Total = sum(Value, na.rm = TRUE)) %>%
mutate(Code = countrycode(Country, "country.name", "iso3c"))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `Code = countrycode(Country, "country.name", "iso3c")`.
## ℹ In group 31: `Country = "Republic of Turkiye"`.
## Caused by warning in `countrycode_convert()`:
## ! Some values were not matched unambiguously: Republic of Turkiye
## ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 1 remaining warning.
# Function to create maps for each flow type
create_map <- function(flow_type) {
map_data <- energy_data_by_flow %>%
filter(Flow == flow_type) %>%
select(Code, Total)
world_map <- joinCountryData2Map(map_data, joinCode = "ISO3", nameJoinColumn = "Code")
map <- mapCountryData(world_map, nameColumnToPlot = "Total",
mapTitle = paste(flow_type, "of", product_name, "in", year),
catMethod = "fixedWidth",
colourPalette = "heat",
addLegend = TRUE)
return(map)
}
# Create maps for Production, Imports, and Exports
prod_map <- create_map("Production")
## Adding missing grouping variables: `Country`
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data

imports_map <- create_map("Imports")
## Adding missing grouping variables: `Country`
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data

exports_map <- create_map("Exports")
## Adding missing grouping variables: `Country`
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data

# Display maps
par(mfrow = c(1, 3))
prod_map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] 0 5235617 10471234 15706851 20942469 26178086 31413703 36649320
##
## $plottedData
## [1] NA NA NA NA NA
## [6] NA NA NA 5923572.381 29429.816
## [11] NA NA 211.200 NA NA
## [16] NA NA NA NA NA
## [21] NA NA NA NA NA
## [26] NA NA 7209027.000 0.000 45717.294
## [31] NA NA NA NA NA
## [36] 473372.100 0.000 NA NA NA
## [41] 7565.606 187514.697 NA 55402.936 NA
## [46] NA NA NA NA 1939.519
## [51] 0.000 NA 0.000 NA NA
## [56] 724.474 NA 1577988.000 NA NA
## [61] NA NA NA NA 310.951
## [66] NA NA NA NA NA
## [71] NA 61423.000 NA NA 76823.972
## [76] NA NA 0.000 495174.510 152921.741
## [81] NA NA 90328.512 NA NA
## [86] NA NA 7711.435 NA NA
## [91] NA NA NA NA NA
## [96] NA 0.000 0.000 0.000 NA
## [101] NA NA 1264563.000 NA NA
## [106] NA NA NA NA NA
## [111] NA NA NA NA NA
## [116] NA NA 802972.991 4560754.645 NA
## [121] 181094.198 NA NA NA NA
## [126] NA NA 158002.414 NA NA
## [131] 0.000 NA NA 340414.329 NA
## [136] NA NA NA NA NA
## [141] NA NA NA NA NA
## [146] NA NA 2544.000 213.331 0.000
## [151] NA NA NA NA NA
## [156] NA NA NA NA NA
## [161] NA NA NA NA NA
## [166] NA 36649319.960 NA NA NA
## [171] NA NA NA NA NA
## [176] NA NA NA NA NA
## [181] NA NA NA NA NA
## [186] NA NA NA NA NA
## [191] NA NA NA NA NA
## [196] NA NA NA NA NA
## [201] NA NA NA NA NA
## [206] NA NA NA NA NA
## [211] NA NA NA NA NA
## [216] NA NA NA NA NA
## [221] NA NA NA NA NA
## [226] NA NA NA NA NA
## [231] NA NA NA NA NA
## [236] NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
imports_map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] 0.0 596983.3 1193966.6 1790950.0 2387933.3 2984916.6 3581899.9
## [8] 4178883.3
##
## $plottedData
## [1] NA NA NA NA NA NA
## [7] NA NA 179742.000 636210.000 NA NA
## [13] 832496.400 NA NA NA NA NA
## [19] NA NA NA NA NA NA
## [25] NA NA NA 923050.000 132589.868 176962.260
## [31] NA NA NA NA NA 13531.000
## [37] 0.000 NA NA NA 291223.427 3092272.826
## [43] NA 103034.296 NA NA NA NA
## [49] NA 1314814.000 17202.000 NA 98580.000 NA
## [55] NA 1917688.977 NA 1721478.000 NA NA
## [61] NA NA NA NA 232184.513 NA
## [67] NA NA NA NA NA 473860.000
## [73] NA NA 135059.000 NA NA 0.000
## [79] 21487.490 2529566.823 NA NA 4178883.262 NA
## [85] NA NA NA 2235706.369 NA NA
## [91] NA NA NA NA NA NA
## [97] 110833.000 28901.447 42387.034 NA NA NA
## [103] 2457266.000 NA NA NA NA NA
## [109] NA NA NA NA NA NA
## [115] NA NA NA 1993689.094 1325.902 NA
## [121] 0.000 NA NA NA NA NA
## [127] NA 673200.874 NA NA 238821.528 NA
## [133] NA 79479.534 NA NA NA NA
## [139] NA NA NA NA NA NA
## [145] NA NA NA 167459.000 34003.863 60377.932
## [151] NA NA NA NA NA NA
## [157] NA NA NA NA NA NA
## [163] NA NA NA NA 2745216.090 NA
## [169] NA NA NA NA NA NA
## [175] NA NA NA NA NA NA
## [181] NA NA NA NA NA NA
## [187] NA NA NA NA NA NA
## [193] NA NA NA NA NA NA
## [199] NA NA NA NA NA NA
## [205] NA NA NA NA NA NA
## [211] NA NA NA NA NA NA
## [217] NA NA NA NA NA NA
## [223] NA NA NA NA NA NA
## [229] NA NA NA NA NA NA
## [235] NA NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
exports_map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] -5781002.2 -4955144.7 -4129287.3 -3303429.8 -2477572.4 -1651714.9 -825857.5
## [8] 0.0
##
## $plottedData
## [1] NA NA NA NA NA
## [6] NA NA NA -4338570.861 -388058.757
## [11] NA NA -134702.900 NA NA
## [16] NA NA NA NA NA
## [21] NA NA NA NA NA
## [26] NA NA -2769347.000 0.000 0.000
## [31] NA NA NA NA NA
## [36] 0.000 0.000 NA NA NA
## [41] 0.000 0.000 NA -66268.204 NA
## [46] NA NA NA NA -47433.000
## [51] 0.000 NA 0.000 NA NA
## [56] -380089.768 NA -381251.000 NA NA
## [61] NA NA NA NA -1320.113
## [66] NA NA NA NA NA
## [71] NA -165534.000 NA NA 0.000
## [76] NA NA 0.000 -136301.270 -12022.836
## [81] NA NA 0.000 NA NA
## [86] NA NA 0.000 NA NA
## [91] NA NA NA NA NA
## [96] NA -20089.000 0.000 0.000 NA
## [101] NA NA -36.000 NA NA
## [106] NA NA NA NA NA
## [111] NA NA NA NA NA
## [116] NA NA -1332715.980 -4348404.023 NA
## [121] 0.000 NA NA NA NA
## [126] NA NA -53832.745 NA NA
## [131] 0.000 NA NA -5310.054 NA
## [136] NA NA NA NA NA
## [141] NA NA NA NA NA
## [146] NA NA 0.000 0.000 -1161.934
## [151] NA NA NA NA NA
## [156] NA NA NA NA NA
## [161] NA NA NA NA NA
## [166] NA -5781002.177 NA NA NA
## [171] NA NA NA NA NA
## [176] NA NA NA NA NA
## [181] NA NA NA NA NA
## [186] NA NA NA NA NA
## [191] NA NA NA NA NA
## [196] NA NA NA NA NA
## [201] NA NA NA NA NA
## [206] NA NA NA NA NA
## [211] NA NA NA NA NA
## [216] NA NA NA NA NA
## [221] NA NA NA NA NA
## [226] NA NA NA NA NA
## [231] NA NA NA NA NA
## [236] NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
year <- 2019 # Replace with desired year
product_name <- "Natural gas (TJ-gross)" # Replace with desired product name
top5_countries <- data1 %>%
filter(Time == year, Product == product_name, Flow == "Production") %>%
group_by(Country) %>%
summarise(Total_Production = sum(Value, na.rm = TRUE)) %>%
top_n(5, wt = Total_Production) %>%
pull(Country)
top5_data <- data1 %>%
filter(Country %in% top5_countries, Product == product_name, Time != 2021) %>%
group_by(Country, Flow, Time) %>%
summarise(Total = sum(Value, na.rm = TRUE))
## `summarise()` has grouped output by 'Country', 'Flow'. You can override using
## the `.groups` argument.
ggplot(top5_data, aes(x = Time, y = Total, color = Country, group = Country)) +
facet_wrap(~Flow) +
geom_line(size = 1) +
geom_point(size = 2) +
labs(title = paste("Energy Flow Trends for Top 5 Countries in", product_name),
x = "Year",
y = "Total",
color = "Country") +
theme_minimal() +
scale_x_continuous(breaks = seq(min(data1$Time), max(data1$Time), by = 1))

total_by_flow_year <- data1 %>%
group_by(Flow, Time) %>%
summarise(Total = sum(Value, na.rm = TRUE))
## `summarise()` has grouped output by 'Flow'. You can override using the
## `.groups` argument.
ggplot(total_by_flow_year, aes(x = Time, y = Total, fill = Flow)) +
geom_bar(stat = "identity") +
labs(title = "Total Energy Production, Imports, and Exports by Year",
x = "Year",
y = "Total",
fill = "Flow") +
theme_minimal() +
scale_x_continuous(breaks = seq(min(data1$Time), max(data1$Time), by = 1))

# Install required packages
# install.packages("gganimate")
# Install required packages
# install.packages("gifski")
library(gifski)
## Warning: 程辑包'gifski'是用R版本4.2.3 来建造的
library(gganimate)
## Warning: 程辑包'gganimate'是用R版本4.2.3 来建造的
animation <- ggplot(total_production_by_product, aes(x = Product, y = Total_Production, fill = Product)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Total Energy Production by Product in {frame_time}",
x = "Product",
y = "Total Production",
fill = "Product") +
theme_minimal() +
transition_time(Time) +
ease_aes('linear')
animate(animation, renderer = gifski_renderer("total_energy_production_by_product.gif"))

library(magick)
## Warning: 程辑包'magick'是用R版本4.2.3 来建造的
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
gif_image1 <- image_read("total_energy_production_by_product.gif")
image_animate(gif_image1)

library(dplyr)
library(gganimate)
product_name <- "Natural gas (TJ-gross)" # Replace with the desired product name
# Prepare the data
top5_data <- data1 %>%
filter(Flow == "Production", Time != 2021, Product == product_name, Country != "World") %>%
group_by(Country, Time) %>%
summarise(Total_Production = sum(Value, na.rm = TRUE)) %>%
arrange(desc(Total_Production))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
# Get the top 5 countries for each year
top5_countries_by_year <- top5_data %>%
group_by(Time) %>%
top_n(5, wt = Total_Production)
# Create the animation
ggplot(top5_countries_by_year, aes(x = reorder(Country, Total_Production), y = Total_Production, fill = Country)) +
geom_bar(stat = "identity") +
labs(title = paste("Top 5 Countries in", product_name, "Production in {frame_time}"),
x = "Country",
y = "Total Production",
fill = "Country") +
theme_minimal() +
transition_time(Time) +
ease_aes('linear') -> top5_animation
animate(top5_animation, renderer = gifski_renderer("top5_energy_production_over_time.gif"))

gif_image2 <- image_read("top5_energy_production_over_time.gif")
image_animate(gif_image2)

top5_total_energy <- data1 %>%
filter(Flow == "Production", Time != 2021, Country != "World") %>%
group_by(Country, Time) %>%
summarise(Total_Production = sum(Value, na.rm = TRUE)) %>%
arrange(desc(Total_Production)) %>%
group_by(Time) %>%
top_n(5, wt = Total_Production)
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
top5_total_energy_countries <- unique(top5_total_energy$Country)
filtered_data <- data1 %>%
filter(Flow == "Production", Time != 2021, Country %in% top5_total_energy_countries) %>%
group_by(Country, Time, Product) %>%
summarise(Total_Production = sum(Value, na.rm = TRUE))
## `summarise()` has grouped output by 'Country', 'Time'. You can override using
## the `.groups` argument.
energy_share <- filtered_data %>%
group_by(Country, Time) %>%
mutate(Total_Country_Production = sum(Total_Production)) %>%
ungroup() %>%
mutate(Share = Total_Production / Total_Country_Production * 100)
# Create the animation
ggplot(energy_share, aes(x = Product, y = Share, fill = Product)) +
geom_bar(stat = "identity", position = "dodge") +
facet_wrap(~Country) +
labs(title = "Energy Production Share by Product in Top 5 Countries ({frame_time})",
x = "Product",
y = "Share (%)",
fill = "Product") +
theme_minimal() +
transition_time(Time) +
ease_aes('linear') -> energy_share_animation
animate(energy_share_animation, renderer = gifski_renderer("energy_production_share_top5_countries.gif"))

gif_image3 <- image_read("energy_production_share_top5_countries.gif")
image_animate(gif_image3)

#Energy self-sufficiency (Production / (Production + Imports))
year <- 2019 # Replace with desired year
product_name <- "Natural gas (TJ-gross)" # Replace with desired product name
energy_self_sufficiency <- data1 %>%
filter(Time == year, Product == product_name, Flow %in% c("Production", "Imports")) %>%
group_by(Country, Flow) %>%
summarise(Total = sum(Value, na.rm = TRUE)) %>%
spread(Flow, Total) %>%
mutate(Self_Sufficiency = Production / (Production + Imports)) %>%
mutate(Code = countrycode(Country, "country.name", "iso3c"))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `Code = countrycode(Country, "country.name", "iso3c")`.
## ℹ In group 31: `Country = "Republic of Turkiye"`.
## Caused by warning in `countrycode_convert()`:
## ! Some values were not matched unambiguously: Republic of Turkiye
## ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 1 remaining warning.
map_data <- energy_self_sufficiency %>%
select(Code, Self_Sufficiency)
## Adding missing grouping variables: `Country`
world_map <- joinCountryData2Map(map_data, joinCode = "ISO3", nameJoinColumn = "Code")
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data
map <- mapCountryData(world_map, nameColumnToPlot = "Self_Sufficiency",
mapTitle = paste("Energy Self-Sufficiency in", product_name, "in", year),
catMethod = "fixedWidth",
colourPalette = "heat",
addLegend = TRUE)

map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] 0.0000000 0.1428571 0.2857143 0.4285714 0.5714286 0.7142857 0.8571429
## [8] 1.0000000
##
## $plottedData
## [1] NA NA NA NA NA
## [6] NA NA NA 0.9644961678 0.0614362239
## [11] NA NA 0.0001518736 NA NA
## [16] NA NA NA NA NA
## [21] NA NA NA NA NA
## [26] NA NA 0.8788684704 0.0000000000 0.2415312443
## [31] NA NA NA NA NA
## [36] 0.9876687031 NaN NA NA NA
## [41] 0.0214897019 0.0546387964 NA 0.7344124680 NA
## [46] NA NA NA NA 0.0035767760
## [51] 0.0000000000 NA 0.0000000000 NA NA
## [56] 0.0002933428 NA 0.4596228150 NA NA
## [61] NA NA NA NA 0.0021401679
## [66] NA NA NA NA NA
## [71] NA 0.0785510780 NA NA 0.4700651032
## [76] NA NA NaN 0.9206208364 0.0632690372
## [81] NA NA 0.0227246413 NA NA
## [86] NA NA 0.0044362143 NA NA
## [91] NA NA NA NA NA
## [96] NA 0.0000000000 0.0000000000 0.0000000000 NA
## [101] NA NA 0.4006561791 NA NA
## [106] NA NA NA NA NA
## [111] NA NA NA NA NA
## [116] NA NA 0.3604191446 0.9997747440 NA
## [121] 1.0000000000 NA NA NA NA
## [126] NA NA 0.1915050175 NA NA
## [131] 0.0000000000 NA NA 0.7931577668 NA
## [136] NA NA NA NA NA
## [141] NA NA NA NA NA
## [146] NA NA 0.0181273793 0.0079674954 0.0000000000
## [151] NA NA NA NA NA
## [156] NA NA NA NA NA
## [161] NA NA NA NA NA
## [166] NA 0.9265543964 NA NA NA
## [171] NA NA NA NA NA
## [176] NA NA NA NA NA
## [181] NA NA NA NA NA
## [186] NA NA NA NA NA
## [191] NA NA NA NA NA
## [196] NA NA NA NA NA
## [201] NA NA NA NA NA
## [206] NA NA NA NA NA
## [211] NA NA NA NA NA
## [216] NA NA NA NA NA
## [221] NA NA NA NA NA
## [226] NA NA NA NA NA
## [231] NA NA NA NA NA
## [236] NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
year <- 2019 # Replace with desired year
product_name <- "Natural gas (TJ-gross)" # Replace with desired product name
energy_imports_percentage <- data1 %>%
filter(Time == year, Product == product_name) %>%
group_by(Country, Flow) %>%
summarise(Total = sum(Value, na.rm = TRUE)) %>%
spread(Flow, Total) %>%
mutate(Consumption = Production + Imports - Exports) %>%
mutate(Imports_Percentage = Imports / Consumption * 100) %>%
mutate(Code = countrycode(Country, "country.name", "iso3c"))
## `summarise()` has grouped output by 'Country'. You can override using the
## `.groups` argument.
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `Code = countrycode(Country, "country.name", "iso3c")`.
## ℹ In group 31: `Country = "Republic of Turkiye"`.
## Caused by warning in `countrycode_convert()`:
## ! Some values were not matched unambiguously: Republic of Turkiye
## ℹ Run ]8;;ide:run:dplyr::last_dplyr_warnings()dplyr::last_dplyr_warnings()]8;; to see the 1 remaining warning.
map_data <- energy_imports_percentage %>%
select(Code, Imports_Percentage)
## Adding missing grouping variables: `Country`
world_map <- joinCountryData2Map(map_data, joinCode = "ISO3", nameJoinColumn = "Code")
## 38 codes from your data successfully matched countries in the map
## 2 codes from your data failed to match with a country code in the map
## 205 codes from the map weren't represented in your data
map <- mapCountryData(world_map, nameColumnToPlot = "Imports_Percentage",
mapTitle = paste("Energy Imports as a Percentage of Consumption in", product_name, "in", year),
catMethod = "fixedWidth",
colourPalette = "heat",
addLegend = TRUE)

map
## $colourVector
## [1] "#FFFF80" "#FFFF00" "#FFCC00" "#FF9900" "#FF6600" "#FF3300" "#FF0000"
##
## $cutVector
## [1] 0.00000 14.28571 28.57143 42.85714 57.14286 71.42857 85.71429
## [8] 100.00000
##
## $plottedData
## [1] NA NA NA NA NA
## [6] NA NA NA 2.06426005 79.13131572
## [11] NA NA 83.69738330 NA NA
## [16] NA NA NA NA NA
## [21] NA NA NA NA NA
## [26] NA NA 8.93286836 100.00000000 75.84687557
## [31] NA NA NA NA NA
## [36] 1.23312969 NaN NA NA NA
## [41] 97.85102981 94.53612036 NA 20.21633715 NA
## [46] NA NA NA NA 96.72946328
## [51] 100.00000000 NA 100.00000000 NA NA
## [56] 83.44948531 NA 49.32433773 NA NA
## [61] NA NA NA NA 99.48239163
## [66] NA NA NA NA NA
## [71] NA 68.52339799 NA NA 52.99348968
## [76] NA NA NaN 7.78745594 93.27298170
## [81] NA NA 97.72753587 NA NA
## [86] NA NA 99.55637857 NA NA
## [91] NA NA NA NA NA
## [96] NA 84.12067500 100.00000000 100.00000000 NA
## [101] NA NA 59.92617583 NA NA
## [106] NA NA NA NA NA
## [111] NA NA NA NA NA
## [116] NA NA 42.23945943 0.01154774 NA
## [121] 0.00000000 NA NA NA NA
## [126] NA NA 76.16802416 NA NA
## [131] 100.00000000 NA NA 20.66327618 NA
## [136] NA NA NA NA NA
## [141] NA NA NA NA NA
## [146] NA NA 98.18726207 98.97409998 97.77679790
## [151] NA NA NA NA NA
## [156] NA NA NA NA NA
## [161] NA NA NA NA NA
## [166] NA 6.51706711 NA NA NA
## [171] NA NA NA NA NA
## [176] NA NA NA NA NA
## [181] NA NA NA NA NA
## [186] NA NA NA NA NA
## [191] NA NA NA NA NA
## [196] NA NA NA NA NA
## [201] NA NA NA NA NA
## [206] NA NA NA NA NA
## [211] NA NA NA NA NA
## [216] NA NA NA NA NA
## [221] NA NA NA NA NA
## [226] NA NA NA NA NA
## [231] NA NA NA NA NA
## [236] NA NA NA NA NA
## [241] NA NA NA
##
## $catMethod
## [1] "fixedWidth"
##
## $colourPalette
## [1] "heat"
data2<-read.csv("E:/JHU/Second Semester/Sustainable Finance/Final project/Data/Allocation of emissions from electricity and heat.csv")
data2 <- data2[,1:(ncol(data2)-2)]
data2 <- subset(data2, select = -c(1, 3, 5, 7))
data2