Homework 5

options(repos = c(CRAN = "https://cran.rstudio.com/"))
library(tidyverse) 
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
install.packages("readxl")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//Rtmpp9iRBD/downloaded_packages
install.packages("here")

The downloaded binary packages are in
    /var/folders/25/scx22j0150v_7hw79jvjm8gh0000gn/T//Rtmpp9iRBD/downloaded_packages
library(here)
here() starts at /Users/palesamnguni/Desktop/Courses/Spring Term/Sustainable Fin/Sus Finance HW/Homework 5
library(readxl)
library(janitor)

Attaching package: 'janitor'

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

    chisq.test, fisher.test
library(ggplot2)

4.4.4.1 Creating a function to read in our worksheet

path_to_sheet <- here("data-raw", "CM_Data_Explorer.xlsx")
path_to_sheet
[1] "/Users/palesamnguni/Desktop/Courses/Spring Term/Sustainable Fin/Sus Finance HW/Homework 5/data-raw/CM_Data_Explorer.xlsx"
read_key_minerals_sheet <- partial(
  # provide the function, in this case readxl::read_excel()
  .f = read_excel,
  # provide any arguments you want filled in
  path = path_to_sheet,
  sheet = "2.3 EV",
  col_names = FALSE
)

# now all we have to do is provide the range
sheet_header <- read_key_minerals_sheet(range = "A4:W5")
New names:
• `` -> `...1`
• `` -> `...2`
• `` -> `...3`
• `` -> `...4`
• `` -> `...5`
• `` -> `...6`
• `` -> `...7`
• `` -> `...8`
• `` -> `...9`
• `` -> `...10`
• `` -> `...11`
• `` -> `...12`
• `` -> `...13`
• `` -> `...14`
• `` -> `...15`
• `` -> `...16`
• `` -> `...17`
• `` -> `...18`
• `` -> `...19`
• `` -> `...20`
• `` -> `...21`
• `` -> `...22`
• `` -> `...23`
sheet_header
# A tibble: 2 × 23
  ...1   ...2 ...3  ...4    ...5  ...6  ...7  ...8  ...9 ...10 ...11 ...12 ...13
  <lgl> <dbl> <lgl> <chr>  <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <chr> <dbl> <dbl>
1 NA       NA NA    State…    NA    NA    NA    NA    NA NA    Anno…    NA    NA
2 NA     2022 NA    2025    2030  2035  2040  2045  2050 NA    2025   2030  2035
# ℹ 10 more variables: ...14 <dbl>, ...15 <dbl>, ...16 <dbl>, ...17 <lgl>,
#   ...18 <chr>, ...19 <dbl>, ...20 <dbl>, ...21 <dbl>, ...22 <dbl>,
#   ...23 <dbl>

4.4.4.2 Tidying the headers

sheet_header_processed <- sheet_header |> 
  # transpose the data
  t() |>
  # turn it back into a tibble
  as_tibble() |>
  # make them meaningful
  rename(scenario = V1, year = V2) |>
  # fill scenario down
  fill(scenario) |>
  #insert "Current" at top
  replace_na(list(scenario = "Current Year"))
Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if
`.name_repair` is omitted as of tibble 2.0.0.
ℹ Using compatibility `.name_repair`.
sheet_header_processed
# A tibble: 23 × 2
   scenario                 year 
   <chr>                    <chr>
 1 Current Year             <NA> 
 2 Current Year             2022 
 3 Current Year             <NA> 
 4 Stated policies scenario 2025 
 5 Stated policies scenario 2030 
 6 Stated policies scenario 2035 
 7 Stated policies scenario 2040 
 8 Stated policies scenario 2045 
 9 Stated policies scenario 2050 
10 Stated policies scenario <NA> 
# ℹ 13 more rows

4.4.4.3 Get The Mineral Name

mineral_name <- read_key_minerals_sheet(range = "A8") |> 
  pull()
New names:
• `` -> `...1`
mineral_name
[1] "Copper"

4.4.4.4 Get The Table

