UNIVERSIDAD CENTRAL DEL ECUADOR

PROYECTO:ESTUDIO ESTADÍSTICO DE LA CALIDAD DEL AIRE EN INDIA

FECHA: 21/11/2025

#Estadística Descriptiva
#Ariana Viteri
#20/11/2025

library(gt)
library(dplyr)
library(RColorBrewer)

#Cargar los datos 
datos <- read.csv("~/ariana tercer semestre/Estadistica/city_day.csv",
                  header = TRUE, dec = ".", sep = ",")

# ================================
# ORDENAR AQI_Bucket COMO VARIABLE ORDINARIA
# ================================

# ORDEN de jerarquía (del mejor al peor)
datos$AQI_Bucket <- factor(
  datos$AQI_Bucket,
  levels = c("Good", "Satisfactory", "Moderate",
             "Poor", "Very Poor", "Severe","-"),
  ordered = TRUE
)

# ================================
# TABLAS CUALITATIVAS NOMINALES
# ================================

AQI_Bucket <- datos$AQI_Bucket

#Tabla de distribución de frecuencia
TDF_AQI_Bucket <- data.frame(table(AQI_Bucket))
ni <- TDF_AQI_Bucket$Freq
hi <- round((ni / sum(ni)) * 100, 2)
AQI_Bucket <- TDF_AQI_Bucket$AQI_Bucket

TDF_AQI_Bucket <- data.frame(AQI_Bucket, ni, hi)
Summary <- data.frame(AQI_Bucket = "TOTAL", ni = sum(ni), hi = 100)

TDF_AQI_Bucket_suma <- rbind(TDF_AQI_Bucket, Summary)
colnames(TDF_AQI_Bucket_suma) <- c("AQI_Nivel", "ni", "hi(%)")

# ================================
# TABLA GT
# ================================

TDF_AQI_Bucket_suma %>%
  gt() %>%
  tab_header(
    title = md("Tabla Nro. 5"),
    subtitle = md("Tabla de distribución de frecuencias del nivel de contaminación")
  ) %>%
  tab_source_note(
    source_note = md("Fuente: Datos procesados por el autor a partir de archivo city.day.csv")
  ) %>%
  tab_style(
    style = cell_borders(sides = "left", color = "black", weight = px(2)),
    locations = cells_body()
  ) %>%
  tab_style(
    style = cell_borders(sides = "right", color = "black", weight = px(2)),
    locations = cells_body()
  ) %>%
  tab_style(
    style = cell_borders(sides = "left", color = "black", weight = px(2)),
    locations = cells_column_labels()
  ) %>%
  tab_style(
    style = cell_borders(sides = "right", color = "black", weight = px(2)),
    locations = cells_column_labels()
  ) %>%
  tab_options(
    table.border.top.color = "black",
    table.border.bottom.color = "black",
    table.border.top.style = "solid",
    table.border.bottom.style = "solid",
    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,
    heading.border.bottom.color = "black",
    heading.border.bottom.width = px(2),
    table_body.hlines.color = "gray",
    table_body.border.bottom.color = "black"
  )
Tabla Nro. 5
Tabla de distribución de frecuencias del nivel de contaminación
AQI_Nivel ni hi(%)
Good 1341 4.54
Satisfactory 8224 27.85
Moderate 8829 29.90
Poor 2781 9.42
Very Poor 2337 7.91
Severe 1338 4.53
- 4681 15.85
TOTAL 29531 100.00
Fuente: Datos procesados por el autor a partir de archivo city.day.csv
#DIAGRAMAS
# ================================

# GDF 1
TDF_AQI_Bucket$Zona <- iconv(TDF_AQI_Bucket$AQI_Bucket, from = "latin1", to = "UTF-8", sub = "")

library(RColorBrewer)

n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)

barplot(
  height = TDF_AQI_Bucket$ni,
  names.arg = TDF_AQI_Bucket$AQI_Bucket,
  main = "Gráfica No. 1.1: Distribución de AQI_Bucket",
  xlab = "",
  ylab = "Cantidad",
  col = heat.colors(length(TDF_AQI_Bucket$ni)),
  las = 2,
  cex.names = 0.7
)
mtext("AQI_Bucket", side = 1, line = 4, cex = 1)

# DIAGRAMA DE BARRAS 
library(RColorBrewer)

n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)

barplot(
  TDF_AQI_Bucket$hi,
  main = "Gráfica No 1.3: Distribución en categoria de AQI_Bucket",
  xlab = "",
  ylab = "Porcentaje (%)",
  col = colores,
  names.arg = TDF_AQI_Bucket$AQI_Bucket,
  ylim = c(0, 30),
  las = 2,
  cex.names = 0.7
)
mtext("AQI_Bucket", side = 1, line = 4, cex = 1)

# DIAGRAMA DE BARRAS
colores <- c("yellow", "orange")
library(RColorBrewer)

n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)

barplot(
  TDF_AQI_Bucket$hi,
  main = "Gráfica N°1.4: Distribución de AQI_Bucket",
  xlab = "",
  ylab = "Porcentaje (%)",
  col = colores,
  names.arg = TDF_AQI_Bucket$AQI_Bucket,
  ylim = c(0, 100),
  las = 2,
  cex.names = 0.7
)
mtext("AQI_Bucket", side = 1, line = 3, cex = 1)

# DIAGRAMA CIRCULAR
# ================================

n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)
etiqueta <- paste(hi, "%")

library(RColorBrewer)

n <- length(TDF_AQI_Bucket$AQI_Bucket)
colores <- brewer.pal(min(n, 15), "Set3")
color <- adjustcolor(colores, alpha.f = 0.9)

pie(
  hi,
  labels = etiqueta,
  radius = 0.8,
  col = color,
  main = "Gráfica No.1.5: Calidad de aire con AQI_Bucket"
)

legend(
  x = 1.1, y = 1.07,
  legend = TDF_AQI_Bucket$AQI_Bucket,
  title = "Leyenda",
  fill = color,
  cex = 0.65
)