# Script encoding: UTF-8 # R version: 4.1.1 # Author: Emmanuel Aramendia # This script builds Figure B.1 from the main paper # Libraries --------------------------------------------------------------- library(dplyr) library(tidyr) library(stringr) library(IEATools) library(Recca) library(ggplot2) library(matsindf) library(matsbyname) library(forcats) # Creating IO matrices, YU method for q calculation----------------------------------------- IEA_io_mats_gma_YU_method <- IEA_data_aggregated_gma_psut %>% Recca::calc_io_mats(method_q_calculation = "sum_U_Y") # Defining list of regions country_list <- c("Brazil", "China", "EU27", "India", "Mexico", "Russian Federation", "United States", "Turkey") # Figuring out where does extracted crude oil flow to -------- # Preparing data frame for results collection reversed_crude_oil_mats_df <- tibble::tibble( Origin_Country = character(), Year = numeric(), Y_matrix = list(), R_matrix = list() ) # Iterating through each region for (region in country_list){ print(region) observation_to_add <- IEA_io_mats_gma_YU_method %>% dplyr::mutate( R_prime = matsbyname::select_cols_byname( R, retain_pattern = paste0(region) ) %>% matsbyname::select_cols_byname( retain_pattern = "Crude oil" ) ) %>% dplyr::mutate( Country = region ) %>% Recca::new_R_ps( R_prime = "R_prime" ) %>% dplyr::select(Year, Country, Y_prime, R_prime) %>% dplyr::rename( Y_matrix = Y_prime, R_matrix = R_prime, Origin_Country = Country ) reversed_crude_oil_mats_df <- dplyr::bind_rows( reversed_crude_oil_mats_df, observation_to_add ) } reversed_crude_oil_df <- tibble::tibble( Origin_Country = character(), Demanding_Country = character(), Demanding_Sector = character(), Demanded_Product = character(), Year = numeric(), Demanded_Energy = numeric() ) # Iterating trough each year for (i in seq(2000, 2017)){ to_add <- reversed_crude_oil_mats_df %>% dplyr::filter(Year == i) %>% dplyr::select(-R_matrix) %>% tidyr::pivot_longer(cols = Y_matrix, names_to = "matnames", values_to = "matvals") %>% matsindf::expand_to_tidy() %>% dplyr::filter(matvals != 0) %>% dplyr::select(-rowtypes, -coltypes, -matnames) %>% dplyr::rename( Demanding_Sector = colnames, Demanded_Product = rownames, Demanded_Energy = matvals ) %>% dplyr::mutate( Demanding_Country = stringr::str_extract(Demanding_Sector, "\\{.*\\}") %>% stringr::str_remove("\\{") %>% stringr::str_remove("\\}"), Demanding_Sector = stringr::str_remove(Demanding_Sector, "\\{.*\\}_"), Demanded_Product = stringr::str_remove(Demanded_Product, "\\{.*\\}_") ) reversed_crude_oil_df <- dplyr::bind_rows( reversed_crude_oil_df, to_add ) } # Plot by country of destination - Figure B.1 x11() reversed_crude_oil_df %>% dplyr::mutate( Demanding_Country = dplyr::case_when( Demanding_Country == "Brazil" ~ "Brazil", Demanding_Country == "China" ~ "China", Demanding_Country == "EU27" ~ "EU27", Demanding_Country == "India" ~ "India", Demanding_Country == "Mexico" ~ "Mexico", Demanding_Country == "Russian Federation" ~ "Russian Federation", Demanding_Country == "Turkey" ~ "Turkey", Demanding_Country == "RoW Middle East" ~ "Middle East", Demanding_Country == "United States" ~ "United States", TRUE ~ "Others" ) ) %>% dplyr::group_by(Origin_Country, Demanding_Country, Year) %>% dplyr::summarise( Demanded_Energy = sum(Demanded_Energy), ) %>% dplyr::group_by(Origin_Country, Year) %>% dplyr::mutate( Share_Demanded_Country = Demanded_Energy / sum(Demanded_Energy) ) %>% ggplot(aes(x = Year, y = Share_Demanded_Country)) + geom_area(aes(fill = Demanding_Country)) + facet_wrap(vars(Origin_Country), scale = "free_y", ncol = 4, nrow = 2) + theme_bw() + theme( legend.position = "bottom", strip.text = element_text(size = 12), legend.text = element_text(size = 11), axis.title = element_text(size = 13), axis.text = element_text(size = 11) ) + scale_fill_viridis_d(option = "turbo", begin = 0.02, end = 0.98) + ylab("Destination share of oil products") + labs(fill = "Demanding Country") ggsave("oil_products_destination_by_country.pdf", width = 11.8, height = 7.5)