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)
# 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
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
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
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 N1**"),
subtitle = md("Distribucion de frecuencias de Unit Type")
) %>%
cols_label(
Categoria = "Tipo de Unidad", ni = md("n~i~"),
hi_unit = md("h~i~ (Unit)"), hi_perc = md("h~i~ (%)"),
Ni_ac = md("N~i~ (Acum \u2191)"), hi_ac_perc = md("H~i~ (% Acum \u2191)")
) %>%
cols_hide(columns = c(Ni_ac, hi_ac_perc)) %>%
cols_align(align = "center", columns = everything()) %>%
fmt_number(columns = c(hi_perc, hi_ac_perc), decimals = 2) %>%
fmt_number(columns = c(hi_unit), decimals = 4) %>%
fmt_number(columns = c(ni, Ni_ac), decimals = 0, use_seps = TRUE) %>%
tab_source_note(fuente_nota) %>%
tab_style(
style = cell_fill(color = "#F5F5F5"),
locations = cells_body(rows = nrow(TDF_Cat) + 1)
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(rows = nrow(TDF_Cat) + 1)
) %>%
sub_missing(columns = everything(), missing_text = "") %>%
tab_options(heading.background.color = "#EBF5FB", column_labels.font.weight = "bold")
| Tabla N1 | |||
| Distribucion de frecuencias de Unit Type | |||
| Tipo de Unidad | ni | hi (Unit) | hi (%) |
|---|---|---|---|
| field | 4,336 | 0.5203 | 52.03 |
| asset | 3,124 | 0.3749 | 37.49 |
| pool | 461 | 0.0553 | 5.53 |
| project | 183 | 0.0220 | 2.20 |
| block | 111 | 0.0133 | 1.33 |
| complex | 73 | 0.0088 | 0.88 |
| concession | 16 | 0.0019 | 0.19 |
| area | 14 | 0.0017 | 0.17 |
| phase | 11 | 0.0013 | 0.13 |
| basin | 3 | 0.0004 | 0.04 |
| sub-basin | 2 | 0.0002 | 0.02 |
| TOTAL | 8,334 | 1.0000 | 100.00 |
| n = 8,334 | Fuente: Global Energy Monitor - GOGET 2023 | |||
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))
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
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
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"))
Se presenta la tabla estandar de indicadores (Tendencia Central, Dispersion, Forma y Outliers). Al ser Unit Type una variable cualitativa nominal, el unico indicador aplicable es la Moda; el resto de columnas no aplica y se marca con un guion “-”.
moda_cat <- TDF_Cat$Categoria[1]
tabla_ind <- data.frame(
Variable = "Unit Type",
Rango = "-",
X = "-",
Me = "-",
Mo = moda_cat,
Sd = "-",
CV = "-",
As = "-",
K = "-",
Outliers = "-",
check.names = FALSE
)
tabla_ind %>%
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"
) %>%
cols_align(align = "center", columns = everything()) %>%
tab_source_note("Autor: Grupo 5") %>%
tab_options(
heading.background.color = "#EBF5FB",
column_labels.font.weight = "bold",
column_labels.background.color = "#D9D9D9",
table.width = pct(100),
data_row.padding = px(10),
table.font.size = px(14),
row.striping.include_table_body = TRUE,
row.striping.background_color = "#F2F2F2",
table.border.top.color = "black",
table.border.bottom.color = "black",
table_body.border.bottom.color = "black"
)
| INDICADORES ESTADISTICOS | |||||||||
| Variable | Rango |
Tendencia Central
|
Dispersion
|
Forma
|
Outliers | ||||
|---|---|---|---|---|---|---|---|---|---|
| X | Me | Mo | Sd | CV(%) | As | K | |||
| Unit Type | - | - | - | field | - | - | - | - | - |
| Autor: Grupo 5 | |||||||||
El valor mas frecuente de Unit Type es field.