0.- Carga de Librerías
library(readxl)
library(gt)
library(dplyr)
library(e1071)
library(RColorBrewer)
1.- Carga de Datos
datos <- read_excel("Data set.xlsx", sheet = "air_pollution_china", skip = 1)
2.- Selección de variable
SO2 <- as.numeric(datos$`SO2 (µg/m³)`)
SO2 <- SO2[!is.na(SO2)]
3.- Conteo de frecuencias
# --- 3.1 Cálculos para Regla de Sturges ---
N <- length(SO2)
min_val <- min(SO2)
max_val <- max(SO2)
R <- max_val - min_val
k <- round(1 + 3.322 * log10(N))
A <- R / k
cortes_st <- seq(min_val, max_val, length.out = k + 1)
Li_st <- cortes_st[-(k + 1)]
Ls_st <- cortes_st[-1]
MC_st <- round((Li_st + Ls_st) / 2, 2)
ni_st <- numeric(k)
for(i in 1:k){
if(i < k){
ni_st[i] <- sum(SO2 >= Li_st[i] & SO2 < Ls_st[i])
} else {
ni_st[i] <- sum(SO2 >= Li_st[i] & SO2 <= Ls_st[i])
}
}
hi_st <- (ni_st / N) * 100
Ni_asc_st <- cumsum(ni_st)
Ni_desc_st <- rev(cumsum(rev(ni_st)))
Hi_asc_st <- cumsum(hi_st)
Hi_desc_st <- rev(cumsum(rev(hi_st)))
Intervalo_st <- ifelse(
1:k < k,
paste0("[", round(Li_st, 2), " - ", round(Ls_st, 2), ")"),
paste0("[", round(Li_st, 2), " - ", round(Ls_st, 2), "]")
)
# --- 3.2 Cálculos para Intervalos Reducidos ---
Li <- c(0, 10, 20, 30, 40)
Ls <- c(10, 20, 30, 40, 50)
MC <- round((Li + Ls) / 2, 1)
ni <- numeric(length(Li))
for(i in 1:length(Li)){
if(i < length(Li)){
ni[i] <- sum(SO2 >= Li[i] & SO2 < Ls[i])
} else {
ni[i] <- sum(SO2 >= Li[i] & SO2 <= Ls[i])
}
}
hi <- (ni / N) * 100
Ni_asc <- cumsum(ni)
Ni_desc <- rev(cumsum(rev(ni)))
Hi_asc <- cumsum(hi)
Hi_desc <- rev(cumsum(rev(hi)))
Intervalo <- c(
"[0 - 10)",
"[10 - 20)",
"[20 - 30)",
"[30 - 40)",
"[40 - 50]"
)
4.- Tablas de distribución de frecuencias
4.1 Tabla con Regla de Sturges
TDF_Sturges <- data.frame(
Intervalo = Intervalo_st,
MC = MC_st,
ni = ni_st,
hi = round(hi_st, 2),
Ni_asc = Ni_asc_st,
Ni_desc = Ni_desc_st,
Hi_asc = round(Hi_asc_st, 2),
Hi_desc = round(Hi_desc_st, 2)
)
totales_st <- data.frame(
Intervalo = "Totales", MC = "-", ni = sum(ni_st), hi = round(sum(hi_st), 2),
Ni_asc = "-", Ni_desc = "-", Hi_asc = "-", Hi_desc = "-"
)
TDF_Sturges <- rbind(TDF_Sturges, totales_st)
TDF_Sturges %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 1 (Regla de Sturges)**"),
subtitle = md("*Distribución de SO2 (µg/m³) en China (2015-2025) - 13 Intervalos*")
) %>%
tab_source_note(
source_note = md("**Autor:** Mateo Tapia **Fuente:** Data set.xlsx (China, 2015-2025)")
) %>%
opt_table_font(
font = list(google_font("Poppins"), default_fonts())
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.top.width = px(3),
table.border.bottom.width = px(3),
heading.align = "center",
heading.title.font.size = px(20),
heading.subtitle.font.size = px(14),
heading.background.color = "#F4F6F7",
column_labels.font.weight = "bold",
column_labels.font.size = px(13),
column_labels.border.top.color = "black",
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width = px(2),
table_body.hlines.color = "#B3B6B7",
table_body.vlines.color = "#B3B6B7",
row.striping.include_table_body = TRUE,
row.striping.background_color = "#F8F9F9",
table.font.size = px(12),
source_notes.font.size = px(11),
data_row.padding = px(6)
) %>%
tab_style(
style = cell_fill(color = "#D6EAF8"),
locations = cells_column_labels()
) %>%
tab_style(
style = cell_fill(color = "#F4F6F7"),
locations = cells_source_notes()
) %>%
tab_style(
style = cell_borders(sides = c("left", "right"), color = "#A6ACAF", weight = px(1)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = c("left", "right"), color = "black", weight = px(1.5)),
locations = cells_column_labels()
) %>%
cols_align(
align = "center",
columns = everything()
)
| Tabla Nro. 1 (Regla de Sturges) |
| Distribución de SO2 (µg/m³) en China (2015-2025) - 13 Intervalos |
| Intervalo |
MC |
ni |
hi |
Ni_asc |
Ni_desc |
Hi_asc |
Hi_desc |
| [1.01 - 4.78) |
2.9 |
242 |
8.07 |
242 |
3000 |
8.07 |
100 |
| [4.78 - 8.55) |
6.67 |
235 |
7.83 |
477 |
2758 |
15.9 |
91.93 |
| [8.55 - 12.32) |
10.43 |
253 |
8.43 |
730 |
2523 |
24.33 |
84.1 |
| [12.32 - 16.08) |
14.2 |
218 |
7.27 |
948 |
2270 |
31.6 |
75.67 |
| [16.08 - 19.85) |
17.97 |
199 |
6.63 |
1147 |
2052 |
38.23 |
68.4 |
| [19.85 - 23.62) |
21.73 |
216 |
7.20 |
1363 |
1853 |
45.43 |
61.77 |
| [23.62 - 27.39) |
25.5 |
234 |
7.80 |
1597 |
1637 |
53.23 |
54.57 |
| [27.39 - 31.15) |
29.27 |
226 |
7.53 |
1823 |
1403 |
60.77 |
46.77 |
| [31.15 - 34.92) |
33.04 |
265 |
8.83 |
2088 |
1177 |
69.6 |
39.23 |
| [34.92 - 38.69) |
36.8 |
216 |
7.20 |
2304 |
912 |
76.8 |
30.4 |
| [38.69 - 42.46) |
40.57 |
247 |
8.23 |
2551 |
696 |
85.03 |
23.2 |
| [42.46 - 46.22) |
44.34 |
236 |
7.87 |
2787 |
449 |
92.9 |
14.97 |
| [46.22 - 49.99] |
48.11 |
213 |
7.10 |
3000 |
213 |
100 |
7.1 |
| Totales |
- |
3000 |
100.00 |
- |
- |
- |
- |
| Autor: Mateo Tapia Fuente: Data set.xlsx (China, 2015-2025) |
4.2 Tabla de Intervalos Reducidos
TDF_Reducida <- data.frame(
Intervalo, MC, ni, hi = round(hi, 2),
Ni_asc, Ni_desc, Hi_asc = round(Hi_asc, 2), Hi_desc = round(Hi_desc, 2)
)
totales_red <- data.frame(
Intervalo = "Totales", MC = "-", ni = sum(ni), hi = round(sum(hi), 2),
Ni_asc = "-", Ni_desc = "-", Hi_asc = "-", Hi_desc = "-"
)
TDF_Reducida <- rbind(TDF_Reducida, totales_red)
TDF_Reducida %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 2 (Intervalos Reducidos)**"),
subtitle = md("*Distribución de SO2 (µg/m³) en China (2015-2025) - 5 Intervalos*")
) %>%
tab_source_note(
source_note = md("**Autor:** Mateo Tapia **Fuente:** Data set.xlsx (China, 2015-2025)")
) %>%
opt_table_font(
font = list(google_font("Poppins"), default_fonts())
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.top.width = px(3),
table.border.bottom.width = px(3),
heading.align = "center",
heading.title.font.size = px(20),
heading.subtitle.font.size = px(14),
heading.background.color = "#F4F6F7",
column_labels.font.weight = "bold",
column_labels.font.size = px(14),
column_labels.border.top.color = "black",
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width = px(2),
table_body.hlines.color = "#B3B6B7",
table_body.vlines.color = "#B3B6B7",
row.striping.include_table_body = TRUE,
row.striping.background_color = "#F8F9F9",
table.font.size = px(13),
source_notes.font.size = px(11),
data_row.padding = px(8)
) %>%
tab_style(
style = cell_fill(color = "#D6EAF8"),
locations = cells_column_labels()
) %>%
tab_style(
style = cell_fill(color = "#F4F6F7"),
locations = cells_source_notes()
) %>%
tab_style(
style = cell_borders(sides = c("left", "right"), color = "#A6ACAF", weight = px(1)),
locations = cells_body()
) %>%
tab_style(
style = cell_borders(sides = c("left", "right"), color = "black", weight = px(1.5)),
locations = cells_column_labels()
) %>%
cols_align(
align = "center",
columns = everything()
)
| Tabla Nro. 2 (Intervalos Reducidos) |
| Distribución de SO2 (µg/m³) en China (2015-2025) - 5 Intervalos |
| Intervalo |
MC |
ni |
hi |
Ni_asc |
Ni_desc |
Hi_asc |
Hi_desc |
| [0 - 10) |
5 |
573 |
19.10 |
573 |
3000 |
19.1 |
100 |
| [10 - 20) |
15 |
582 |
19.40 |
1155 |
2427 |
38.5 |
80.9 |
| [20 - 30) |
25 |
602 |
20.07 |
1757 |
1845 |
58.57 |
61.5 |
| [30 - 40) |
35 |
632 |
21.07 |
2389 |
1243 |
79.63 |
41.43 |
| [40 - 50] |
45 |
611 |
20.37 |
3000 |
611 |
100 |
20.37 |
| Totales |
- |
3000 |
100.00 |
- |
- |
- |
- |
| Autor: Mateo Tapia Fuente: Data set.xlsx (China, 2015-2025) |
5.- Gráficas de distribución de frecuencias
5.1 Histogramas de cantidad
# GRÁFICA Nro. 1: HISTOGRAMA DE CANTIDAD (ESCALA LOCAL)
colores <- brewer.pal(5, "Set3")
barplot(
height = ni, names.arg = Intervalo,
main = "Gráfica Nro. 1\nHistograma de Cantidad de SO2 (µg/m³)\nen China (2015-2025) - Escala Local",
xlab = "Concentración SO2 (µg/m³)", ylab = "Cantidad",
col = colores, border = "black", space = 0, las = 1,
cex.names = 0.9, cex.lab = 1, cex.main = 1
)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