mineral_info <- read_key_minerals_sheet(range = "A8:W18")
New names:
• `` -> `...1`
• `` -> `...2`
• `` -> `...3`
• `` -> `...4`
• `` -> `...5`
• `` -> `...6`
• `` -> `...7`
• `` -> `...8`
• `` -> `...9`
• `` -> `...10`
• `` -> `...11`
• `` -> `...12`
• `` -> `...13`
• `` -> `...14`
• `` -> `...15`
• `` -> `...16`
• `` -> `...17`
• `` -> `...18`
• `` -> `...19`
• `` -> `...20`
• `` -> `...21`
• `` -> `...22`
• `` -> `...23`
mineral_info
# A tibble: 11 × 23
   ...1         ...2 ...3     ...4    ...5    ...6    ...7    ...8    ...9 ...10
   <chr>       <dbl> <lgl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl> <lgl>
 1 Copper    3.81e+2 NA    617.    1.39e+3 1.74e+3 2.23e+3 2.42e+3 2.31e+3 NA   
 2 Cobalt    6.35e+1 NA     61.2   3.80e+1 2.91e+1 3.54e+1 4.09e+1 4.74e+1 NA   
 3 Graphite  5.57e+2 NA    936.    1.59e+3 1.78e+3 1.69e+3 1.49e+3 1.08e+3 NA   
 4 Lithium   6.96e+1 NA    123.    2.19e+2 3.07e+2 4.07e+2 4.50e+2 4.13e+2 NA   
 5 Manganese 7.45e+1 NA     53.9   1.56e+2 3.36e+2 5.33e+2 7.12e+2 8.58e+2 NA   
 6 Nickel    3.13e+2 NA    554.    5.13e+2 5.89e+2 6.30e+2 6.92e+2 6.88e+2 NA   
 7 Silicon   8.70e+0 NA     40.5   1.39e+2 2.24e+2 3.52e+2 3.74e+2 3.73e+2 NA   
 8 Neodymium 3.96e+0 NA      7.25  1.22e+1 1.51e+1 1.88e+1 2.18e+1 2.29e+1 NA   
 9 Dysprosi… 4.13e-1 NA      0.741 1.21e+0 1.48e+0 1.84e+0 2.13e+0 2.23e+0 NA   
10 Praseody… 5.94e-1 NA      1.09  1.83e+0 2.27e+0 2.83e+0 3.27e+0 3.43e+0 NA   
11 Terbium   8.06e-2 NA      0.148 2.52e-1 3.14e-1 3.91e-1 4.52e-1 4.74e-1 NA   
# ℹ 13 more variables: ...11 <dbl>, ...12 <dbl>, ...13 <dbl>, ...14 <dbl>,
#   ...15 <dbl>, ...16 <dbl>, ...17 <lgl>, ...18 <dbl>, ...19 <dbl>,
#   ...20 <dbl>, ...21 <dbl>, ...22 <dbl>, ...23 <dbl>

4.4.4.5 Putting It All Together

mineral_info_col_names <- names(mineral_info)

mineral_info_col_names
 [1] "...1"  "...2"  "...3"  "...4"  "...5"  "...6"  "...7"  "...8"  "...9" 
[10] "...10" "...11" "...12" "...13" "...14" "...15" "...16" "...17" "...18"
[19] "...19" "...20" "...21" "...22" "...23"
sheet_headers_and_col_names <- sheet_header_processed |> 
  add_column(mineral_info_col_names = mineral_info_col_names)

sheet_headers_and_col_names
# A tibble: 23 × 3
   scenario                 year  mineral_info_col_names
   <chr>                    <chr> <chr>                 
 1 Current Year             <NA>  ...1                  
 2 Current Year             2022  ...2                  
 3 Current Year             <NA>  ...3                  
 4 Stated policies scenario 2025  ...4                  
 5 Stated policies scenario 2030  ...5                  
 6 Stated policies scenario 2035  ...6                  
 7 Stated policies scenario 2040  ...7                  
 8 Stated policies scenario 2045  ...8                  
 9 Stated policies scenario 2050  ...9                  
