Data Import

Author

Grace Huang

options(repos = c(CRAN = "https://cran.rstudio.com/"))

library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library(readxl)
library(tidyr)
library(ggplot2)
library(purrr)


path_to_sheet <- file.path("C:/Users/Shiya (Grace) Huang/Desktop/24 Spring/sustainable finance/week 5 0228/data-raw", "CM_Data_Explorer.xlsx")

path_to_sheet
[1] "C:/Users/Shiya (Grace) Huang/Desktop/24 Spring/sustainable finance/week 5 0228/data-raw/CM_Data_Explorer.xlsx"
sheet_name <- "2.3 EV"

ev_data <- read_excel(path = path_to_sheet, sheet = sheet_name, skip = 4)
New names:
• `` -> `...1`
• `` -> `...3`
• `2025` -> `2025...4`
• `2030` -> `2030...5`
• `2035` -> `2035...6`
• `2040` -> `2040...7`
• `2045` -> `2045...8`
• `2050` -> `2050...9`
• `` -> `...10`
• `2025` -> `2025...11`
• `2030` -> `2030...12`
• `2035` -> `2035...13`
• `2040` -> `2040...14`
• `2045` -> `2045...15`
• `2050` -> `2050...16`
• `` -> `...17`
• `2025` -> `2025...18`
• `2030` -> `2030...19`
• `2035` -> `2035...20`
• `2040` -> `2040...21`
• `2045` -> `2045...22`
• `2050` -> `2050...23`
ev_data_tidy <- ev_data %>%
  mutate(row = row_number()) %>%
  mutate(Technology = case_when(
    row_number() >= 2 & row_number() <= 14 ~ "Constrained Nickel Supply",
    row_number() >= 17 & row_number() <= 29 ~ "Wider use of Silicon-rich Anodes",
    row_number() >= 32 & row_number() <= 44 ~ "Faster Uptake of Solid State Batteries",
    row_number() >= 47 & row_number() <= 59 ~ "Lower Battery Size"
  )) %>%
  filter(row %in% c(3,4,18,19,33,34,48,49)) %>%
  mutate(Material = case_when(
    row %in% c(3,18,33,48) ~ 'Copper',
    TRUE ~ 'Cobalt'
  )) %>%
  mutate(across(starts_with('20'), as.numeric, .names = "clean_{.col}"))

# Long format transformation
ev_data_long <- ev_data_tidy %>%
  pivot_longer(
    cols = starts_with("clean_"),  # Selecting cleaned year columns
    names_to = "Year",
    values_to = "Demand"
  ) %>%
  mutate(
    Year = readr::parse_number(Year),  # Correcting Year from 'Year_Scenario' to just 'Year'
    Scenario = case_when(
      grepl("scenario1", Year) ~ 'Stated policies scenario',
      grepl("scenario2", Year) ~ 'Announced pledges scenario',
      grepl("scenario3", Year) ~ 'Net Zero Emissions by 2050 scenario',
      TRUE ~ NA_character_
    )
  ) %>%
  select(-row)  # Removing the 'row' column from the final dataset

head(ev_data_tidy)
# A tibble: 6 × 45
  ...1   `2022` ...3  `2025...4` `2030...5` `2035...6` `2040...7` `2045...8`
  <chr>   <dbl> <lgl>      <dbl>      <dbl>      <dbl>      <dbl>      <dbl>
1 Copper  381.  NA         617.      1389.      1736.      2233.      2418. 
2 Cobalt   63.5 NA          61.2       38.0       29.1       35.4       40.9
3 Copper  373.  NA         580.      1044.      1219.      1421.      1458. 
4 Cobalt   64.6 NA          66.4       73.9       77.7      105.       134. 
5 Copper  373.  NA         580.      1044.      1219.      1421.      1458. 
6 Cobalt   64.6 NA          66.4       73.9       77.7      105.       134. 
# ℹ 37 more variables: `2050...9` <dbl>, ...10 <lgl>, `2025...11` <dbl>,
#   `2030...12` <dbl>, `2035...13` <dbl>, `2040...14` <dbl>, `2045...15` <dbl>,
#   `2050...16` <dbl>, ...17 <lgl>, `2025...18` <dbl>, `2030...19` <dbl>,
#   `2035...20` <dbl>, `2040...21` <dbl>, `2045...22` <dbl>, `2050...23` <dbl>,
#   row <int>, Technology <chr>, Material <chr>, clean_2022 <dbl>,
#   clean_2025...4 <dbl>, clean_2030...5 <dbl>, clean_2035...6 <dbl>,
#   clean_2040...7 <dbl>, clean_2045...8 <dbl>, clean_2050...9 <dbl>, …
##1. Demand Trends Over Time by Material and Technology:
ggplot(ev_data_long, aes(x = Year, y = Demand, color = Material, shape = Technology)) +
  geom_point() +  # Changed from geom_line() to geom_point()
  scale_color_manual(values = c("Copper" = "#FF5733", "Cobalt" = "#3498DB")) +
  labs(title = "Demand Trends Over Time by Material and Technology",
       x = "Year",
       y = "Demand (Metric Tons)",
       color = "Material",
       shape = "Technology") +  # Changed from linetype to shape for clarity in a point plot
  theme_minimal()