# GRÁFICA Nro. 2: HISTOGRAMA DE CANTIDAD CON RESPECTO AL TOTAL
barplot(
height = ni, names.arg = Intervalo,
main = "Gráfica Nro. 2\nHistograma de Cantidad de SO2 (µg/m³)\nen China (2015-2025) - Con respecto al total (N = 3000)",
xlab = "Concentración SO2 (µg/m³)", ylab = "Cantidad",
col = colores, border = "black", space = 0,
ylim = c(0, length(SO2)), yaxt = "n", las = 1,
cex.names = 0.9, cex.lab = 1, cex.main = 1
)
marcas_y <- seq(0, length(SO2), by = 500)
axis(2, at = marcas_y, labels = marcas_y, las = 1, cex.axis = 0.9)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

5.2 Histogramas porcentuales
# GRÁFICA Nro. 3: HISTOGRAMA PORCENTUAL (ESCALA LOCAL)
barplot(
height = hi, names.arg = Intervalo,
main = "Gráfica Nro. 3\nHistograma Porcentual de SO2 (µg/m³)\nen China (2015-2025) - Escala Local",
xlab = "Concentración SO2 (µg/m³)", ylab = "Porcentaje",
col = colores, border = "black", space = 0, las = 1,
cex.names = 0.9, cex.lab = 1, cex.main = 1, yaxt = "n"
)
marcas_y <- pretty(c(0, max(hi)))
axis(2, at = marcas_y, labels = paste0(marcas_y, "%"), las = 1, cex.axis = 0.9)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