10 Stated policies scenario <NA>  ...10                 
# ℹ 13 more rows
mineral_info_long <- mineral_info |> 
  rename(indicator = `...1`) |> 
  pivot_longer(cols = -indicator,
               names_to = "mineral_info_col_names") |> 
  add_column(mineral_name)

mineral_info_long
# A tibble: 242 × 4
   indicator mineral_info_col_names value mineral_name
   <chr>     <chr>                  <dbl> <chr>       
 1 Copper    ...2                    381. Copper      
 2 Copper    ...3                     NA  Copper      
 3 Copper    ...4                    617. Copper      
 4 Copper    ...5                   1389. Copper      
 5 Copper    ...6                   1736. Copper      
 6 Copper    ...7                   2233. Copper      
 7 Copper    ...8                   2418. Copper      
 8 Copper    ...9                   2313. Copper      
 9 Copper    ...10                    NA  Copper      
10 Copper    ...11                   732. Copper      
# ℹ 232 more rows
combined_data <- mineral_info_long |> 
  left_join(sheet_headers_and_col_names, by = join_by(mineral_info_col_names)) |> 
  # filter out what were empty columns (where years are NA) 
  filter(!is.na(year)) |> 
  # case_when is supercharged if else
  mutate(unit = case_when(
    indicator == "Share of clean technologies in total demand" ~ "Percent",
    .default = "kiloton"
    ),
    # convert the year column from character to numeric
    year = as.integer(year)
  ) |> 
  select(mineral_name, indicator, scenario, unit, year, value)

combined_data
# A tibble: 209 × 6
   mineral_name indicator scenario                   unit     year value
   <chr>        <chr>     <chr>                      <chr>   <int> <dbl>
 1 Copper       Copper    Current Year               kiloton  2022  381.
 2 Copper       Copper    Stated policies scenario   kiloton  2025  617.
 3 Copper       Copper    Stated policies scenario   kiloton  2030 1389.
 4 Copper       Copper    Stated policies scenario   kiloton  2035 1736.
 5 Copper       Copper    Stated policies scenario   kiloton  2040 2233.
 6 Copper       Copper    Stated policies scenario   kiloton  2045 2418.
 7 Copper       Copper    Stated policies scenario   kiloton  2050 2313.
 8 Copper       Copper    Announced pledges scenario kiloton  2025  732.
 9 Copper       Copper    Announced pledges scenario kiloton  2030 2113.
10 Copper       Copper    Announced pledges scenario kiloton  2035 3587.
# ℹ 199 more rows

4.4.4.6 Building the function

mineral_name_range <- "A8"
mineral_info_range <- "A7:W18"


mineral_name <- read_key_minerals_sheet(range = mineral_name_range) |> 
  pull()
New names:
• `` -> `...1`
mineral_info <- read_key_minerals_sheet(range = mineral_info_range)
New names:
• `` -> `...1`
• `` -> `...2`
• `` -> `...3`
• `` -> `...4`
• `` -> `...5`
• `` -> `...6`
• `` -> `...7`
• `` -> `...8`
• `` -> `...9`
• `` -> `...10`
• `` -> `...11`
• `` -> `...12`
• `` -> `...13`
• `` -> `...14`
• `` -> `...15`
• `` -> `...16`
• `` -> `...17`
• `` -> `...18`
• `` -> `...19`
• `` -> `...20`
• `` -> `...21`
• `` -> `...22`
• `` -> `...23`
mineral_info_col_names <- names(mineral_info)

mineral_info_long <- mineral_info |> 
  rename(indicator = `...1`) |> 
  pivot_longer(cols = -indicator,
               names_to = "mineral_info_col_names") |> 
  add_column(mineral_name)

combined_data <- mineral_info_long |> 
  left_join(sheet_headers_and_col_names, by = join_by(mineral_info_col_names)) |> 
  # filter out what were empty columns (where years are NA) 
  filter(!is.na(year)) |> 
  # case_when is supercharged if else
  mutate(unit = case_when(
    indicator == "Share of clean technologies in total demand" ~ "Percent",
    .default = "kiloton"
    ),
    # convert the year column from character to numeric
    year = as.integer(year)
  ) |> 
  select(mineral_name, indicator, scenario, unit, year, value)

