Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.
Running Code
When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:
options(repos =c(CRAN ="https://cran.rstudio.com/"))library(tidyverse) # pretty much always...
── 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 fileslibrary(here) # for using relative file paths
here() starts at C:/Users/rachelz/OneDrive/Desktop/Graduate/Spring 2024/Sustainable Finance/Assignmengts/Week 5 data import
[1] "C:/Users/rachelz/OneDrive/Desktop/Graduate/Spring 2024/Sustainable Finance/Assignmengts/Week 5 data import/data-raw/CM_Data_Explorer.xlsx"
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)sheet_header <-read_EV_sheet(range ="A4:W5")
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
#-----------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 elsemutate(unit =case_when( indicator =="Share of clean technologies in total demand"~"Percent",.default ="kiloton" ),# convert the year column from character to numericyear =as.integer(year) ) |>select(issue, indicator, scenario, unit, year, value)combined_data
read_iea_EV_table <-function(mineral_name_range, mineral_info_range) { mineral_name <-read_EV_sheet(range = mineral_name_range) |>pull() mineral_info <-read_EV_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)) |>rename(issue = mineral_name) |># case_when is supercharged if elsemutate(unit =case_when( indicator =="Share of clean technologies in total demand"~"Percent",.default ="kiloton" ),# convert the year column from character to numericyear =as.integer(year) ) |>select(issue, indicator, scenario, unit, year, value) combined_data }
Copper_Cobalt <- final_iea_EV_table |>filter(indicator =="Copper"| indicator =="Cobalt")ggplot(Copper_Cobalt, aes(x = scenario, y = value, fill =factor(indicator))) +geom_boxplot(fill ="skyblue", color ="black") +labs(x ="Scenario", y ="Value", title ="Boxplot of Copper and Cobalt by Scenario") +facet_wrap(~factor(indicator)) +theme(axis.text.x =element_text(angle =30, hjust =1))