Final Project 608

Author

Michael Mayne

Final Project

Overview:

It is no surprise that the U.S often relies heavily on imported good and services. Although the united states has manufacturing means is even the main supplier of many completed products, most raw goods are procured from overseas. This reliance of external good has caused several vulnerabilities in their security due to the procurement of rare mineral and resources

Data Cleaning

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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(maps)

Attaching package: 'maps'

The following object is masked from 'package:purrr':

    map
library(countrycode)
library(plotly)

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
resource_full <- read.csv("https://raw.githubusercontent.com/Mayneman000/DATA607Assignment/refs/heads/608/Mineral%20Main%20Sources.csv")
# #of resources over 50% dependent 
resource_50_count <- resource_full %>%
  count(Majority_Exported.50., sort = TRUE)
resource_full %>%
  filter(Majority_Exported.50. == "Y")
      Mineral   Status   Main_Source Affliation_Main
1    Antimony Critical         China      Competitor
2     Arsenic Critical         China      Competitor
3      Barite Critical        China       Competitor
4     Bismuth Critical        China       Competitor
5      Cerium Critical         Japan            Ally
6      Cesium Critical      Germany             Ally
7    Chromium Critical  South Africa         Neutral
8      Cobalt Critical       Norway             Ally
9   Fluorspar Critical        Mexico             N/A
10 Gadolinium Critical         China      Competitor
11    Gallium Critical        China       Competitor
12  Germanium Critical        China       Competitor
13   Graphite Critical        China       Competitor
14    Holmium Critical         China      Competitor
15     Indium Critical        China       Competitor
16    Iridium Critical  South Africa         Neutral
17   Lutetium Critical        China       Competitor
18  Manganese Critical         Gabon            Ally
19    Niobium Critical       Brazil          Neutral
20   Platinum Critical South Africa          Neutral
21    Rhodium Critical South Africa          Neutral
22   Rubidium Critical       Germany            Ally
23   Samarium Critical         China      Competitor
24   Scandium Critical        Europe            Ally
25   Tantalum Critical         China      Competitor
26  Tellurium Critical        Canada            Ally
27    Thulium Critical         China      Competitor
28        Tin Critical    Indonesia              N/A
29   Titanium Critical        Japan             Ally
30   Tungsten Critical         China      Competitor
31   Vanadium Critical        Canada            Ally
32  Ytterbium Critical         China      Competitor
33    Yttrium Critical         China      Competitor
34       Zinc Critical        Canada            Ally
                 Secondary_Source Affliation_Secondary Majority_Exported.50.
1                             N/A                  N/A                     Y
2                Morroco Belgium               Neutral                     Y
3            India Morocco Mexico             Positive                     Y
4      South Korea Mexico Belgium             Positive                     Y
5                             N/A                  N/A                     Y
6                           China             Negative                     Y
7        Kazakhstan Russia Mexico                Mixed                     Y
8            Canada Japan Finland             Positive                     Y
9     Vietnam South Africa Canada              Neutral                     Y
10                            N/A                  N/A                     Y
11 United Kingdom Germany Ukraine             Positive                     Y
12         Belgium Germany Russia                Mixed                     Y
13            Mexico Canada India             Positive                     Y
14                            N/A                  N/A                     Y
15      Canada South Korea France             Positive                     Y
16                         Canada             Positive                     Y
17            Germany South Korea             Positive                     Y
18 South Africa Australia Georgia             Positive                     Y
19                         Canada             Positive                     Y
20      Germany Switzerland Italy             Positive                     Y
21                            N/A                  N/A                     Y
22                            N/A                  N/A                     Y
23                  United States                  N/A                     Y
24             China Japan Russia             Negative                     Y
25    Germany Australia Indonesia             Positive                     Y
26       GermanyChina Philippines                Mixed                     Y
27                            N/A                  N/A                     Y
28          Peru Malaysia Bolivia                Mixed                     Y
29             Kazakhstan Ukraine                Mixed                     Y
30         Bolivia Germany Canada             Positive                     Y
31      China Brazil South Africa                Mixed                     Y
32  Germany Chile and South Korea             Positive                     Y
33              South Korea Japan             Positive                     Y
34              Mexico Peru Spain              Neutral                     Y
resource_full %>%
  filter(Majority_Exported.50.== "N")
        Mineral   Status   Main_Source Affliation_Main         Secondary_Source