combined_data
# A tibble: 228 × 6
   mineral_name indicator                 scenario             unit   year value
   <chr>        <chr>                     <chr>                <chr> <int> <dbl>
 1 Copper       Constrained nickel supply Current Year         kilo…  2022    NA
 2 Copper       Constrained nickel supply Stated policies sce… kilo…  2025    NA
 3 Copper       Constrained nickel supply Stated policies sce… kilo…  2030    NA
 4 Copper       Constrained nickel supply Stated policies sce… kilo…  2035    NA
 5 Copper       Constrained nickel supply Stated policies sce… kilo…  2040    NA
 6 Copper       Constrained nickel supply Stated policies sce… kilo…  2045    NA
 7 Copper       Constrained nickel supply Stated policies sce… kilo…  2050    NA
 8 Copper       Constrained nickel supply Announced pledges s… kilo…  2025    NA
 9 Copper       Constrained nickel supply Announced pledges s… kilo…  2030    NA
10 Copper       Constrained nickel supply Announced pledges s… kilo…  2035    NA
# ℹ 218 more rows
read_iea_mineral_table <-
  function(mineral_name_range, mineral_info_range) {
    mineral_name <-
      read_key_minerals_sheet(range = mineral_name_range) |>
      pull()
    
    mineral_info <- read_key_minerals_sheet(range = mineral_info_range)
    
    mineral_info_col_names <- names(mineral_info)
    
    mineral_info_long <- mineral_info |>
      rename(indicator = `...1`) |>
      pivot_longer(cols = -indicator,
                   names_to = "mineral_info_col_names") |>
      add_column(mineral_name)
    
    combined_data <- mineral_info_long |>
      left_join(sheet_headers_and_col_names, by = join_by(mineral_info_col_names)) |>
      # filter out what were empty columns (where years are NA)
      filter(!is.na(year)) |>
      # case_when is supercharged if else
      mutate(
        unit = case_when(
          indicator == "Share of clean technologies in total demand" ~ "Percent",
          .default = "kiloton"
        ),
        # convert the year column from character to numeric
        year = as.integer(year)
      ) |>
      select(mineral_name, indicator, scenario, unit, year, value)
    
    combined_data
  }
constrained_nickle_table <- read_iea_mineral_table(
  mineral_name_range = "A7",
  mineral_info_range = "A8:W19"
)
New names:
New names:
• `` -> `...1`
silicon_table <- read_iea_mineral_table(
  mineral_name_range = "A22",
  mineral_info_range = "A23:W34"
)
New names:
New names:
• `` -> `...1`
solidstate_table <- read_iea_mineral_table(
  mineral_name_range = "A37",
  mineral_info_range = "A38:W49"
)
New names:
New names:
• `` -> `...1`
lowerbatteries_table <- read_iea_mineral_table(
  mineral_name_range = "A52",
  mineral_info_range = "A53:W64"
)
New names:
New names:
• `` -> `...1`
limitedbatteries_table <- read_iea_mineral_table(
  mineral_name_range = "A67",
  mineral_info_range = "A68:W79"
)
New names:
New names:
• `` -> `...1`
final_iea_ev_table <-constrained_nickle_table |> 
  bind_rows(constrained_nickle_table) |> 
  bind_rows(silicon_table) |>
  bind_rows(solidstate_table) |>
  bind_rows(lowerbatteries_table)|>
  bind_rows(limitedbatteries_table)

final_iea_ev_table
# A tibble: 1,368 × 6
   mineral_name              indicator scenario                unit   year value
   <chr>                     <chr>     <chr>                   <chr> <int> <dbl>
 1 Constrained nickel supply Copper    Current Year            kilo…  2022  381.
 2 Constrained nickel supply Copper    Stated policies scenar… kilo…  2025  617.
 3 Constrained nickel supply Copper    Stated policies scenar… kilo…  2030 1389.
 4 Constrained nickel supply Copper    Stated policies scenar… kilo…  2035 1736.
 5 Constrained nickel supply Copper    Stated policies scenar… kilo…  2040 2233.
 6 Constrained nickel supply Copper    Stated policies scenar… kilo…  2045 2418.
 7 Constrained nickel supply Copper    Stated policies scenar… kilo…  2050 2313.
 8 Constrained nickel supply Copper    Announced pledges scen… kilo…  2025  732.
 9 Constrained nickel supply Copper    Announced pledges scen… kilo…  2030 2113.
