1.Carga de Librerias

Preparacion del entorno de trabajo cargando los paquetes necesarios para el manejo de datos, formato de tablas y visualizacion de graficas.

library(readxl)
library(dplyr)
library(gt)
library(ggplot2)
library(scales)

2.Leer Datos

# Seleccion del archivo Excel (se abre ventana emergente)
ruta_archivo <- file.choose()
Datos <- read_excel(ruta_archivo, guess_max = 50000)

cat("Dataset cargado exitosamente. Total de variables importadas:", ncol(Datos), "\n")
## Dataset cargado exitosamente. Total de variables importadas: 32

3.Seleccion de la Variable

Se extrae y limpia la variable Unit type (Tipo de Unidad). Se eliminan los valores nulos y se estandarizan los textos (eliminando espacios) para garantizar que el conteo de categorias sea correcto

Variable <- Datos$`Unit type` %>%
  na.omit() %>%
  as.character() %>%
  trimws()

N <- length(Variable)

cat("Variable analizada: Unit type\n")
## Variable analizada: Unit type
cat("Tipo de variable: Cualitativa Nominal\n")
## Tipo de variable: Cualitativa Nominal
cat("Total de observaciones utiles (n):", N, "\n")
## Total de observaciones utiles (n): 8334

4.Conteo (Frecuencias)

La variable Unit Type es de naturaleza cualitativa nominal (no posee orden jerarquico entre sus categorias: field, asset, pool, etc.). Se calcula la frecuencia absoluta, relativa y porcentual de cada categoria, ordenando de mayor a menor para facilitar la lectura.

TDF_Cat <- data.frame(Categoria = Variable) %>%
  group_by(Categoria) %>%
  summarise(ni = n(), .groups = "drop") %>%
  arrange(desc(ni)) %>%
  mutate(
    hi_unit = ni / N,
    hi_perc = hi_unit * 100,
    Ni_ac = cumsum(ni),
    hi_ac_perc = cumsum(hi_perc)
  )

K_real <- nrow(TDF_Cat)

cat("Numero de categorias distintas (k):", K_real, "\n")
## Numero de categorias distintas (k): 11

5.Tabla de Distribucion de Frecuencias

Se estructura la matriz de resultados ordenando las categorias por su frecuencia absoluta, de mayor a menor, para identificar rapidamente las tipologias mas comunes en la industria. La frecuencia local corresponde al valor de cada categoria (ni y hi) y la frecuencia global al valor acumulado hasta esa categoria (Ni y Hi).

fuente_nota <- paste("n =", format(N, big.mark = ","), "| Fuente: Global Energy Monitor - GOGET 2023")

fila_total_cat <- data.frame(
  Categoria = "TOTAL",
  ni = sum(TDF_Cat$ni),
  hi_unit = sum(TDF_Cat$hi_unit),
  hi_perc = sum(TDF_Cat$hi_perc),
  Ni_ac = NA,
  hi_ac_perc = NA
)

bind_rows(TDF_Cat, fila_total_cat) %>%
  select(Categoria, ni, Ni_ac, hi_perc, hi_ac_perc) %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°1*"),
    subtitle = md("**Distribucion de frecuencias de Unit Type**")
  ) %>%
  tab_spanner(label = md("**Frecuencia Absoluta**"), columns = c(ni, Ni_ac)) %>%
  tab_spanner(label = md("**Frecuencia Relativa (%)**"), columns = c(hi_perc, hi_ac_perc)) %>%
  cols_label(
    Categoria  = "Tipo de Unidad",
    ni         = md("Local (n~i~)"),
    Ni_ac      = md("Global (N~i~)"),
    hi_perc    = md("Local (h~i~)"),
    hi_ac_perc = md("Global (H~i~)")
  ) %>%
  cols_align(align = "center", columns = everything()) %>%
  fmt_number(columns = c(hi_perc, hi_ac_perc), decimals = 2) %>%
  fmt_number(columns = c(ni, Ni_ac), decimals = 0, use_seps = TRUE) %>%
  tab_source_note(fuente_nota) %>%
  tab_style(
    style = cell_text(weight = "bold"),
    locations = cells_body(rows = nrow(TDF_Cat) + 1)
  ) %>%
  tab_style(
    style = cell_borders(sides = c("left", "right"), color = "black", weight = px(1)),
    locations = list(cells_body(columns = everything()), cells_column_labels(columns = everything()))
  ) %>%
  sub_missing(columns = everything(), missing_text = "") %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    column_labels.font.weight = "bold",
    column_labels.border.top.color = "black",
    column_labels.border.bottom.color = "black",
    column_labels.border.bottom.width = px(2),
    heading.border.bottom.color = "black",
    heading.border.bottom.width = px(2),
    table_body.hlines.color = "white",
    table_body.border.bottom.color = "black"
  )