# GRÁFICA Nro. 4: HISTOGRAMA PORCENTUAL CON RESPECTO AL TODO
barplot(
height = hi, names.arg = Intervalo,
main = "Gráfica Nro. 4\nHistograma Porcentual de SO2 (µg/m³)\nen China (2015-2025) - Con respecto al total (100%)",
xlab = "Concentración SO2 (µg/m³)", ylab = "Porcentaje",
col = colores, border = "black", space = 0,
ylim = c(0, 100), yaxt = "n", las = 1,
cex.names = 0.9, cex.lab = 1, cex.main = 1
)
marcas_y <- seq(0, 100, by = 20)
axis(2, at = marcas_y, labels = paste0(marcas_y, "%"), las = 1, cex.axis = 0.9)
grid(nx = NA, ny = NULL, col = "gray85", lty = 1)

5.3 Diagrama de caja
# GRÁFICA Nro. 5: BOXPLOT SO2
par(mar = c(4, 6, 4, 2))
boxplot(
SO2, horizontal = TRUE,
main = "Gráfica Nro. 5\nDiagrama de Caja de SO2 (µg/m³)\nen China (2015-2025)",
xlab = "Concentración SO2 (µg/m³)",
col = "#76D7C4", border = "black", pch = 1,
cex.main = 1, cex.lab = 1, las = 1
)
grid(nx = NULL, ny = NA, col = "gray85", lty = 1)

