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.

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) %>%
  gt() %>%
  tab_header(
    title = md("*Tabla N°1*"),
    subtitle = md("**Distribucion de frecuencias de Unit Type**")
  ) %>%
  cols_label(
    Categoria = "Tipo de Unidad", ni = md("n~i~"), hi_perc = md("h~i~")
  ) %>%
  cols_hide(columns = c(hi_unit, Ni_ac, hi_ac_perc)) %>%
  cols_align(align = "center", columns = everything()) %>%
  fmt_number(columns = hi_perc, decimals = 2) %>%
  fmt_number(columns = ni, 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 ni hi
field 4,336 52.03
asset 3,124 37.49
pool 461 5.53
project 183 2.20
block 111 1.33
complex 73 0.88
concession 16 0.19
area 14 0.17
phase 11 0.13
basin 3 0.04
sub-basin 2 0.02
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

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",
       x = "Tipo de Unidad", y = "Frecuencia Absoluta (ni)",
       caption = fuente_nota) +
  tema_base

6.2 Diagrama de Barras - Frecuencia Relativa Porcentual

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 Porcentual",
       x = "Tipo de Unidad", y = "Frecuencia Relativa (%)",
       caption = fuente_nota) +
  tema_base

6.3 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)]

data.frame(
  Variable = "Unit Type",
  Rango    = "-",
  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 5") %>%
  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 - - - field - - - - -
Autor: Grupo 5

8.- Conclusiones

El valor mas frecuente de Unit Type es field.