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
Hour <- as.numeric(datos$Hour)
Hour <- Hour[!is.na(Hour)]
3.- Conteo de frecuencias
Li <- c(0, 4, 8, 12, 16, 20)
Ls <- c(4, 8, 12, 16, 20, 23)
MC <- round((Li + Ls) / 2, 1)
ni <- numeric(length(Li))
for(i in 1:length(Li)){
if(i < length(Li)){
ni[i] <- sum(Hour >= Li[i] & Hour < Ls[i])
} else {
ni[i] <- sum(Hour >= Li[i] & Hour <= 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(
"[0 - 4)",
"[4 - 8)",
"[8 - 12)",
"[12 - 16)",
"[16 - 20)",
"[20 - 23]"
)
4.- Tabla de distribución de frecuencias
TDF_Hour <- 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_Hour <- rbind(TDF_Hour, totales)
TDF_Hour %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 1**"),
subtitle = md("*Distribución de la Hora de Medición (Hour) 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 de la Hora de Medición (Hour) en China (2015-2025) |
| Intervalo |
MC |
ni |
hi |
Ni_asc |
Ni_desc |
Hi_asc |
Hi_desc |
| [0 - 4) |
2 |
510 |
17.00 |
510 |
3000 |
17 |
100 |
| [4 - 8) |
6 |
496 |
16.53 |
1006 |
2490 |
33.53 |
83 |
| [8 - 12) |
10 |
530 |
17.67 |
1536 |
1994 |
51.2 |
66.47 |
| [12 - 16) |
14 |
504 |
16.80 |
2040 |
1464 |
68 |
48.8 |
| [16 - 20) |
18 |
499 |
16.63 |
2539 |
960 |
84.63 |
32 |
| [20 - 23] |
21.5 |
461 |
15.37 |
3000 |
461 |
100 |
15.37 |
| 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(6, "Set3")
barplot(
height = ni, names.arg = Intervalo,
main = "Gráfica Nro. 1\nDistribución de la Hora de Medición (Hour)\nen China (2015-2025) - Escala Local",
xlab = "Hora del día (0 - 23 h)", 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 de la Hora de Medición (Hour)\nen China (2015-2025) - Con respecto al total (N = 3000)",
xlab = "Hora del día (0 - 23 h)", ylab = "Cantidad",
col = colores, border = "white", space = 0,
ylim = c(0, length(Hour)), yaxt = "n", las = 1,
cex.names = 0.9, cex.lab = 1, cex.main = 1
)
marcas_y <- seq(0, length(Hour), 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 de la Hora de Medición (Hour)\nen China (2015-2025) - Escala Local",
xlab = "Hora del día (0 - 23 h)", 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 de la Hora de Medición (Hour)\nen China (2015-2025) - Con respecto al total (100%)",
xlab = "Hora del día (0 - 23 h)", 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 HOUR
par(mar = c(4, 6, 4, 2))
boxplot(
Hour, horizontal = TRUE,
main = "Gráfica Nro. 5\nDistribución de la Hora de Medición (Hour)\nen China (2015-2025)",
xlab = "Hora del día (0 - 23 h)",
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 HOUR
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 la Hora\nen China (2015-2025)",
xlab = "Hora del día (0 - 23 h)", 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 HOUR
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 la Hora de Medición (Hour)\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("Hora del día (0 - 23 h)", 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(Hour, na.rm = TRUE)
Me <- median(Hour, na.rm = TRUE)
Mo <- Intervalo[which.max(ni)]
desv <- sd(Hour, na.rm = TRUE)
CV <- (desv / X) * 100
As <- skewness(Hour, na.rm = TRUE)
K <- kurtosis(Hour, na.rm = TRUE)
Variable <- "Hour"
min_h <- min(Hour, na.rm = TRUE)
max_h <- max(Hour, na.rm = TRUE)
Rango <- paste0("[", round(min_h, 0), " - ", round(max_h, 0), "]")
atipicos_reales <- boxplot.stats(Hour)$out
valoresatipicos <- ifelse(
length(atipicos_reales) > 0,
paste0(length(atipicos_reales), " atípicos (> ", round(min(atipicos_reales), 0), ")"),
"Sin valores atípicos"
)
Tabla_indicadores_Hour <- 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_Hour) <- c(
"Variable", "Rango", "X", "Me", "Mo", "sd", "CV", "As", "K", "Valores atípicos"
)
Tabla_indicadores_Hour %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 2**"),
subtitle = md("*Indicadores Estadísticos de la Hora de Medición (Hour) 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 de la Hora de Medición (Hour) en China (2015-2025) |
| Variable |
Rango |
X |
Me |
Mo |
sd |
CV |
As |
K |
Valores atípicos |
| Hour |
[0 - 23] |
11.29 |
11 |
[8 - 12) |
6.84 |
60.54 |
0.02 |
-1.17 |
Sin valores atípicos |
| Autor: Mateo Tapia Fuente: Data set.xlsx (China, 2015-2025) |
7.- Conclusión
atipicos_h <- boxplot.stats(Hour)$out
texto_atipicos <- ifelse(
length(atipicos_h) > 0,
paste0("Se evidencian ", length(atipicos_h), " valores atípicos superiores a ", round(min(atipicos_h), 0), "."),
"No se evidencian valores atípicos en la distribución."
)
df_conclusion_h <- data.frame(
Conclusión = paste(
"En el estudio de la distribución de la Hora de Medición (Hour) en China (2015-2025), la variable fluctúa entre las",
round(min(Hour, na.rm = TRUE), 0), "y las", round(max(Hour, na.rm = TRUE), 0),
"horas, y sus valores giran en torno a las", round(mean(Hour, na.rm = TRUE), 2), "horas.",
texto_atipicos
)
)
df_conclusion_h %>%
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 de la Hora de Medición (Hour) en China (2015-2025), la variable fluctúa entre las 0 y las 23 horas, y sus valores giran en torno a las 11.29 horas. No se evidencian valores atípicos en la distribución. |