Installing package into 'C:/Users/ashwi/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
package 'here' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\ashwi\AppData\Local\Temp\RtmpwXQppS\downloaded_packages
install.packages("ggplot2")
Installing package into 'C:/Users/ashwi/AppData/Local/R/win-library/4.3'
(as 'lib' is unspecified)
package 'ggplot2' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\ashwi\AppData\Local\Temp\RtmpwXQppS\downloaded_packages
library(tidyverse) # pretty much always...
Warning: package 'ggplot2' was built under R version 4.3.3
── 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
Warning: package 'here' was built under R version 4.3.3
read_key_minerals_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)# now all we have to do is provide the rangesheet_header <-read_key_minerals_sheet(range ="A4:W5")
# 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
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 elsemutate(# convert the year column from character to numericyear =as.integer(year) ) |>select(mineral_name, indicator, scenario, year, value) combined_data }
# Load the ggplot2 librarylibrary(ggplot2)# Filter the data for indicator == 'Copper' and year == '2022'copper_2022 <-subset(final_iea_EV_table, indicator =='Copper'& year ==2022)# Create a bar chart with different colors for each technology scenarioggplot(copper_2022, aes(x = technology_scenario, y = value, fill = technology_scenario)) +geom_bar(stat ="identity") +labs(title ="Copper Demand for EV in 2022 by Technology Scenario",x ="Technology Scenario",y ="Values") +theme_minimal() +theme(axis.text.x =element_text(angle =45, hjust =1))
Homework Question 2
cobalt_2022 <-subset(final_iea_EV_table, indicator =='Cobalt'& year ==2022)# Create a bar chart with different colors for each technology scenarioggplot(cobalt_2022, aes(x = technology_scenario, y = value, fill = technology_scenario)) +geom_bar(stat ="identity") +labs(title ="Cobalt Demand for EV in 2022 by Technology Scenario",x ="Technology Scenario",y ="Values") +theme_minimal() +theme(axis.text.x =element_text(angle =45, hjust =1))
Homework Question 3
copper_all_data <-subset(final_iea_EV_table, indicator =='Copper')ggplot(copper_all_data) +aes(x = year, y = value, fill = scenario, colour = scenario) +geom_line() +scale_fill_hue(direction =1) +scale_color_hue(direction =1) +labs(subtitle ="Copper Demand by Scenarios") +theme_minimal() +facet_wrap(vars(technology_scenario))
Homework Question 4
cobalt_all_data <-subset(final_iea_EV_table, indicator =='Cobalt')ggplot(cobalt_all_data) +aes(x = year, y = value, fill = scenario, colour = scenario) +geom_line() +scale_fill_hue(direction =1) +scale_color_hue(direction =1) +labs(title ="Cobalt Demand by Scenarios") +theme_minimal() +facet_wrap(vars(technology_scenario))
Homework 5
# Load the ggplot2 librarylibrary(ggplot2)# Filter the data for scenario == "Net Zero Emissions by 2050 scenario"filtered_data <-subset(final_iea_EV_table, scenario =="Net Zero Emissions by 2050 scenario")# Filter the data for cobalt and copper indicatorscobalt_copper_data <-subset(filtered_data, indicator %in%c("Cobalt", "Copper"))# Create a line chart comparing cobalt and copper over the yearsggplot(cobalt_copper_data, aes(x = year, y = value, color = indicator, group = indicator)) +geom_line() +labs(title ="Projected Demand for Cobalt and Copper (Net Zero Emissions by 2050 scenario)",x ="Year",y ="Value",color ="Indicator") +theme_minimal()