Tabla N°1
Distribucion de frecuencias de Unit Type
Tipo de Unidad
Frecuencia Absoluta
Frecuencia Relativa (%)
Local (ni) Global (Ni) Local (hi) Global (Hi)
field 4,336 4,336 52.03 52.03
asset 3,124 7,460 37.49 89.51
pool 461 7,921 5.53 95.04
project 183 8,104 2.20 97.24
block 111 8,215 1.33 98.57
complex 73 8,288 0.88 99.45
concession 16 8,304 0.19 99.64
area 14 8,318 0.17 99.81
phase 11 8,329 0.13 99.94
basin 3 8,332 0.04 99.98
sub-basin 2 8,334 0.02 100.00
TOTAL 8,334
100.00
n = 8,334 | Fuente: Global Energy Monitor - GOGET 2023

6.Graficos de Distribucion de Frecuencias

paleta_azul <- colorRampPalette(c("#08519c", "#3182bd", "#6baed6", "#9ecae1", "#c6dbef"))(K_real)

tema_base <- theme_minimal(base_size = 12) +
  theme(
    legend.position = "none",
    plot.title = element_text(face = "bold", size = 13),
    plot.caption = element_text(color = "#888888", size = 9, hjust = 0),
    axis.title = element_text(face = "bold", size = 11),
    axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
    panel.grid.major.x = element_blank(),
    panel.grid.major.y = element_line(color = "#EEEEEE"),
    panel.grid.minor = element_blank(),
    plot.background = element_rect(fill = "white", color = NA)
  )

df_graf <- TDF_Cat %>%
  mutate(Categoria = factor(Categoria, levels = Categoria))

6.1 Diagrama de Barras - Frecuencia Absoluta Local (ni)

ggplot(df_graf, aes(x = Categoria, y = ni, fill = Categoria)) +
  geom_col(color = "black", linewidth = 0.3) +
  geom_text(aes(label = format(ni, big.mark = ",")), vjust = -0.4, size = 3.5, fontface = "bold") +
  scale_fill_manual(values = paleta_azul) +
  scale_y_continuous(labels = label_comma(), expand = expansion(mult = c(0, 0.15))) +
  labs(title = "Diagrama de Barras de Frecuencia Absoluta Local",
       x = "Tipo de Unidad", y = "Frecuencia Absoluta Local (ni)",
       caption = fuente_nota) +
  tema_base

6.2 Diagrama de Barras - Frecuencia Absoluta Global (Ni)

ggplot(df_graf, aes(x = Categoria, y = Ni_ac, fill = Categoria)) +
  geom_col(color = "black", linewidth = 0.3) +
  geom_text(aes(label = format(Ni_ac, big.mark = ",")), vjust = -0.4, size = 3.5, fontface = "bold") +
  scale_fill_manual(values = paleta_azul) +
  scale_y_continuous(labels = label_comma(), expand = expansion(mult = c(0, 0.15))) +
  labs(title = "Diagrama de Barras de Frecuencia Absoluta Global",
       x = "Tipo de Unidad", y = "Frecuencia Absoluta Global (Ni)",
       caption = fuente_nota) +
  tema_base

6.3 Diagrama de Barras - Frecuencia Relativa Local (hi %)

ggplot(df_graf, aes(x = Categoria, y = hi_perc, fill = Categoria)) +
  geom_col(color = "black", linewidth = 0.3) +
  geom_text(aes(label = paste0(round(hi_perc, 1), "%")), vjust = -0.4, size = 3.5, fontface = "bold") +
  scale_fill_manual(values = paleta_azul) +
  scale_y_continuous(labels = function(x) paste0(x, "%"), expand = expansion(mult = c(0, 0.15))) +
  labs(title = "Diagrama de Barras de Frecuencia Relativa Local",
       x = "Tipo de Unidad", y = "Frecuencia Relativa Local (%)",
       caption = fuente_nota) +
  tema_base

6.4 Diagrama de Barras - Frecuencia Relativa Global (Hi %)

