En este primer paso, preparamos el entorno de trabajo cargando las
librerías necesarias para el análisis de datos (dplyr), la
creación de gráficas avanzadas (ggplot2), y el formato
profesional de tablas (gt).
library(readxl)
library(dplyr)
library(gt)
library(ggplot2)
library(scales)
library(forcats)
datos <- read_excel("dataset_mundial_petro.xlsx")
cat("Dataset cargado exitosamente. Total de variables:", ncol(datos), "\n")
## Dataset cargado exitosamente. Total de variables: 23
Se aísla la variable de interés (Location accuracy) y se
eliminan los registros nulos (NA). Esto garantiza que los
porcentajes reflejen exclusivamente aquellos yacimientos que cuentan con
un nivel de precisión reportado.
cat("Variable analizada: Location accuracy\n")
## Variable analizada: Location accuracy
# Jerarquía lógica de precisión (de mayor a menor exactitud)
orden <- c("exact", "approximate")
Variable <- datos$`Location accuracy`
Variable <- Variable[!is.na(Variable)]
N <- length(Variable)
cat("Total de observaciones limpias (N):", N, "\n")
## Total de observaciones limpias (N): 7538
Variable <- factor(Variable, levels = orden, ordered = TRUE)
Se calcula la frecuencia absoluta (ni), la frecuencia relativa en proporción (hi) y en porcentaje (hi %) para cada categoría, ordenadas de mayor a menor.
tabla_freq <- as.data.frame(table(Variable)) %>%
rename(Precision = Variable, ni = Freq) %>%
arrange(desc(ni)) %>%
mutate(
Precision = as.character(Precision),
ni = as.numeric(ni),
hi_prop = ni / N,
hi_pct = hi_prop * 100,
i = row_number()
) %>%
select(i, Precision, ni, hi_pct, hi_prop)
k <- nrow(tabla_freq)
cat("Número de categorías (k):", k, "\n")
## Número de categorías (k): 2
cat("Categoría más frecuente :", tabla_freq$Precision[1],
"-", tabla_freq$ni[1], "registros\n")
## Categoría más frecuente : exact - 6353 registros
cat("Categoría menos frecuente:", tabla_freq$Precision[k],
"-", tabla_freq$ni[k], "registro(s)\n")
## Categoría menos frecuente: approximate - 1185 registro(s)
cat("Verificación - Σni:", sum(tabla_freq$ni), "(debe ser", N, ")\n")
## Verificación - Σni: 7538 (debe ser 7538 )
cat("Verificación - Σhi%:", round(sum(tabla_freq$hi_pct)), "(debe ser 100)\n")
## Verificación - Σhi%: 100 (debe ser 100)
La tabla estructura las categorías de precisión. Las frecuencias acumuladas permiten evaluar rápidamente qué porcentaje de la base de datos cumple con niveles de precisión específicos o superiores.
tdf <- as.data.frame(table(Variable)) %>%
rename(Nivel = Variable, Fi = Freq) %>%
mutate(
Nivel = as.character(Nivel),
Fi = as.numeric(Fi),
hi = round(Fi / N, 5),
pct = round(hi * 100, 2),
Fi_ac = cumsum(Fi),
hi_ac = round(cumsum(hi), 5)
)
fila_total <- tibble(
Nivel = "TOTAL",
Fi = sum(tdf$Fi),
hi = sum(tdf$hi),
pct = sum(tdf$pct),
Fi_ac = NA_real_,
hi_ac = NA_real_
)
tdf_final <- bind_rows(tdf, fila_total)
tdf_final %>%
gt() %>%
tab_header(
title = md("**TABLA 1. Distribución de Frecuencias por Precisión de Ubicación**")
) %>%
cols_label(
Nivel = "Nivel de Precisión",
Fi = "Frec. Absoluta (Fi)",
hi = "Frec. Relativa (hi)",
pct = "Porcentaje (%)",
Fi_ac = "Frec. Abs. Acumulada (Fi_ac)",
hi_ac = "Frec. Rel. Acumulada (hi_ac)"
) %>%
fmt_number(columns = hi, decimals = 5) %>%
fmt_number(columns = pct, decimals = 2) %>%
fmt_number(columns = hi_ac, decimals = 5) %>%
fmt_integer(columns = c(Fi, Fi_ac)) %>%
sub_missing(columns = c(Fi_ac, hi_ac), missing_text = "") %>%
tab_source_note(source_note = paste0("Fuente: Global Energy Monitor — GOGET 2023 | n = ",
format(N, big.mark = ","))) %>%
cols_align(align = "center", columns = everything()) %>%
tab_style(
style = list(cell_text(weight = "bold"),
cell_borders(sides = "top", color = "black", weight = px(2))),
locations = cells_body(rows = nrow(tdf_final))
) %>%
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 = "grey",
table_body.border.bottom.color = "black"
)
| TABLA 1. Distribución de Frecuencias por Precisión de Ubicación | |||||
| Nivel de Precisión | Frec. Absoluta (Fi) | Frec. Relativa (hi) | Porcentaje (%) | Frec. Abs. Acumulada (Fi_ac) | Frec. Rel. Acumulada (hi_ac) |
|---|---|---|---|---|---|
| exact | 6,353 | 0.84280 | 84.28 | 6,353 | 0.84280 |
| approximate | 1,185 | 0.15720 | 15.72 | 7,538 | 1.00000 |
| TOTAL | 7,538 | 1.00000 | 100.00 | ||
| Fuente: Global Energy Monitor — GOGET 2023 | n = 7,538 | |||||
ggplot(conteo_graf, aes(x = Nivel, y = Fi, fill = Nivel)) +
geom_col(width = 0.5, color = "white", linewidth = 0.3) +
geom_text(
aes(label = format(Fi, big.mark = ",")),
vjust = -0.4, size = 3.5, color = "#222222", fontface = "bold"
) +
scale_fill_manual(values = colores_barras) +
scale_y_continuous(
labels = label_comma(),
expand = expansion(mult = c(0, 0.15))
) +
labs(
title = "Gráfica N°1 — Frecuencia Absoluta",
x = "Nivel de Precisión",
y = "Frecuencia Absoluta (Fi)"
) +
theme_minimal(base_size = 12) +
theme(
legend.position = "none",
plot.title = element_text(face = "bold", color = "#222222", size = 12),
axis.title = element_text(face = "bold", color = "#333333"),
axis.text = element_text(color = "#333333"),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = "#EEEEEE"),
plot.background = element_rect(fill = "white", color = NA),
panel.background = element_rect(fill = "white", color = NA)
)
ggplot(conteo_graf, aes(x = Nivel, y = pct, fill = Nivel)) +
geom_col(width = 0.5, color = "white", linewidth = 0.3) +
geom_text(
aes(label = paste0(pct, "%")),
vjust = -0.4, size = 3.5, color = "#222222", fontface = "bold"
) +
scale_fill_manual(values = colores_barras) +
scale_y_continuous(
labels = function(x) paste0(x, "%"),
expand = expansion(mult = c(0, 0.15))
) +
labs(
title = "Gráfica N°2 — Distribución Porcentual",
x = "Nivel de Precisión",
y = "Porcentaje (%)"
) +
theme_minimal(base_size = 12) +
theme(
legend.position = "none",
plot.title = element_text(face = "bold", color = "#222222", size = 12),
axis.title = element_text(face = "bold", color = "#333333"),
axis.text = element_text(color = "#333333"),
panel.grid.major.x = element_blank(),
panel.grid.major.y = element_line(color = "#EEEEEE"),
plot.background = element_rect(fill = "white", color = NA),
panel.background = element_rect(fill = "white", color = NA)
)
datos_pie <- conteo_graf %>%
mutate(etiqueta = paste0(Nivel, " (", pct, "%)"))
ggplot(datos_pie, aes(x = "", y = pct, fill = Nivel)) +
geom_col(width = 1, color = "white", linewidth = 0.6) +
coord_polar(theta = "y", start = 0) +
scale_fill_manual(
values = colores_pie,
labels = datos_pie$etiqueta
) +
labs(
fill = "Nivel de Precisión"
) +
theme_void(base_size = 12) +
theme(
legend.position = "right",
legend.title = element_text(face = "bold", color = "#222222"),
legend.text = element_text(size = 8, color = "#333333"),
plot.background = element_rect(fill = "white", color = NA)
)
fila_moda <- tdf[which.max(tdf$Fi), ]
tabla_ind <- data.frame(
"Total Registros (N)" = N,
"Niveles Diferentes" = k,
"Nivel Predominante (Moda)" = fila_moda$Nivel,
"Frecuencia de la Moda" = fila_moda$Fi,
"Peso de la Moda" = paste0(fila_moda$pct, "%"),
check.names = FALSE
)
tabla_ind %>%
gt() %>%
tab_header(
title = md("**TABLA 2. Indicadores Estadísticos**")
) %>%
fmt_integer(columns = c(`Total Registros (N)`, `Niveles Diferentes`, `Frecuencia de la Moda`)) %>%
tab_source_note(source_note = "Autor: Grupo 5") %>%
cols_align(align = "center", columns = everything()) %>%
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 = "grey",
table_body.border.bottom.color = "black"
)
| TABLA 2. Indicadores Estadísticos | ||||
| Total Registros (N) | Niveles Diferentes | Nivel Predominante (Moda) | Frecuencia de la Moda | Peso de la Moda |
|---|---|---|---|---|
| 7,538 | 2 | exact | 6,353 | 84.28% |
| Autor: Grupo 5 | ||||