# Load necessary libraries
pacman::p_load(pacman, readr, dplyr, ggplot2, tidyr, sf, skimr)
# Load the data
mining_refining <- read.csv("mining_refining.csv")
# Drop NA values in the 'value' column
mining_refining <- mining_refining %>% drop_na(value)
# Convert Country to a factor with specified levels (excluding "NA")
mining_refining$Country <- factor(mining_refining$Country,
levels = c("Argentina", "Australia", "Brazil", "Canada",
"Chile", "China", "Finland", "Germany",
"India", "Indonesia", "Japan", "Madagascar",
"Malaysia", "Mozambique", "Myanmar", "New Caledonia",
"Philippines", "Russia", "Sweden", "Tanzania",
"United States", "Total"))
# Filter for a specific year (e.g., 2023) for the bar plot, excluding "NA"
data_2023 <- mining_refining %>% filter(Year == 2023, !is.na(Country) & Country != "NA")
ggplot(data_2023, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
facet_wrap(~ Country, scales = "free_y") +
labs(title = "Mining and Refining Values by Country and Category (2023)",
x = "Category", y = "Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

data_2030 <- mining_refining %>% filter(Year == 2030, !is.na(Country) & Country != "NA")
ggplot(data_2030, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
facet_wrap(~ Country, scales = "free_y") +
labs(title = "Mining and Refining Values by Country and Category (2030)",
x = "Category", y = "Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

data_2035 <- mining_refining %>% filter(Year == 2035, !is.na(Country) & Country != "NA")
ggplot(data_2035, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
facet_wrap(~ Country, scales = "free_y") +
labs(title = "Mining and Refining Values by Country and Category (2035)",
x = "Category", y = "Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

data_2040 <- mining_refining %>% filter(Year == 2040, !is.na(Country) & Country != "NA")
ggplot(data_2040, aes(x = category, y = value, fill = category)) +
geom_bar(stat = "identity") +
facet_wrap(~ Country, scales = "free_y") +
labs(title = "Mining and Refining Values by Country and Category (2040)",
x = "Category", y = "Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Boxplot to show value distribution by category
ggplot(mining_refining %>% filter(!is.na(Country) & Country != "NA"),
aes(x = category, y = value, fill = category)) +
geom_boxplot() +
labs(title = "Distribution of Mining and Refining Values by Category",
x = "Category",
y = "Value") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

# Filter data for 2023
data_2023 <- mining_refining %>% filter(Year == 2023)
# Pie Chart Total Mining and Refining by Category (all countries combined)
category_totals <- data_2023 %>%
group_by(category) %>%
summarise(total_value = sum(value))
ggplot(category_totals, aes(x = "", y = total_value, fill = category)) +
geom_bar(stat = "identity", width = 1, color = "#ffffff") + # white borders
coord_polar(theta = "y") +
labs(title = "Total Mining and Refining by Category (2023)",
x = NULL, y = NULL, fill = "Category") +
theme_minimal() +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
panel.grid = element_blank())