10 Constrained nickel supply Copper    Announced pledges scen… kilo…  2035 3587.
# ℹ 1,358 more rows

4.4.4.7 Writing The Tidied Data To data

write_csv(final_iea_ev_table,here("data", "iea_ev_.csv"))
our_cleaned_data <- here("data", "iea_ev_.csv") |> 
  read_csv()
Rows: 1368 Columns: 6
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (4): mineral_name, indicator, scenario, unit
dbl (2): year, value

ℹ 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.
our_cleaned_data
# A tibble: 1,368 × 6
   mineral_name              indicator scenario                unit   year value
   <chr>                     <chr>     <chr>                   <chr> <dbl> <dbl>
 1 Constrained nickel supply Copper    Current Year            kilo…  2022  381.
 2 Constrained nickel supply Copper    Stated policies scenar… kilo…  2025  617.
 3 Constrained nickel supply Copper    Stated policies scenar… kilo…  2030 1389.
 4 Constrained nickel supply Copper    Stated policies scenar… kilo…  2035 1736.
 5 Constrained nickel supply Copper    Stated policies scenar… kilo…  2040 2233.
 6 Constrained nickel supply Copper    Stated policies scenar… kilo…  2045 2418.
 7 Constrained nickel supply Copper    Stated policies scenar… kilo…  2050 2313.
 8 Constrained nickel supply Copper    Announced pledges scen… kilo…  2025  732.
 9 Constrained nickel supply Copper    Announced pledges scen… kilo…  2030 2113.
10 Constrained nickel supply Copper    Announced pledges scen… kilo…  2035 3587.
# ℹ 1,358 more rows

Insight 1

# Prep Copper data for scenarios under constrained nickel supply for copper vs current
copper_data<-our_cleaned_data|>
  filter(indicator=="Copper", mineral_name=="Constrained nickel supply")
copper_data
# A tibble: 38 × 6
   mineral_name              indicator scenario                unit   year value
   <chr>                     <chr>     <chr>                   <chr> <dbl> <dbl>
 1 Constrained nickel supply Copper    Current Year            kilo…  2022  381.
 2 Constrained nickel supply Copper    Stated policies scenar… kilo…  2025  617.
 3 Constrained nickel supply Copper    Stated policies scenar… kilo…  2030 1389.
 4 Constrained nickel supply Copper    Stated policies scenar… kilo…  2035 1736.
 5 Constrained nickel supply Copper    Stated policies scenar… kilo…  2040 2233.
 6 Constrained nickel supply Copper    Stated policies scenar… kilo…  2045 2418.
 7 Constrained nickel supply Copper    Stated policies scenar… kilo…  2050 2313.
 8 Constrained nickel supply Copper    Announced pledges scen… kilo…  2025  732.
 9 Constrained nickel supply Copper    Announced pledges scen… kilo…  2030 2113.
10 Constrained nickel supply Copper    Announced pledges scen… kilo…  2035 3587.
# ℹ 28 more rows
# Visualization 1:

ggplot(copper_data, aes(x = year, y = value, color = scenario, group = scenario)) +
  geom_line() +
  geom_point() +
  facet_wrap(~indicator, scales = 'free_y') +
  labs(title = "Demand for Copper Over Time by Scenario",
       x = "Year",
       y = "Demand (kilotons)",
       color = "Scenario") +
  theme_minimal()

#The chart to show the projected demand for copper over time, segmented by different policy scenarios. It indicates a significant increase in demand across all scenarios, with the "Net Zero Emissions by 2050" scenario showing the steepest rise.

Insight 2

# Plot for Cobalt demand under different scenarios vs current