####2. Changes in the demand for copper and cobalt under different policy scenarios over time
ggplot(ev_data_long, aes(x = Year, y = Demand, color = Scenario)) +
  geom_line() +
  facet_wrap(~Technology, scales = 'free_y') +
  labs(title = "Demand for Copper and Cobalt Over Time by Policy Scenario",
       x = "Year", y = "Demand (metric tons)",
       color = "Policy Scenario") +
  theme_minimal() +
  scale_color_manual(values = c("Stated policies scenario" = "blue",
                                "Announced pledges scenario" = "red",
                                "Net Zero Emissions by 2050 scenario" = "green"))

# 3. Different technology sceneries

ggplot(ev_data_long, aes(x = Year, y = Demand, fill = Technology)) +
  geom_area(position = 'stack') +
  facet_wrap(~Scenario, scales = 'free_y') +
  labs(title = "Demand by Technology Scenario",
       x = "Year", y = "Demand (metric tons)",
       fill = "Technology Scenario") +
  theme_minimal() +
  scale_fill_brewer(palette = "Set2")

# 4. Demand Difference by Scenario and Technology
ggplot(ev_data_long, aes(x = Technology, y = Demand, fill = Scenario)) +
  geom_bar(stat = "identity", position = "dodge") +
  facet_wrap(~Year, scales = 'free_y') +
  labs(title = "Demand Difference by Scenario and Technology",
       x = "Technology", y = "Demand (metric tons)",
       fill = "Policy Scenario") +
  theme_minimal() +
  scale_fill_manual(values = c("Stated policies scenario" = "blue",
                               "Announced pledges scenario" = "red",
                               "Net Zero Emissions by 2050 scenario" = "green"))

# 5. Cumulative Demand Analysis
ev_data_cumulative <- ev_data_long %>%
  group_by(Year, Scenario) %>%
  summarise(Total_Demand = sum(Demand, na.rm = TRUE))
`summarise()` has grouped output by 'Year'. You can override using the
`.groups` argument.
ggplot(ev_data_cumulative, aes(x = Year, y = Total_Demand, fill = Scenario)) +
  geom_bar(stat = "identity") +
  labs(title = "Cumulative Demand Analysis",
       x = "Year", y = "Total Demand (metric tons)",
       fill = "Policy Scenario") +
  theme_minimal() +
  scale_fill_manual(values = c("Stated policies scenario" = "blue",
                               "Announced pledges scenario" = "red",
                               "Net Zero Emissions by 2050 scenario" = "green"))

##6. Combined Impact of Technology and Policy Scenarios on Copper and Cobalt Demand
ev_data_long %>%
  group_by(Year, Technology, Scenario) %>%
  summarise(Total_Demand = sum(Demand, na.rm = TRUE)) %>%
  ggplot(aes(x = Year, y = Total_Demand, color = Technology)) +
  geom_line() +
  facet_grid(Scenario ~ .) +
  labs(title = "Combined Impact of Technology and Policy Scenarios on Copper and Cobalt Demand",
       x = "Year", y = "Total Demand (metric tons)",
       color = "Technology") +
  theme_minimal()
`summarise()` has grouped output by 'Year', 'Technology'. You can override
using the `.groups` argument.

##Comparison of Copper and Cobalt Demand Across Policy Scenarios
ev_data_summary <- ev_data_long %>%
  group_by(Year, Scenario) %>%
  summarise(Total_Demand = sum(Demand, na.rm = TRUE), .groups = 'drop')  

ggplot(ev_data_summary, aes(x = Year, y = Total_Demand, fill = Scenario)) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Comparison of Copper and Cobalt Demand Across Policy Scenarios",
       x = "Year", y = "Total Demand (metric tons)",
       fill = "Policy Scenario") +
  theme_minimal() +
  scale_fill_manual(values = c("Stated policies scenario" = "blue",
                               "Announced pledges scenario" = "red",
                               "Net Zero Emissions by 2050 scenario" = "green"))