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`.
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(mineral_name, indicator, scenario, unit, year, value)combined_data
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(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(mineral_name, indicator, scenario, unit, year, value)combined_data
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(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(mineral_name, indicator, scenario, unit, year, value) combined_data }
# A tibble: 1,368 × 6
mineral_name indicator scenario unit year value
<chr> <chr> <chr> <chr> <int> <dbl>
1 STEPS - Base case Copper Current Year kiloton 2022 373.
2 STEPS - Base case Copper Stated policies scenario kiloton 2025 580.
3 STEPS - Base case Copper Stated policies scenario kiloton 2030 1044.
4 STEPS - Base case Copper Stated policies scenario kiloton 2035 1219.
5 STEPS - Base case Copper Stated policies scenario kiloton 2040 1421.
6 STEPS - Base case Copper Stated policies scenario kiloton 2045 1458.
7 STEPS - Base case Copper Stated policies scenario kiloton 2050 1346.
8 STEPS - Base case Copper Announced pledges scenario kiloton 2025 689.
9 STEPS - Base case Copper Announced pledges scenario kiloton 2030 1592.
10 STEPS - Base case Copper Announced pledges scenario kiloton 2035 2570.
# ℹ 1,358 more rows
#Write the datawrite_csv(final_iea_ev_table, "/Users/liaoshiyu/Desktop/Data mining/data2/EV.csv")clean_data <-read_csv("/Users/liaoshiyu/Desktop/Data mining/data2/EV.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.
#3ggplot(clean_data) +aes(x = scenario, y = value, fill = scenario, group = scenario) +geom_boxplot() +scale_fill_hue(direction =1) +labs(title ="Total unit in different senario", subtitle ="By Shiyu Liao") +theme_minimal()
#4clean_data %>%filter(mineral_name %in%"Constrained nickel supply") %>%filter(indicator %in%c("Neodymium", "Praseodymium")) %>%filter(scenario %in%"Net Zero Emissions by 2050 scenario") %>%ggplot() +aes(x = year, y = value, colour = indicator) +geom_line() +scale_color_hue(direction =1) +theme_minimal()
clean_data %>%filter(mineral_name %in%"Faster uptake of solid state batteries") %>%filter(indicator %in%"Total EV") %>%filter(!(scenario %in%"Current Year")) %>%filter(year >= 2030L & year <= 2050L) %>%ggplot() +aes(x = year, y = value, colour = scenario) +geom_line() +scale_color_hue(direction =1) +labs(x ="Year in Predict", y ="KT", title ="Total demand of M to EV by Scenario", subtitle ="By Shiyu Liao") +theme_minimal()