# Visualization 2: Line Plot of Copper and Cobalt Demand Over Time 
cobalt_data<-our_cleaned_data|>
  filter(indicator=="Cobalt",mineral_name=="Constrained nickel supply")
cobalt_data
# A tibble: 38 × 6
   mineral_name              indicator scenario                unit   year value
   <chr>                     <chr>     <chr>                   <chr> <dbl> <dbl>
 1 Constrained nickel supply Cobalt    Current Year            kilo…  2022  63.5
 2 Constrained nickel supply Cobalt    Stated policies scenar… kilo…  2025  61.2
 3 Constrained nickel supply Cobalt    Stated policies scenar… kilo…  2030  38.0
 4 Constrained nickel supply Cobalt    Stated policies scenar… kilo…  2035  29.1
 5 Constrained nickel supply Cobalt    Stated policies scenar… kilo…  2040  35.4
 6 Constrained nickel supply Cobalt    Stated policies scenar… kilo…  2045  40.9
 7 Constrained nickel supply Cobalt    Stated policies scenar… kilo…  2050  47.4
 8 Constrained nickel supply Cobalt    Announced pledges scen… kilo…  2025  71.2
 9 Constrained nickel supply Cobalt    Announced pledges scen… kilo…  2030  58.4
10 Constrained nickel supply Cobalt    Announced pledges scen… kilo…  2035  61.8
# ℹ 28 more rows
ggplot(cobalt_data, aes(x = year, y = value, color = scenario, group = scenario)) +
  geom_line() +
  geom_point() +
  facet_wrap(~indicator, scales = 'free_y') +
  labs(title = "Demand for Cobalt Over Time by Scenario",
       x = "Year",
       y = "Demand (kilotons)",
       color = "Scenario") +
  theme_minimal()

#The chart  illustrates the projected demand for cobalt over time, delineated by different policy scenarios. It shows an initial dip in demand under the "Stated policies scenario," followed by a recovery and eventual surpassing of the "Current Year" demand in the "Net Zero Emissions by 2050" scenario, indicating a rebound and growth in demand for cobalt in the context of aggressive environmental goals.

Insight 3

library(dplyr)
#Filter for Copper and Cobalt for scenario where theres a faster uptake of solid state batteries
copper_faster <- our_cleaned_data|>
  filter(indicator == "Copper" & mineral_name == "Faster uptake of solid state batteries")

cobalt_faster <- our_cleaned_data |>
  filter(indicator == "Cobalt" & mineral_name == "Faster uptake of solid state batteries")

# Joining the two data frames
# Assuming you want to join by 'year' and 'scenario' which are common columns
combined_faster <- full_join(copper_faster, cobalt_faster, by = c("year", "scenario"), suffix = c("_copper", "_cobalt"))

# Now combined_faster contains both Copper and Cobalt data for Net zero scenario
combined_faster_netzero<-combined_faster|>
  filter(scenario=="Net Zero Emissions by 2050 scenario")

# If you want to add Cobalt data to the same plot, you could add another geom_line() for cobalt, e.g.,
ggplot(combined_faster_netzero, aes(x = year)) +
  geom_smooth(aes(y = value_copper, color = "Copper"), method = 'loess') +
  geom_smooth(aes(y = value_cobalt, color = "Cobalt"), method = 'loess') +
  labs(title = "Trend of Copper and Cobalt Demand if there's faster uptake of solid state batteries",
       x = "Year",
       y = "Demand (kilotons)") +
  scale_color_manual(values = c("Copper" = "blue", "Cobalt" = "red")) +
  theme_minimal()
`geom_smooth()` using formula = 'y ~ x'
`geom_smooth()` using formula = 'y ~ x'
Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
-Inf

Warning in max(ids, na.rm = TRUE): no non-missing arguments to max; returning
-Inf

#This chart compares the demand trends for copper and cobalt under the assumption of a faster uptake of solid-state batteries. The steep increase and plateau in copper demand suggest a significant reliance and leveling off in the technology's maturation, while the relatively flat line for cobalt indicates a lower impact on its demand, possibly due to alternative technologies reducing cobalt usage

Insight 4

# Assuming df is your cleaned dataframe

