#==============================ENCABEZADO===================================
# TEMA: ED Variables Cualitativas
# AUTOR: GRUPO 4
# FECHA: 18-12-2025
#==============================CARGA DE DATOS===================================
library(gt)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
setwd("C:/Users/HP/Documents/PROYECTO ESTADISTICA/RStudio")
datos <- read.csv("tablap.csv", header = TRUE, dec = ",", sep = ";")
#==============================TABLA DE DATOS===================================
# VARIABLE 1: ECORREGION
ecorreg <- datos$Ecoregion
TDFecorreg <- table(ecorreg)
Tablaecorreg <- as.data.frame(TDFecorreg)

#Nombres de columnas
names(Tablaecorreg) <- c("ECORREGION", "ni") 
Tablaecorreg$hi_porcentaje <- (Tablaecorreg$ni / sum(Tablaecorreg$ni)) * 100
#A??ADIMOS EL NUMERO DE FILA (N)
Tablaecorreg <- cbind(N = 1:nrow(Tablaecorreg), Tablaecorreg)
#A??ADIMOS LA FILA TOTAL
TDFecorregFinal <- rbind(
  Tablaecorreg,
  data.frame(N = NA, ECORREGION = "TOTAL", ni = sum(Tablaecorreg$ni), 
             hi_porcentaje = 100)
)
TDFecorregFinal$hi_porcentaje <- round(TDFecorregFinal$hi_porcentaje, 2)
print(TDFecorregFinal)
##    N              ECORREGION    ni hi_porcentaje
## 1  1      Chihuahuan Deserts  2150         17.12
## 2  2       Colorado Plateaus  3792         30.19
## 3  3             High Plains   946          7.53
## 4  4    New Mexico Mountains     4          0.03
## 5  5      New Mexico Plateau  3745         29.81
## 6  6        Southern Rockies  1747         13.91
## 7  7 Southwestern Tablelands   177          1.41
## 8 NA                   TOTAL 12561        100.00
#==============================TDF PLOT================================
# Aqui incluimos la columna N
tabla_gt_resultado <- gt(TDFecorregFinal) %>%
  tab_header(
    title = "Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto 
        climatico de pozos de gas natural en Nuevo Mexico",
  ) %>%
  cols_label(
    N = "N",              
    ECORREGION = "Ecorregion", 
    ni = "ni", 
    hi_porcentaje = "hi"
  ) %>%
  sub_missing(columns = N, missing_text = "") %>% 
  tab_style(
    style = list(cell_fill(color = "gray90"), cell_text(weight = "bold")),
    locations = cells_body(rows = ECORREGION == "TOTAL")
  ) %>%
  tab_options(table.width = pct(95))
tabla_gt_resultado
Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto climatico de pozos de gas natural en Nuevo Mexico
N Ecorregion ni hi
1 Chihuahuan Deserts 2150 17.12
2 Colorado Plateaus 3792 30.19
3 High Plains 946 7.53
4 New Mexico Mountains 4 0.03
5 New Mexico Plateau 3745 29.81
6 Southern Rockies 1747 13.91
7 Southwestern Tablelands 177 1.41

TOTAL 12561 100.00
#===================DIAGRAMA DE FRECUENCIA ABSOLUTA LOCAL===========================
par(mar = c(10, 4, 4, 2) + 0.1) 
barplot(table(ecorreg),
        main = "Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto 
        climatico de pozos de gas natural en Nuevo Mexico (Local)",
        xlab = "ECORREGION", ylab = "CANTIDAD", col = "grey30",
        las = 2, cex.names = 0.6)

#===================DIAGRAMA DE FRECUENCIA ABSOLUTA GLOBAL===========================
par(mar = c(10, 4, 4, 2) + 0.1) 
barplot(table(ecorreg),
        main = "Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto 
        climatico de pozos de gas natural en Nuevo Mexico (Global)",
        xlab = "ECORREGION", ylab = "CANTIDAD", col = "grey40",
        las = 2, ylim = c(0, sum(Tablaecorreg$ni)), cex.names = 0.6)

#====================DIAGRAMA DE FRECUENCIA RELATIVA LOCAL==========================
par(mar = c(10, 4, 4, 2) + 0.1) 
barplot(TDFecorregFinal$hi_porcentaje[TDFecorregFinal$ECORREGION != "TOTAL"],
        main = "Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto 
        climatico de pozos de gas natural en Nuevo Mexico (Local)",
        xlab = "ECORREGION", ylab = "PORCENTAJE", col = "grey50",
        las = 2, cex.names = 0.6,
        names.arg = TDFecorregFinal$ECORREGION[TDFecorregFinal$ECORREGION != "TOTAL"])

#====================DIAGRAMA DE FRECUENCIA RELATIVA GLOBAL==========================
par(mar = c(10, 4, 4, 2) + 0.1) 
barplot(TDFecorregFinal$hi_porcentaje[TDFecorregFinal$ECORREGION != "TOTAL"],
        main = "Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto 
        climatico de pozos de gas natural en Nuevo Mexico (Global)",
        xlab = "ECORREGION", ylab = "PORCENTAJE", col = "grey60",
        las = 2, cex.names = 0.6, ylim = c(0, 100),
        names.arg = TDFecorregFinal$ECORREGION[TDFecorregFinal$ECORREGION != "TOTAL"])

#================================DIAGRAMA CIRCULAR===========================
# DIAGRAMA CIRCULAR ECORREGION
par(mar = c(0, 0, 1, 4), xpd = TRUE)
etiqueta <- paste(TDFecorregFinal$hi_porcentaje[TDFecorregFinal$ECORREGION != "TOTAL"], "%")
colores <- c(rev(grey.colors(nrow(Tablaecorreg))))

pie(TDFecorregFinal$hi_porcentaje[TDFecorregFinal$ECORREGION != "TOTAL"],
    main = "Distribucion de ecorregiones asociadas al Analisis geoespacial del impacto 
        climatico de pozos de gas natural en Nuevo Mexico",
    radius = 0.65,
    col = colores,
    labels = etiqueta,
    cex = 0.6)

legend("bottomright",
       legend = TDFecorregFinal$ECORREGION[TDFecorregFinal$ECORREGION != "TOTAL"],
       title = "Leyenda",
       fill = colores,
       cex = 0.55)