── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.1
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ 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
library(readxl) # for reading excel files# install.packages("here") # install the package if you haven't yetlibrary(here) # for using relative file paths
read_EV_sheet <-partial(# provide the function, in this case readxl::read_excel().f = read_excel,# provide any arguments you want filled inpath = path_to_sheet,sheet ="2.3 EV",col_names =FALSE)
# 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>
sheet_header_processed <- sheet_header |># transpose the datat() |># turn it back into a tibbleas_tibble() |># make them meaningfulrename(scenario = V1, year = V2) |># fill scenario downfill(scenario) |>#insert "Current" at topreplace_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
#for all market trends: read_EV_table <-function(market_trend_range, market_implication_info_range) { market_trend <-read_EV_sheet(range = market_trend_range) |>pull() market_implication_info <-read_EV_sheet(range = market_implication_info_range) info_col_names <-names(market_implication_info) implication_info_long <- market_implication_info |>rename(mineral =`...1`) |>pivot_longer(cols =-mineral,names_to ="info_col_names") |>add_column(market_trend) combined_data <- implication_info_long |>left_join(sheet_headers_and_col_names, by =join_by(info_col_names)) |>filter(!is.na(year)) |>mutate(year =as.integer(year),unit='kilotone') |>select(market_trend, mineral, scenario, year, value, unit) combined_data }#read in all of the tables at the same time and bind them together as one dataset with just a few lines of code.nickel_supply_table <-read_EV_table(market_trend_range ="A7",market_implication_info_range ="A8:W9")
#visualization1#general demand trend under each policy scenariosggplot(final_EV_table, aes(x = year, y = value, color = scenario, group = scenario)) +geom_point() +facet_wrap(~mineral, scales ='free_y') +labs(title ="Mineral Demand Trends for EVs", y ="Demand (kt)", x ="Year", color ="Scenario") +theme_minimal()
#visualization 2#Overall demand trend under each market advancementggplot(final_EV_table, aes(x = year, y = value, fill = market_trend)) +geom_bar(stat ="identity", position =position_dodge(width =0.7)) +facet_wrap(~mineral, scales ='free_y') +labs(title ="Impact of Technology on Copper and Cobalt Demand",subtitle ="Overall Demand under different technological advancements",y ="Demand (kt)",x ="Year") +scale_fill_brewer(palette ="Set2") +theme_minimal() +theme(legend.position ="bottom")
#3ggplot(final_EV_table, aes(x = market_trend, y = scenario, fill = value)) +geom_tile() +facet_wrap(~mineral, scales ='free_y') +scale_fill_gradient(low ="blue", high ="red") +labs(title ="Demand Heatmap for Copper and Cobalt",subtitle ="Interplay between policy and technological scenarios",y ="Policy Scenario",x ="Market Trend") +theme_minimal()
#4 Compare the demand trend for copper and cobalt using grouped bar chartggplot(final_EV_table, aes(x =factor(year), y = value, fill = mineral)) +geom_bar(stat ="identity", position =position_dodge()) +scale_fill_manual(values =c("Copper"="orange", "Cobalt"="blue")) +labs(title ="Comparative Demand for Copper and Cobalt",y ="Demand (kt)",x ="Year",fill ="Mineral") +theme_minimal()