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
Year <- as.numeric(datos$Year)
Year <- Year[!is.na(Year)]
3.- Conteo de frecuencias
Li <- c(2015, 2017, 2019, 2021, 2023)
Ls <- c(2017, 2019, 2021, 2023, 2024)
MC <- round((Li + Ls) / 2, 1)
ni <- numeric(length(Li))
for(i in 1:length(Li)){
if(i < length(Li)){
ni[i] <- sum(Year >= Li[i] & Year < Ls[i])
} else {
ni[i] <- sum(Year >= Li[i] & Year <= Ls[i])
}
}
N <- sum(ni)
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(
"[2015 - 2017)",
"[2017 - 2019)",
"[2019 - 2021)",
"[2021 - 2023)",
"[2023 - 2024]"
)
4.- Tabla de distribución de frecuencias
TDF_Year <- 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 <- data.frame(
Intervalo = "Totales", MC = "-", ni = sum(ni), hi = round(sum(hi), 2),
Ni_asc = "-", Ni_desc = "-", Hi_asc = "-", Hi_desc = "-"
)
TDF_Year <- rbind(TDF_Year, totales)
TDF_Year %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 1**"),
subtitle = md("*Distribución del Año de Medición (Year) 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.title.font.size = px(22),
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. 1 |
| Distribución del Año de Medición (Year) en China (2015-2025) |
| Intervalo |
MC |
ni |
hi |
Ni_asc |
Ni_desc |
Hi_asc |
Hi_desc |
| [2015 - 2017) |
2016 |
562 |
18.73 |
562 |
3000 |
18.73 |
100 |
| [2017 - 2019) |
2018 |
612 |
20.40 |
1174 |
2438 |
39.13 |
81.27 |
| [2019 - 2021) |
2020 |
629 |
20.97 |
1803 |
1826 |
60.1 |
60.87 |
| [2021 - 2023) |
2022 |
587 |
19.57 |
2390 |
1197 |
79.67 |
39.9 |
| [2023 - 2024] |
2023.5 |
610 |
20.33 |
3000 |
610 |
100 |
20.33 |
| Totales |
- |
3000 |
100.00 |
- |
- |
- |
- |
| Autor: Mateo Tapia Fuente: Data set.xlsx (China, 2015-2025) |
5.- Gráfica de distribución de frecuencias
5.1 Diagrama de cantidad
# GRÁFICA Nro. 1: DIAGRAMA DE CANTIDAD (ESCALA LOCAL)
colores <- brewer.pal(5, "Set3")
barplot(
height = ni, names.arg = Intervalo,
main = "Gráfica Nro. 1\nDistribución del Año de Medición (Year)\nen China (2015-2025) - Escala Local",
xlab = "Año de medición", ylab = "Cantidad",
col = colores, border = "white", 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: DIAGRAMA DE CANTIDAD CON RESPECTO AL TOTAL
barplot(
height = ni, names.arg = Intervalo,
main = "Gráfica Nro. 2\nDistribución del Año de Medición (Year)\nen China (2015-2025) - Con respecto al total (N = 3000)",
xlab = "Año de medición", ylab = "Cantidad",
col = colores, border = "white", space = 0,
ylim = c(0, length(Year)), yaxt = "n", las = 1,
cex.names = 0.9, cex.lab = 1, cex.main = 1
)
marcas_y <- seq(0, length(Year), 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 Diagrama porcentual
# GRÁFICA Nro. 3: DIAGRAMA PORCENTUAL (ESCALA LOCAL)
barplot(
height = hi, names.arg = Intervalo,
main = "Gráfica Nro. 3\nDistribución Porcentual del Año de Medición (Year)\nen China (2015-2025) - Escala Local",
xlab = "Año de medición", ylab = "Porcentaje",
col = colores, border = "white", 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: DIAGRAMA PORCENTUAL CON RESPECTO AL TODO
barplot(
height = hi, names.arg = Intervalo,
main = "Gráfica Nro. 4\nDistribución Porcentual del Año de Medición (Year)\nen China (2015-2025) - Con respecto al total (100%)",
xlab = "Año de medición", ylab = "Porcentaje",
col = colores, border = "white", 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 cajas
# GRÁFICA Nro. 5: BOXPLOT YEAR
par(mar = c(4, 6, 4, 2))
boxplot(
Year, horizontal = TRUE,
main = "Gráfica Nro. 5\nDistribución del Año de Medición (Year)\nen China (2015-2025)",
xlab = "Año de medición",
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 YEAR
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) del Año\nen China (2015-2025)",
xlab = "Año de medición", 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 YEAR
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 del Año de Medición (Year)\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("Año de medición", 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(Year, na.rm = TRUE)
Me <- median(Year, na.rm = TRUE)
Mo <- Intervalo[which.max(ni)]
desv <- sd(Year, na.rm = TRUE)
CV <- (desv / X) * 100
As <- skewness(Year, na.rm = TRUE)
K <- kurtosis(Year, na.rm = TRUE)
Variable <- "Year"
min_y <- min(Year, na.rm = TRUE)
max_y <- max(Year, na.rm = TRUE)
Rango <- paste0("[", round(min_y, 0), " - ", round(max_y, 0), "]")
atipicos_reales <- boxplot.stats(Year)$out
valoresatipicos <- ifelse(
length(atipicos_reales) > 0,
paste0(length(atipicos_reales), " atípicos (> ", round(min(atipicos_reales), 0), ")"),
"Sin valores atípicos"
)
Tabla_indicadores_Year <- 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_Year) <- c(
"Variable", "Rango", "X", "Me", "Mo", "sd", "CV", "As", "K", "Valores atípicos"
)
Tabla_indicadores_Year %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 2**"),
subtitle = md("*Indicadores Estadísticos del Año de Medición (Year) 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. 2 |
| Indicadores Estadísticos del Año de Medición (Year) en China (2015-2025) |
| Variable |
Rango |
X |
Me |
Mo |
sd |
CV |
As |
K |
Valores atípicos |
| Year |
[2015 - 2024] |
2019.56 |
2020 |
[2019 - 2021) |
2.85 |
0.14 |
-0.01 |
-1.2 |
Sin valores atípicos |
| Autor: Mateo Tapia Fuente: Data set.xlsx (China, 2015-2025) |
7.- Conclusión
atipicos_y <- boxplot.stats(Year)$out
texto_atipicos <- ifelse(
length(atipicos_y) > 0,
paste0("Se evidencian ", length(atipicos_y), " valores atípicos superiores a ", round(min(atipicos_y), 0), "."),
"No se evidencian valores atípicos en la distribución."
)
df_conclusion_y <- data.frame(
Conclusión = paste(
"En el estudio de la distribución del Año de Medición (Year) en China (2015-2025), la variable fluctúa entre",
round(min(Year, na.rm = TRUE), 0), "y", round(max(Year, na.rm = TRUE), 0),
", y sus valores giran en torno a", round(mean(Year, na.rm = TRUE), 2), ".",
texto_atipicos
)
)
df_conclusion_y %>%
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 Año de Medición (Year) en China (2015-2025), la variable fluctúa entre 2015 y 2024 , y sus valores giran en torno a 2019.56 . No se evidencian valores atípicos en la distribución. |