# Estadística descriptiva
# Variable cualitativa nominal: Orientación de Acumulación 
# Autora: Lopez Dayanara 
# Fecha: 14/11/2026

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
library(gt)

# 1. Cargar datos
archivo <- "C:/Users/USER/Downloads/dataset/database.csv"
datos <- read.csv(archivo, stringsAsFactors = FALSE)

# 2. Estandarizar y mapear las orientaciones cardinales de acumulación
nombres_orientacion <- c(
  "N"  = "Norte (N)",
  "NE" = "Noreste (NE)",
  "E"  = "Este (E)",
  "SE" = "Sureste (SE)",
  "S"  = "Sur (S)",
  "SW" = "Suroeste (SW)",
  "W"  = "Oeste (W)",
  "NW" = "Noroeste (NW)"
)

datos$Acc.Orient.Str <- trimws(as.character(datos$Accumulation.Orientation))

TDF_ORIENTACION <- data.frame(Orientacion = datos$Acc.Orient.Str) %>%
  mutate(Orientacion = ifelse(Orientacion %in% names(nombres_orientacion), nombres_orientacion[Orientacion], "No registrado")) %>%
  count(Orientacion, name = "ni") %>%
  arrange(desc(ni)) %>%
  mutate(
    `hi(%)` = round((ni / sum(ni)) * 100, 2)
  )

# Cambiar el título de la columna principal
colnames(TDF_ORIENTACION)[1] <- "Orientación de Acumulación"

# 3. Agregar fila TOTAL
fila_total <- data.frame(
  `Orientación de Acumulación` = "TOTAL",
  ni = sum(TDF_ORIENTACION$ni),
  `hi(%)` = round(sum(TDF_ORIENTACION$`hi(%)`), 0),
  check.names = FALSE
)

TDF_ORIENTACION_suma <- bind_rows(TDF_ORIENTACION, fila_total)

# 4. Mostrar tabla estilizada con gt
TDF_ORIENTACION_suma %>%
  gt() %>%
  tab_header(
    title = md("**Tabla Nro. 1**"),
    subtitle = "Distribución de frecuencias de la variable Orientación de Acumulación para el análisis de los Glaciares"
  ) %>%
  tab_source_note(
    source_note = md("Autora: Grpo 1 <br> Fuente: https://www.kaggle.com/datasets/nsidcorg/glacier-inventory")
  ) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    column_labels.border.bottom.width = px(2),
    row.striping.include_table_body = TRUE,
    heading.border.bottom.width = px(2)
  )
Tabla Nro. 1
Distribución de frecuencias de la variable Orientación de Acumulación para el análisis de los Glaciares
Orientación de Acumulación ni hi(%)
Noreste (NE) 24553 18.48
Norte (N) 24004 18.06
Noroeste (NW) 20687 15.57
No registrado 18458 13.89
Este (E) 10051 7.56
Sureste (SE) 9993 7.52
Suroeste (SW) 8987 6.76
Oeste (W) 8322 6.26
Sur (S) 7835 5.90
TOTAL 132890 100.00
Autora: Grpo 1
Fuente: https://www.kaggle.com/datasets/nsidcorg/glacier-inventory
# --- GAMA DE COLORES CELESTE / AMBIENTAL ---
gama_celeste <- c("#E0F2FE", "#BAE6FD", "#7DD3FC", "#38BDF8", "#0284C7", "#0369A1", "#075985", "#0C4A6E", "#134E4A", "#042F2E")

# --- GRÁFICO 1: Distribución Global (ni) ---
par(mar = c(9, 5, 4, 2))

grafico_ni_global <- barplot(
  TDF_ORIENTACION$ni,
  names.arg = TDF_ORIENTACION$`Orientación de Acumulación`,
  col = gama_celeste[1:nrow(TDF_ORIENTACION)],
  main = "Gráfico Nro. 1: Distribución Global de la Orientación de Acumulación (ni)",
  ylab = "Cantidad",
  ylim = c(0, sum(TDF_ORIENTACION$ni)),
  las = 2,
  cex.names = 0.72,
  cex.axis = 0.85
)

text(
  x = grafico_ni_global,
  y = TDF_ORIENTACION$ni,
  labels = TDF_ORIENTACION$ni,
  pos = 3,
  cex = 0.8,
  col = "black"
)

# --- GRÁFICO 2: Distribución Local (ni) ---
par(mar = c(9, 6.5, 4, 2))