5.4 Diagrama de Ojivas
5.4.1 Ojivas (Ni)
# GRÁFICA NRO. 6: OJIVAS LOCALES SO2
par(mar = c(5, 6, 4, 2) + 0.1)
x <- Ls
plot(
x, Ni_asc, type = "b", col = "orange", pch = 19, lwd = 2,
main = "Gráfica Nro. 6: Distribución Acumulada (Ojivas) de SO2 (µg/m³)\nen China (2015-2025)",
xlab = "Concentración SO2 (µg/m³)", ylab = "Cantidad",
ylim = c(0, N), xaxt = "n", yaxt = "n", las = 1
)
lines(x, Ni_desc, type = "b", col = "blue", pch = 19, lwd = 2)
axis(1, at = x, labels = x, las = 1, cex.axis = 0.9)
marcas_y <- pretty(c(0, N))
axis(2, at = marcas_y, labels = marcas_y, las = 1, cex.axis = 0.9)
grid(nx = NULL, ny = NA, col = "gray85", lty = 1)
legend(
"right",
legend = c("Ascendente (Ojiva Menor que)", "Descendente (Ojiva Mayor que)"),
col = c("orange", "blue"), pch = 19, lty = 1, lwd = 2, bty = "n", cex = 0.9
)

5.4.2 Ojivas (Hi)
# GRÁFICA NRO. 7: OJIVAS PORCENTUALES SO2
par(mar = c(6, 5, 4, 2) + 0.1)
x <- Ls
plot(
x, Hi_asc, type = "b", col = "orange", pch = 19, lwd = 2,
main = "Gráfica Nro. 7: Ojiva Porcentual de SO2 (µg/m³)\nen China (2015-2025)",
xlab = "", ylab = "Porcentaje",
ylim = c(0, 100), xaxt = "n", yaxt = "n",
panel.first = grid(col = "lightgray", lty = 2)
)
lines(x, Hi_desc, type = "b", col = "blue", pch = 19, lwd = 2)
axis(1, at = x, labels = x, las = 1, cex.axis = 0.9)
mtext("Concentración SO2 (µg/m³)", side = 1, line = 3.5)
marcas_y <- seq(0, 100, by = 20)
axis(2, at = marcas_y, labels = paste0(marcas_y, "%"), las = 1, cex.axis = 0.9)
legend(
"right", legend = c("Ascendente", "Descendente"),
col = c("orange", "blue"), pch = 19, lty = 1, lwd = 2, bty = "n", cex = 0.9
)