1      Aluminum Critical        Canada            Ally        U.A.E RussiaChina
2     Beryllium Critical United States             N/A                      N/A
3    Dysprosium Critical United States             N/A                      N/A
4        Erbium Critical United States             N/A                      N/A
5      Europium Critical United States             N/A                      N/A
6       Hafnium Critical United States             N/A                      N/A
7     Lanthanum Critical United States             N/A                      N/A
8       Lithium Critical    Argentina          Neutral        ChileChina Russia
9     Magnesium Critical        Canada            Ally            Israel Mexico
10    Neodymium Critical United States             N/A                      N/A
11       Nickel Critical        Canada            Ally Norway Finland Australia
12    Palladium Critical       Russia       Competitor     South Africa Germany
13 Praseodymium Critical United States             N/A                      N/A
14    Ruthenium Critical           N/A             N/A                      N/A
15      Terbium Critical United States             N/A                      N/A
16    Zirconium Critical  South Africa         Neutral Senegal Australia Russia
   Affliation_Secondary Majority_Exported.50.
1              Negative                     N
2                   N/A                     N
3                   N/A                     N
4                   N/A                     N
5                   N/A                     N
6                   N/A                     N
7                   N/A                     N
8              Negative                     N
9              Positive                     N
10                  N/A                     N
11             Positive                     N
12             Positive                     N
13                  N/A                     N
14                  N/A                     N
15                  N/A                     N
16                Mixed                     N
# location of primary resources over 50% dependent 

resource_main <- resource_full %>%
  filter(Main_Source != "N/A")%>%
  mutate(Main_Source = str_trim(Main_Source)) %>% 
  count(Main_Source, sort = TRUE) 
# affiliation of Primary and Secondary Sources
 
affiliations_main <- resource_full %>%
  count(Affliation_Main, sort = FALSE)



affiliations_secondary <- resource_full %>%
  count(Affliation_Secondary, sort = FALSE)

Visualizations

Pie Chart

Number of Resources (Majority Exported)

ggplot(resource_50_count,aes(x = "", fill = reorder( Majority_Exported.50.,n), y = n)) +
  geom_col(color = "black")+
  coord_polar("y", start = 0) +
  geom_text(aes(label = n), 
            position = position_stack(vjust = 0.5), 
            color = "white", size = 6) +
  scale_fill_discrete(labels = c("N" = "<50%", "Y" = ">50%"))+
  labs(
    title = "Amount of Critical Resources U.S. Imports over 50%",
    fill = "Over/Under Reliance"
    
  ) +
  theme_void()

Bar Chart Main source of Imports

China by far the most frequent resource for import of rare minerals.

ggplot(data = resource_main, aes(x = reorder(Main_Source,n) , y= n, fill= Main_Source))+
  coord_flip()+
  geom_col()+
  scale_fill_manual(values = c("China" = "coral","United States" = "darkblue"))+
  geom_text(aes(label = n), 
            hjust = 1.1,
            size = 4,
            color = "lightgray",
            fontface= "bold")+
  labs( y = "Primary Import for the U.S.",
        x =  "Country")+
  theme_minimal()+
  theme(legend.position = "none")

Chart for Secondary Sources

bar_primary <- ggplot(affiliations_main, aes(x = reorder(Affliation_Main, n), y = n)) +
  geom_col(fill = "maroon") +
  geom_text(aes(label = n), hjust = -0.3, size = 3.5, fontface = "bold") +
  coord_flip() +
  theme_minimal() +
  labs(
    title = "Primary Source Minerals",
    x = "Affiliations",
    y = "Number of Minerals"
  )