grafico_ni_local <- barplot(
  TDF_ORIENTACION$ni,
  names.arg = TDF_ORIENTACION$`Orientación de Acumulación`,
  col = gama_celeste[1:nrow(TDF_ORIENTACION)],
  main = "Gráfico Nro. 2: Distribución Local de la Orientación de Acumulación (ni)",
  xlab = "",
  ylim = c(0, max(TDF_ORIENTACION$ni) * 1.15),
  las = 2,
  cex.names = 0.72,
  cex.axis = 0.85
)

title(xlab = "Puntos Cardinales", line = 7)
title(ylab = "Cantidad", line = 4.5)

text(
  x = grafico_ni_local,
  y = TDF_ORIENTACION$ni,
  labels = TDF_ORIENTACION$ni,
  pos = 3,
  cex = 0.8,
  col = "black"
)

# --- GRÁFICO 3: Distribución Global Porcentual (hi%) ---
par(mar = c(9, 6.5, 4, 2))

grafico_hi_global <- barplot(
  TDF_ORIENTACION$`hi(%)`,
  names.arg = TDF_ORIENTACION$`Orientación de Acumulación`,
  col = gama_celeste[1:nrow(TDF_ORIENTACION)],
  main = "Gráfico Nro. 3: Distribución Global Porcentual de la Orientación de Acumulación (hi%)",
  xlab = "",
  ylim = c(0, 100),
  las = 2,
  cex.names = 0.72,
  cex.axis = 0.85
)

title(xlab = "Puntos Cardinales", line = 7)
title(ylab = "Cantidad (%)", line = 4.5)

text(
  x = grafico_hi_global,
  y = TDF_ORIENTACION$`hi(%)`,
  labels = paste0(TDF_ORIENTACION$`hi(%)`, "%"),
  pos = 3,
  cex = 0.8,
  col = "black"
)

# --- GRÁFICO 4: Distribución Local Porcentual (hi%) ---
par(mar = c(9, 6.5, 4, 2))

grafico_hi_local <- barplot(
  TDF_ORIENTACION$`hi(%)`,
  names.arg = TDF_ORIENTACION$`Orientación de Acumulación`,
  col = gama_celeste[1:nrow(TDF_ORIENTACION)],
  main = "Gráfico Nro. 4: Distribución Local Porcentual de la Orientación de Acumulación (hi%)",
  xlab = "",
  ylim = c(0, max(TDF_ORIENTACION$`hi(%)`) * 1.15),
  las = 2,
  cex.names = 0.72,
  cex.axis = 0.85
)

title(xlab = "Puntos Cardinales", line = 7)
title(ylab = "Cantidad (%)", line = 4.5)

text(
  x = grafico_hi_local,
  y = TDF_ORIENTACION$`hi(%)`,
  labels = paste0(TDF_ORIENTACION$`hi(%)`, "%"),
  pos = 3,
  cex = 0.8,
  col = "black"
)

# --- GRÁFICO 5: Diagrama Circular con Porcentajes Alrededor y Leyenda Lateral ---
par(mar = c(4, 4, 4, 19), xpd = TRUE)

# 1. Dibujar el pastel limpio sin textos internos
pie_obj <- pie(
  TDF_ORIENTACION$`hi(%)`,
  labels = NA,
  col = gama_celeste[1:nrow(TDF_ORIENTACION)],
  main = "Gráfico Nro. 5: Distribución Porcentual de la Orientación de Acumulación",
  radius = 0.70
)

# 2. Calcular posiciones polares (ángulos medios) para ubicar los porcentajes alrededor
porcentajes <- TDF_ORIENTACION$`hi(%)`
acumulado <- cumsum(porcentajes)
angulos <- pi * (acumulado - porcentajes / 2) / 50

# Coordenadas X e Y flotando alrededor del radio del círculo (0.88)
x_etq <- 0.88 * cos(angulos)
y_etq <- 0.88 * sin(angulos)

# 3. Imprimir los porcentajes alrededor del diagrama sin solaparse
text(
  x = x_etq,
  y = y_etq,
  labels = paste0(porcentajes, "%"),
  cex = 0.75,
  font = 2,
  col = "#0369A1"
)

# 4. Leyenda lateral detallada con las categorías y sus porcentajes
leyenda_texto <- paste0(TDF_ORIENTACION$`Orientación de Acumulación`, " (", TDF_ORIENTACION$`hi(%)`, "%)")

legend(
  "right",
  inset = c(-1.18, 0),
  legend = leyenda_texto,
  fill = gama_celeste[1:nrow(TDF_ORIENTACION)],
  bty = "n",
  cex = 0.65,
  title = "Orientación Cardinal"
)

par(xpd = FALSE)