# Filter the data for the current year
current_year <- our_cleaned_data |>
  filter(scenario == "Current Year")

# Further filter the data for 'Constrained nickel supply'
current_year_filtered <- current_year |>
  filter(mineral_name == "Constrained nickel supply")
current_year_base <- current_year_filtered |>
  filter(indicator != "Total EV")
current_year_base
# A tibble: 22 × 6
   mineral_name              indicator    scenario     unit     year   value
   <chr>                     <chr>        <chr>        <chr>   <dbl>   <dbl>
 1 Constrained nickel supply Copper       Current Year kiloton  2022 381.   
 2 Constrained nickel supply Cobalt       Current Year kiloton  2022  63.5  
 3 Constrained nickel supply Graphite     Current Year kiloton  2022 557.   
 4 Constrained nickel supply Lithium      Current Year kiloton  2022  69.6  
 5 Constrained nickel supply Manganese    Current Year kiloton  2022  74.5  
 6 Constrained nickel supply Nickel       Current Year kiloton  2022 313.   
 7 Constrained nickel supply Silicon      Current Year kiloton  2022   8.70 
 8 Constrained nickel supply Neodymium    Current Year kiloton  2022   3.96 
 9 Constrained nickel supply Dysprosium   Current Year kiloton  2022   0.413
10 Constrained nickel supply Praseodymium Current Year kiloton  2022   0.594
# ℹ 12 more rows
total_ev <- current_year_filtered |>
  filter(indicator == "Total EV") |>
  distinct()
total_ev
# A tibble: 1 × 6
  mineral_name              indicator scenario     unit     year value
  <chr>                     <chr>     <chr>        <chr>   <dbl> <dbl>
1 Constrained nickel supply Total EV  Current Year kiloton  2022 1473.
# Calculate the total value for 'Total EV'
total_ev_value <- sum(total_ev$value)

# Calculate the indicator share of EV
indicator_share_of_ev <- current_year_base |>
  mutate(share_of_ev = value / total_ev_value)

indicator_share_of_ev
# A tibble: 22 × 7
   mineral_name              indicator  scenario unit   year   value share_of_ev
   <chr>                     <chr>      <chr>    <chr> <dbl>   <dbl>       <dbl>
 1 Constrained nickel supply Copper     Current… kilo…  2022 381.       0.259   
 2 Constrained nickel supply Cobalt     Current… kilo…  2022  63.5      0.0431  
 3 Constrained nickel supply Graphite   Current… kilo…  2022 557.       0.378   
 4 Constrained nickel supply Lithium    Current… kilo…  2022  69.6      0.0473  
 5 Constrained nickel supply Manganese  Current… kilo…  2022  74.5      0.0506  
 6 Constrained nickel supply Nickel     Current… kilo…  2022 313.       0.213   
 7 Constrained nickel supply Silicon    Current… kilo…  2022   8.70     0.00591 
 8 Constrained nickel supply Neodymium  Current… kilo…  2022   3.96     0.00269 
 9 Constrained nickel supply Dysprosium Current… kilo…  2022   0.413    0.000280
10 Constrained nickel supply Praseodym… Current… kilo…  2022   0.594    0.000403
# ℹ 12 more rows
#Graphing 
# Ordering Bars in Bar Graph
indicator_share_of_ev_ordered <- indicator_share_of_ev |>
  arrange(desc(share_of_ev))

# Flip the Bar Graph to Horizontal
ggplot(indicator_share_of_ev_ordered, aes(x = reorder(indicator, share_of_ev), y = share_of_ev, fill = indicator)) +
  geom_bar(stat = "identity", position = "dodge") +
  coord_flip() +
  labs(title = "Mineral Demand Share for EV under Constrained Nickle Supply Scenraio", x = "Indicator", y = "Share of EV Demand") +
  theme_minimal()

#Thebar chart displays the share of each mineral's demand in the production of electric vehicles (EV) under a constrained nickel supply scenario. The length of each bar represents the proportion of demand for that particular mineral. The chart highlights which minerals may see increased demand relative to others when nickel supply is limited, guiding strategic decisions in supply chain management and investment within the EV industry.