ggplot(df_graf, aes(x = Categoria, y = hi_ac_perc, fill = Categoria)) +
  geom_col(color = "black", linewidth = 0.3) +
  geom_text(aes(label = paste0(round(hi_ac_perc, 1), "%")), vjust = -0.4, size = 3.5, fontface = "bold") +
  scale_fill_manual(values = paleta_azul) +
  scale_y_continuous(labels = function(x) paste0(x, "%"), expand = expansion(mult = c(0, 0.15))) +
  labs(title = "Diagrama de Barras de Frecuencia Relativa Global",
       x = "Tipo de Unidad", y = "Frecuencia Relativa Global (%)",
       caption = fuente_nota) +
  tema_base

6.5 Diagrama Circular

df_pie <- df_graf %>%
  mutate(etiqueta = paste0(Categoria, " (", round(hi_perc, 1), "%)"))

ggplot(df_pie, aes(x = "", y = hi_perc, fill = reorder(etiqueta, -hi_perc))) +
  geom_col(width = 1, color = "white", linewidth = 0.5) +
  coord_polar("y", start = 0) +
  scale_fill_manual(values = paleta_azul) +
  theme_void(base_size = 12) +
  theme(
    legend.position = "right",
    legend.text = element_text(size = 10),
    legend.title = element_text(face = "bold", size = 11),
    plot.margin = margin(t = 20, r = 20, b = 20, l = 20)
  ) +
  guides(fill = guide_legend(title = "Tipo de Unidad"))

7.Indicadores Estadisticos

La variable Unit Type es cualitativa nominal. Para este tipo de variable, el unico indicador de tendencia central aplicable es la moda; el resto de columnas no aplica y se marca con un guion “-”.

moda_cat <- TDF_Cat$Categoria[which.max(TDF_Cat$ni)]
rango_cat <- K_real

data.frame(
  Variable = "Unit Type",
  Rango    = rango_cat,
  X        = "-",
  Me       = "-",
  Mo       = moda_cat,
  Sd       = "-",
  CV       = "-",
  As       = "-",
  K        = "-",
  Outliers = "-",
  check.names = FALSE
) %>%
  gt() %>%
  tab_header(title = md("**INDICADORES ESTADISTICOS**")) %>%
  tab_spanner(label = md("**Tendencia Central**"), columns = c(X, Me, Mo)) %>%
  tab_spanner(label = md("**Dispersion**"), columns = c(Sd, CV)) %>%
  tab_spanner(label = md("**Forma**"), columns = c(As, K)) %>%
  cols_label(
    Variable = "Variable",
    Rango    = "Rango",
    X        = "X",
    Me       = "Me",
    Mo       = "Mo",
    Sd       = "Sd",
    CV       = md("CV(%)"),
    As       = "As",
    K        = "K",
    Outliers = "Outliers"
  ) %>%
  tab_source_note("Autor: Grupo 3") %>%
  tab_options(
    table.width                       = pct(100),
    table.font.size                   = px(12),
    table.font.names                  = "Arial",
    heading.align                     = "center",
    heading.title.font.size           = px(13),
    heading.background.color          = "#AAAAAA",
    heading.border.bottom.color       = "#AAAAAA",
    column_labels.border.bottom.color = "#CCCCCC",
    column_labels.border.bottom.width = px(1),
    table_body.border.bottom.color    = "#CCCCCC",
    table_body.border.bottom.width    = px(1),
    table.border.top.color            = "#AAAAAA",
    table.border.top.width            = px(1),
    table.border.bottom.color         = "#AAAAAA",
    table.border.bottom.width         = px(1),
    source_notes.font.size            = px(11),
    source_notes.border.lr.color      = "transparent",
    data_row.padding                  = px(5)
  ) %>%
  tab_style(
    style     = cell_text(color = "white", weight = "bold"),
    locations = cells_title(groups = "title")
  ) %>%
  tab_style(
    style     = cell_text(color = "#333333", align = "center"),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style     = cell_text(color = "#333333", align = "center"),
    locations = cells_body(columns = c(X, Me, Mo, Sd, CV, As, K, Outliers))
  )
INDICADORES ESTADISTICOS
Variable Rango
Tendencia Central
Dispersion
Forma
Outliers
X Me Mo Sd CV(%) As K
Unit Type 11 - - field - - - - -
Autor: Grupo 3

8.Conclusiones

El valor mas frecuente de Unit Type es field.