setwd("~/UCE/III/estadistica")

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

#Variable oper_type
#Extraer variable oper_type y convertir a carácter
OperType <- as.character(datos$oper_type)
OperType[is.na(OperType) | OperType == ""] <- "NA"

# TDF
TDFOperType <- table(OperType)
TablaOperType <- as.data.frame(TDFOperType)

#Renombrar la columna
colnames(TablaOperType) <- c("OperType", "ni")
#Elementos de la tabla
ni<-TablaOperType$ni
hi_porc<-round((TablaOperType$ni/sum(ni))*100,2)

#Verificar suma de frecuancias absolutas y relativas
sum(ni)
## [1] 304632
sum(hi_porc)
## [1] 100.01
#Agregar elementos a la tabla
TablaOperType<- data.frame(TablaOperType,hi_porc)
print(TablaOperType)
##               OperType     ni hi_porc
## 1      Brine Operation     58    0.02
## 2           Geothermal    377    0.12
## 3                Leach     40    0.01
## 4             Offshore    481    0.16
## 5               Placer   9063    2.98
## 6     Processing Plant   4434    1.46
## 7              Surface  90959   29.86
## 8  Surface-Underground   9822    3.22
## 9          Underground  40699   13.36
## 10             Unknown 147491   48.42
## 11                Well   1208    0.40
# Crear una paleta de colores
colores <- rainbow(nrow(TablaOperType))
#GDF
#ni Global
barplot(TablaOperType$ni,
        names.arg = TablaOperType$OperType,
        col = colores,
        main = "Gráfica No.1: Distribución de cantidad de tipo de explotación por 
        categoría",
        xlab = "Categoría",
        ylab = "Cantidad",
        las = 2, ylim = c(0,length(OperType)),
        cex.names = 0.5,
        cex.axis = 0.5)

#ni Local
barplot(TablaOperType$ni,
        names.arg = TablaOperType$OperType,
        col = colores,
        main = "Gráfica No.2: Distribución de cantidad de tipo de explotación por 
        categoría",
        xlab = "Categoría",
        ylab = "Cantidad",
        las = 2,
        cex.names = 0.5,
        cex.axis = 0.5)

#hi Global
barplot(TablaOperType$hi_porc,
        names.arg = TablaOperType$OperType,
        col = colores,
        main = "Gráfica No.3: Distribución de porcentaje de tipo de explotación por 
        categoría",
        xlab = "Categoría",
        ylab = "Cantidad",
        las = 2, ylim = c(0,100),
        cex.names = 0.5,
        cex.axis = 0.5)

#hi Local
barplot(TablaOperType$hi_porc,
        names.arg = TablaOperType$OperType,
        col = colores,
        main = "Gráfica No.4: Distribución de porcentaje de tipo de explotación por 
        categoría",
        xlab = "Categoría",
        ylab = "Cantidad",
        las = 2,
        cex.names = 0.5,
        cex.axis = 0.5)

# Gráfico circular
colores_gris <- gray(seq(0.2, 0.8, length.out = length(TablaOperType$hi_porc)))

pie(TablaOperType$hi_porc,
    labels = rep("", length(TablaOperType$hi_porc)),
    col = colores_gris,
    main = "Gráfica No.5: Distribución de porcentaje de tipo de explotación por categoría",
    cex.main = 1,
    radius = 1)

legend("topright",
       legend = paste(TablaOperType$OperType, ": ", round(TablaOperType$hi_porc, 1), "%"),
       fill = colores_gris,
       cex = 0.5,
       bty = "n",
       xpd = TRUE)