Insight 4
::: {.cell}

```{.r .cell-code}
library(dplyr)
# Filter the data for the Net zero target
scenario_2050 <-our_cleaned_data |>
  filter(scenario =="Net Zero Emissions by 2050 scenario")

# Further filter the data for 'Wider use of silicon-rich anodes'
scenario2050_filtered <-scenario_2050|>
  filter(mineral_name == "Wider use of silicon-rich anodes")
current_year_base <- current_year_filtered |>
  filter(indicator != "Total EV")
current_year_base
# A tibble: 22 × 6
   mineral_name              indicator    scenario     unit     year   value
   <chr>                     <chr>        <chr>        <chr>   <dbl>   <dbl>
 1 Constrained nickel supply Copper       Current Year kiloton  2022 381.   
 2 Constrained nickel supply Cobalt       Current Year kiloton  2022  63.5  
 3 Constrained nickel supply Graphite     Current Year kiloton  2022 557.   
 4 Constrained nickel supply Lithium      Current Year kiloton  2022  69.6  
 5 Constrained nickel supply Manganese    Current Year kiloton  2022  74.5  
 6 Constrained nickel supply Nickel       Current Year kiloton  2022 313.   
 7 Constrained nickel supply Silicon      Current Year kiloton  2022   8.70 
 8 Constrained nickel supply Neodymium    Current Year kiloton  2022   3.96 
 9 Constrained nickel supply Dysprosium   Current Year kiloton  2022   0.413
10 Constrained nickel supply Praseodymium Current Year kiloton  2022   0.594
# ℹ 12 more rows
total_ev <- current_year_filtered |>
  filter(indicator == "Total EV") |>
  distinct()
total_ev
# A tibble: 1 × 6
  mineral_name              indicator scenario     unit     year value
  <chr>                     <chr>     <chr>        <chr>   <dbl> <dbl>
1 Constrained nickel supply Total EV  Current Year kiloton  2022 1473.
# Calculate the total value for 'Total EV'
total_ev_value <- sum(total_ev$value)

# Calculate the indicator share of EV
indicator_share_of_ev <- current_year_base |>
  mutate(share_of_ev = value / total_ev_value)

indicator_share_of_ev
# A tibble: 22 × 7
   mineral_name              indicator  scenario unit   year   value share_of_ev
   <chr>                     <chr>      <chr>    <chr> <dbl>   <dbl>       <dbl>
 1 Constrained nickel supply Copper     Current… kilo…  2022 381.       0.259   
 2 Constrained nickel supply Cobalt     Current… kilo…  2022  63.5      0.0431  
 3 Constrained nickel supply Graphite   Current… kilo…  2022 557.       0.378   
 4 Constrained nickel supply Lithium    Current… kilo…  2022  69.6      0.0473  
 5 Constrained nickel supply Manganese  Current… kilo…  2022  74.5      0.0506  
 6 Constrained nickel supply Nickel     Current… kilo…  2022 313.       0.213   
 7 Constrained nickel supply Silicon    Current… kilo…  2022   8.70     0.00591 
 8 Constrained nickel supply Neodymium  Current… kilo…  2022   3.96     0.00269 
 9 Constrained nickel supply Dysprosium Current… kilo…  2022   0.413    0.000280
10 Constrained nickel supply Praseodym… Current… kilo…  2022   0.594    0.000403
# ℹ 12 more rows
ggplot(indicator_share_of_ev, aes(x = '', y = share_of_ev, fill=indicator)) +
  geom_bar(stat = 'identity', width = 1) +
  coord_polar('y', start = 0) +
  labs(title = 'Mineral Share of EV for Silicon-rich Anodes in 2050 Net Zero Scenario', x = '', y = '', fill = 'Mineral Name') +
  theme_void()

#The pie chart represents the distribution of various minerals used for silicon-rich anodes in electric vehicles (EV) in the 2050 Net Zero Emissions scenario. It visually breaks down the relative demand for each mineral, with larger pie slices indicating a higher demand, and thus highlighting critical minerals for future EV technology

:::