6.- Indicadores
X <- mean(SO2, na.rm = TRUE)
Me <- median(SO2, na.rm = TRUE)
Mo <- Intervalo[which.max(ni)]
desv <- sd(SO2, na.rm = TRUE)
CV <- (desv / X) * 100
As <- skewness(SO2, na.rm = TRUE)
K <- kurtosis(SO2, na.rm = TRUE)
Variable <- "SO2 (µg/m³)"
Rango <- paste0("[", round(min(SO2), 2), " - ", round(max(SO2), 2), "]")
atipicos_reales <- boxplot.stats(SO2)$out
valoresatipicos <- ifelse(
length(atipicos_reales) > 0,
paste0(length(atipicos_reales), " atípicos (> ", round(min(atipicos_reales), 2), ")"),
"Sin valores atípicos"
)
Tabla_indicadores_SO2 <- data.frame(
Variable = Variable, Rango = Rango, X = round(X, 2), Me = round(Me, 2),
Mo = Mo, sd = round(desv, 2), CV = round(CV, 2), As = round(As, 2),
K = round(K, 2), valoresatipicos = valoresatipicos
)
colnames(Tabla_indicadores_SO2) <- c(
"Variable", "Rango", "X", "Me", "Mo", "sd", "CV", "As", "K", "Valores atípicos"
)
Tabla_indicadores_SO2 %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 3**"),
subtitle = md("*Indicadores Estadísticos de SO2 (µg/m³) en China (2015-2025)*")
) %>%
tab_source_note(
source_note = md("**Autor:** Mateo Tapia **Fuente:** Data set.xlsx (China, 2015-2025)")
) %>%
opt_table_font(
font = list(google_font("Poppins"), default_fonts())
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.top.width = px(3),
table.border.bottom.width = px(3),
heading.align = "center",
heading.background.color = "#F4F6F7",
column_labels.font.weight = "bold",
column_labels.border.top.color = "black",
column_labels.border.bottom.color = "black",
column_labels.border.bottom.width = px(2),
row.striping.include_table_body = TRUE,
row.striping.background_color = "#F8F9F9",
table_body.hlines.color = "#B3B6B7",
table_body.border.bottom.color = "black"
) %>%
tab_style(
style = cell_fill(color = "#D6EAF8"),
locations = cells_column_labels()
) %>%
cols_align(
align = "center",
columns = everything()
)
| Tabla Nro. 3 |
| Indicadores Estadísticos de SO2 (µg/m³) en China (2015-2025) |
| Variable |
Rango |
X |
Me |
Mo |
sd |
CV |
As |
K |
Valores atípicos |
| SO2 (µg/m³) |
[1.01 - 49.99] |
25.43 |
25.68 |
[30 - 40) |
14.18 |
55.76 |
-0.02 |
-1.23 |
Sin valores atípicos |
| Autor: Mateo Tapia Fuente: Data set.xlsx (China, 2015-2025) |
7.- Conclusión
atipicos_so2 <- boxplot.stats(SO2)$out
texto_atipicos <- ifelse(
length(atipicos_so2) > 0,
paste0("Se evidencian ", length(atipicos_so2), " valores atípicos superiores a ", round(min(atipicos_so2), 2), "."),
"No se evidencian valores atípicos en la distribución."
)
df_conclusion_so2 <- data.frame(
Conclusión = paste(
"En el estudio de la distribución del Dióxido de Azufre SO2 (µg/m³) en China (2015-2025), la variable fluctúa entre",
round(min(SO2, na.rm = TRUE), 2), "y", round(max(SO2, na.rm = TRUE), 2),
"µg/m³, y sus valores giran en torno a", round(mean(SO2, na.rm = TRUE), 2), "µg/m³.",
texto_atipicos
)
)
df_conclusion_so2 %>%
gt() %>%
tab_header(
title = md("**Conclusión**")
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
table.border.top.width = px(3),
table.border.bottom.width = px(3),
heading.align = "center"
) %>%
tab_style(
style = cell_text(align = "justify", v_align = "middle"),
locations = cells_body(columns = Conclusión)
)
| Conclusión |
| Conclusión |
| En el estudio de la distribución del Dióxido de Azufre SO2 (µg/m³) en China (2015-2025), la variable fluctúa entre 1.01 y 49.99 µg/m³, y sus valores giran en torno a 25.43 µg/m³. No se evidencian valores atípicos en la distribución. |