bar_secondary <- ggplot(affiliations_secondary, aes(x = reorder(Affliation_Secondary, n), y = n)) +
  geom_col(fill = "purple") +
  geom_text(aes(label = n), hjust = -0.3, size = 3.5, fontface = "bold") +
  coord_flip() +
  theme_minimal() +
  labs(
    title = "Secondary Source Minerals",
    x = "Affiliations",
    y = "Number of Minerals"
  )
bar_primary

bar_secondary

Creating a back to back bar chart for Ally ship and resources

primary_data <- tibble(
  Affiliation = c("Competitor", "Ally", "Local", "Neutral"),
  Count = c(18, 13, 12, 7),
  SourceType = "Primary Source"
)

secondary_data <- tibble(
  Affiliation = c("Ally", "Local","Mixed", "Competitor", "Neutral"),
  Count = c(18,18, 7, 4, 3),
  SourceType = "Secondary Source"
)
combined_data <- bind_rows(primary_data, secondary_data) %>%
  mutate(
    PlotCount = if_else(SourceType == "Primary Source", -Count, Count),
    Affiliation = factor(Affiliation, levels = c("Competitor", "Ally", "Mixed","Local", "Neutral"))
  ) %>%
  arrange()
ggplot(combined_data, aes(x = Affiliation, y = PlotCount, fill = SourceType)) +
  geom_col(width = 0.75, color = "#121212") +
  coord_flip() + 
  scale_fill_manual(values = c("Primary Source" = "violet", "Secondary Source" = "darkblue")) +
  geom_text(aes(label = Count, 
                  hjust = if_else(SourceType == "Primary Source", 1.3, -0.3)
                 ), color = "black", fontface = "bold", size = 5)+
  labs( y = "Amount of Countries[Sources]",
        fill = "Level of Source")+
  theme_minimal()+
  theme(legend.position = "top")

MAP for Primary Sources- Interactive chart

main_map_data <- resource_full %>% 
  filter(!Main_Source %in% c("N/A", "Europe"), !is.na(Main_Source)) %>% 
  count(Main_Source, name = "n") %>% 
  mutate(Main_Source = case_when( 
    Main_Source == "United States" ~ "USA", 
    TRUE ~ Main_Source 
  ))

Main_Source_Map <- plot_geo(main_map_data) %>% 
  add_trace(
    z = ~n, 
    locations = ~Main_Source,
    locationmode = "country names", 
    colors = "inferno", 
    text = ~paste("Country:", Main_Source, "<br>Critical Minerals Supplied:", n), 
    hoverinfo = "text" 
  ) %>% 
  layout(
    title = "Global Primary Sources of Critical Minerals",
    geo = list( 
      showframe = FALSE, 
      showcoastlines = TRUE, 
      projection = list(type = "mercator"), 
      landcolor = toRGB("lightgray") 
    )
  ) 

Main_Source_Map
Secondary <- read_csv("https://raw.githubusercontent.com/Mayneman000/DATA607Assignment/refs/heads/main/Secondary.csv")
Rows: 85 Columns: 2
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): Country, Mineral

ℹ 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.

Map for Secondary Sources

library(plotly)
library(tidyverse)


map_count <- Secondary %>%
  count(Country, name = "n") %>%
  mutate(Country = case_when(
    Country == "U.A.E." ~ "United Arab Emirates",
    TRUE ~ Country
  ))

Backup_map <- plot_geo(map_count) %>% 
  add_trace(
    z = ~n, 
    locations = ~Country, 
    locationmode = "country names", 
    colors = "OrRd", 
    text = ~paste("Country:", Country, "<br>Minerals Supplied; Secondary:", n), 
    hoverinfo = "text" 
  ) %>% 
  layout(
    title = "Global Sources of Critical Minerals (Secondary)", 
    geo = list( 
      showframe = FALSE, 
      showcoastlines = TRUE, 
      projection = list(type = 'mercator'), 
      landcolor = toRGB("lightgray") 
    ) 
  )

# Display the map
Backup_map

End of Report.