# Cargar datos
datos <- read.csv("Conjunto_Datos_Minerales.csv", header = TRUE, sep = ";", dec = ".")

# Cargar paquete necesario
if (!require(countrycode)) install.packages("countrycode")
library(countrycode)

# 1. Extraer variable país
Country <- datos$country

# 2. Tabla de frecuencia por país
TablaCountry <- as.data.frame(table(Country))
colnames(TablaCountry) <- c("Country", "Freq")

# 3. Calcular porcentaje
TablaCountry$hi_porc <- round((TablaCountry$Freq / sum(TablaCountry$Freq)) * 100,2)

# 4. Agregar continente
TablaCountry$Continente <- countrycode(TablaCountry$Country,
                                       origin = "country.name",
                                       destination = "continent")

# 5. Tabla por continente
TablaContinente <- aggregate(Freq ~ Continente, data = TablaCountry, sum)
TablaContinente$hi_porc <- round((TablaContinente$Freq / sum(TablaContinente$Freq)) * 100,2)

print(TablaContinente)
##   Continente   Freq hi_porc
## 1     Africa   2751    0.90
## 2   Americas 291797   95.79
## 3 Antarctica      2    0.00
## 4       Asia   5102    1.67
## 5     Europe   3459    1.14
## 6    Oceania   1505    0.49
# 6. Gráficas de frecuencia por país

# Global
barplot(TablaCountry$Freq,
        main = "Gráfica No.1: Distribución global de cantidad por país",
        xlab = "País", ylab = "Cantidad",
        col = "skyblue",
        names.arg = TablaCountry$Country,
        ylim = c(0,length(Country)),
        las = 2, 
        cex.names = 0.4, 
        cex.axis = 0.5)

# Local
barplot(TablaCountry$Freq,
        main = "Gráfica No.2: Distribución local de cantidad por país",
        xlab = "País", ylab = "Cantidad",
        col = "red",
        names.arg = TablaCountry$Country,
        las = 2, 
        cex.names = 0.4,
        cex.axis = 0.5)

# 7. Gráficas de porcentaje por país

# Global
barplot(TablaCountry$hi_porc,
        main = "Gráfica No.3: Distribución global de porcentaje por país",
        xlab = "País", ylab = "Porcentaje",
        col = "lightcoral",
        names.arg = TablaCountry$Country,
        las = 2,
        ylim = c(0, 100),
        cex.names = 0.4, 
        cex.axis = 0.6)

# Local
barplot(TablaCountry$hi_porc,
        main = "Gráfica No.4: Distribución local de porcentaje por país",
        xlab = "País", ylab = "Porcentaje",
        col = "lightcoral",
        names.arg = TablaCountry$Country,
        las = 2, 
        cex.names = 0.4, 
        cex.axis = 0.6)

# 8. Gráficas de frecuencia por continente

# Global
barplot(TablaContinente$Freq,
        main = "Gráfica No.5: Distribución global por continente",
        xlab = "Continente", ylab = "Cantidad",
        col = "palegreen",
        names.arg = TablaContinente$Continente,
        ylim = c(0,length(Country)),
        las = 1, 
        cex.names = 0.6, 
        cex.axis = 0.6)

# Local
barplot(TablaContinente$Freq,
        main = "Gráfica No.6: Distribución local por continente",
        xlab = "Continente", ylab = "Cantidad",
        col = "palegreen",
        names.arg = TablaContinente$Continente,
        las = 1, 
        cex.names = 0.6, 
        cex.axis = 0.6)

# 9. Gráficas de porcentaje por continente

# Global
barplot(TablaContinente$hi_porc,
        main = "Gráfica No.7: Porcentaje global por continente",
        xlab = "Continente", ylab = "Porcentaje",
        col = "gold",
        names.arg = TablaContinente$Continente,
        las = 1, ylim = c(0, 100),
        cex.names = 0.6, cex.axis = 0.6)

# Local
barplot(TablaContinente$hi_porc,
        main = "Gráfica No.8: Porcentaje local por continente",
        xlab = "Continente", ylab = "Porcentaje",
        col = "gold",
        names.arg = TablaContinente$Continente,
        las = 1, cex.names = 0.6, cex.axis = 0.6)

# 10. Gráfico circular de porcentaje por continente

# Paleta de grises
colores_grises <- gray.colors(length(TablaContinente$Continente), start = 0.3, end = 0.9)

# Pie chart
pie(TablaContinente$hi_porc,
    labels = NA,
    main = "Gráfica No.9: Porcentaje por continente (circular)",
    col = colores_grises)

# Leyenda
legend("topright",
       legend = paste(TablaContinente$Continente, ": ", round(TablaContinente$hi_porc, 1), "%"),
       fill = colores_grises,
       cex = 0.8)