#------------------ UNIVERSIDAD CENTRAL DEL ECUADOR
##-----Facultad de Ingeniería en Geología, Minas, Petróleos y Ambiental
###-----------------Carrera de Ingeniería Ambiental
Proyecto: Estudio de Contaminación por Plásticos a
Nivel Mundial
Etapa: Estadística Descriptiva
Variable cualitativa nominal: Continente
# Integrantes: Jhoao Cueva, Deisy Guasti, Marck Pillajo, María José Samaniego, Carlos Yugsi
# Fecha: 16/11/2026
# Paquetes (solo la primera vez): install.packages(c("dplyr", "gt"))
library(dplyr)
##
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(gt)
Se trabaja con el dataset depurado. Si guardaste el archivo en otra carpeta, cambia la ruta.
archivo <- "C:/Users/HOME/Downloads/dataset_depurado.csv"
datos <- read.csv(archivo, stringsAsFactors = FALSE,
na.strings = c("", "NA"), fileEncoding = "UTF-8")
nrow(datos)
## [1] 49667
ni: frecuencia absoluta. hi: frecuencia relativa (proporción). hi(%): frecuencia relativa porcentual.
TDF <- data.frame(Categoria = datos$CONTINENT) %>%
filter(!is.na(Categoria) & Categoria != "") %>%
count(Categoria, name = "ni") %>%
arrange(desc(ni))
TDF <- TDF %>%
mutate(
Categoria = as.character(Categoria),
hi = round(ni / sum(ni), 4),
hi_pct = round((ni / sum(ni)) * 100, 2)
)
# Agregar fila TOTAL
fila_total <- data.frame(Categoria = "TOTAL", ni = sum(TDF$ni), hi = 1, hi_pct = 100)
TDF_suma <- bind_rows(TDF, fila_total)
# Mostrar tabla estilizada con gt
TDF_suma %>%
gt() %>%
tab_header(
title = md("**Tabla Nro. 1**"),
subtitle = "Distribución de frecuencias de la variable Continente en el Estudio de Contaminación por Plásticos a Nivel Mundial"
) %>%
cols_label(Categoria = "Continente", ni = "ni", hi = "hi", hi_pct = "hi(%)") %>%
fmt_number(columns = "ni", decimals = 0, use_seps = FALSE) %>%
fmt_number(columns = "hi", decimals = 4) %>%
fmt_number(columns = "hi_pct", decimals = 2) %>%
cols_align(align = "center", columns = c("ni", "hi", "hi_pct")) %>%
tab_style(
style = list(cell_text(weight = "bold"), cell_fill(color = "#E3EAF2")),
locations = cells_body(rows = Categoria == "TOTAL")
) %>%
tab_source_note(
source_note = md("Elaborado por: Grupo 2 <br> Fuente: dataset de contaminación por plásticos a nivel mundial (depurado)")
) %>%
tab_options(
table.border.top.color = "black",
table.border.bottom.color = "black",
column_labels.border.bottom.width = px(2),
row.striping.include_table_body = TRUE,
heading.border.bottom.width = px(2)
)
| Tabla Nro. 1 | |||
| Distribución de frecuencias de la variable Continente en el Estudio de Contaminación por Plásticos a Nivel Mundial | |||
| Continente | ni | hi | hi(%) |
|---|---|---|---|
| North America | 35168 | 0.7118 | 71.18 |
| Europe | 6212 | 0.1257 | 12.57 |
| Asia | 4204 | 0.0851 | 8.51 |
| South America | 1785 | 0.0361 | 3.61 |
| Oceania | 1027 | 0.0208 | 2.08 |
| Africa | 1011 | 0.0205 | 2.05 |
| Antarctica | 1 | 0.0000 | 0.00 |
| TOTAL | 49408 | 1.0000 | 100.00 |
| Elaborado por: Grupo 2 Fuente: dataset de contaminación por plásticos a nivel mundial (depurado) |
|||
Colores y nombres que se usan en todas las gráficas.
col_ni <- "#2B5C8F" # azul formal (frecuencia absoluta)
col_hi <- "#3E8E87" # verde azulado formal (frecuencia relativa)
paleta <- colorRampPalette(c("#1F3A5F", "#4F81A8", "#B8CCDC"))(nrow(TDF))
nombres <- ifelse(nchar(TDF$Categoria) > 27,
paste0(substr(TDF$Categoria, 1, 24), "..."), TDF$Categoria)
mb <- max(6, min(14, max(nchar(nombres)) * 0.45)) # margen inferior
par(mar = c(mb, 6.5, 4, 2))
g1 <- barplot(TDF$ni, names.arg = nombres, col = col_ni, border = NA,
main = "Gráfico Nro. 1: Distribución Global de Continente (ni)",
xlab = "", ylab = "", ylim = c(0, sum(TDF$ni)),
las = 2, cex.names = 0.85, cex.axis = 0.85, cex.main = 1)
title(xlab = "Continente", line = mb - 1.5)
title(ylab = "Cantidad", line = 4.5)
text(g1, TDF$ni, labels = TDF$ni, pos = 3, cex = 0.8, col = "black")
par(mar = c(mb, 6.5, 4, 2))
g2 <- barplot(TDF$ni, names.arg = nombres, col = col_ni, border = NA,
main = "Gráfico Nro. 2: Distribución Local de Continente (ni)",
xlab = "", ylab = "", ylim = c(0, max(TDF$ni) * 1.15),
las = 2, cex.names = 0.85, cex.axis = 0.85, cex.main = 1)
title(xlab = "Continente", line = mb - 1.5)
title(ylab = "Cantidad", line = 4.5)
text(g2, TDF$ni, labels = TDF$ni, pos = 3, cex = 0.8, col = "black")
par(mar = c(mb, 6.5, 4, 2))
g3 <- barplot(TDF$hi_pct, names.arg = nombres, col = col_hi, border = NA,
main = "Gráfico Nro. 3: Distribución Global Porcentual de Continente (hi%)",
xlab = "", ylab = "", ylim = c(0, 100),
las = 2, cex.names = 0.85, cex.axis = 0.85, cex.main = 1)
title(xlab = "Continente", line = mb - 1.5)
title(ylab = "Porcentaje (%)", line = 4.5)
text(g3, TDF$hi_pct, labels = paste0(TDF$hi_pct, "%"), pos = 3, cex = 0.8, col = "black")
par(mar = c(mb, 6.5, 4, 2))
g4 <- barplot(TDF$hi_pct, names.arg = nombres, col = col_hi, border = NA,
main = "Gráfico Nro. 4: Distribución Local Porcentual de Continente (hi%)",
xlab = "", ylab = "", ylim = c(0, max(TDF$hi_pct) * 1.15),
las = 2, cex.names = 0.85, cex.axis = 0.85, cex.main = 1)
title(xlab = "Continente", line = mb - 1.5)
title(ylab = "Porcentaje (%)", line = 4.5)
text(g4, TDF$hi_pct, labels = paste0(TDF$hi_pct, "%"), pos = 3, cex = 0.8, col = "black")
El porcentaje de cada categoría se escribe afuera del círculo y la leyenda indica a qué categoría corresponde cada color.
# El círculo va a la izquierda, con los porcentajes afuera, y la leyenda en recuadro a la derecha
layout(matrix(c(1, 2), nrow = 1), widths = c(3, 2))
par(mar = c(1.5, 2, 4.5, 0), xpd = TRUE)
r_pie <- 0.68
pie(TDF$hi_pct, labels = rep("", nrow(TDF)),
col = paleta, border = "black", radius = r_pie,
clockwise = TRUE, init.angle = 90,
main = "Gráfico Nro. 5: Distribución Porcentual de Continente")
# Cálculo de posiciones con espacio extra arriba para apilar etiquetas sin tocar el título
prop <- TDF$ni / sum(TDF$ni)
ang <- (90 - 360 * (cumsum(prop) - prop / 2)) * pi / 180
x0 <- r_pie * cos(ang)
y0 <- r_pie * sin(ang)
x1 <- 1.14 * r_pie * cos(ang)
y1 <- 1.14 * r_pie * sin(ang)
dy <- strheight("X", cex = 0.85) * 1.25
separar_arriba <- function(y, dy) {
o <- order(y, decreasing = FALSE)
yy <- y[o]
if (length(yy) > 1) {
for (k in 2:length(yy)) {
if (yy[k] < yy[k - 1] + dy) yy[k] <- yy[k - 1] + dy
}
}
y[o] <- yy
y
}
der <- cos(ang) >= 0
y1[der] <- separar_arriba(y1[der], dy)
y1[!der] <- separar_arriba(y1[!der], dy)
x2 <- x1 + ifelse(der, 0.08, -0.08)
segments(x0, y0, x1, y1)
segments(x1, y1, x2, y1)
text(x2, y1, labels = sprintf("%.2f %%", TDF$hi_pct),
pos = ifelse(der, 4, 2), cex = 0.85, offset = 0.2)
# Leyenda en recuadro
par(mar = c(1.5, 0, 4.5, 2))
plot.new()
legend("topleft", inset = c(0.02, 0.1), legend = nombres,
fill = paleta, title = "Leyenda", cex = 0.9)
layout(1)