Gráfica apilada y porcentaje de conteo de plásticos
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.4 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
library(scales)
## Warning: package 'scales' was built under R version 4.4.3
##
## Adjuntando el paquete: 'scales'
##
## The following object is masked from 'package:purrr':
##
## discard
##
## The following object is masked from 'package:readr':
##
## col_factor
conteo = read_excel("Conteo_material_nido.xlsx")
conteo
## # A tibble: 50 × 3
## Nido Material Conteo
## <chr> <chr> <dbl>
## 1 P18 Felpa 2
## 2 P18 Fragmentos 1
## 3 P87_N1 Felpa 10
## 4 P87_N1 Fibras 6
## 5 P87_N2 Felpa 1
## 6 P87_N2 Fibras 9
## 7 P87_N3 Felpa 1
## 8 P87_N3 Fibras 3
## 9 P177_N1 Felpa 4
## 10 P177_N1 Fragmentos 51
## # ℹ 40 more rows
conteo_pct <- conteo %>%
group_by(Nido) %>%
mutate(Porcentaje = Conteo / sum(Conteo))
conteo_pct
## # A tibble: 50 × 4
## # Groups: Nido [14]
## Nido Material Conteo Porcentaje
## <chr> <chr> <dbl> <dbl>
## 1 P18 Felpa 2 0.667
## 2 P18 Fragmentos 1 0.333
## 3 P87_N1 Felpa 10 0.625
## 4 P87_N1 Fibras 6 0.375
## 5 P87_N2 Felpa 1 0.1
## 6 P87_N2 Fibras 9 0.9
## 7 P87_N3 Felpa 1 0.25
## 8 P87_N3 Fibras 3 0.75
## 9 P177_N1 Felpa 4 0.00924
## 10 P177_N1 Fragmentos 51 0.118
## # ℹ 40 more rows
ggplot(conteo_pct, aes(x = Nido, y = Porcentaje, fill = Material)) +
geom_bar(stat = "identity") +
scale_y_continuous(labels = percent_format()) +
geom_text(aes(label = percent(Porcentaje)),
position = position_stack(vjust = 0.5),
size = 3) +
theme_minimal() +
labs(title = "Porcentaje de plástico por Nido",
x = "Identificador del Nido",
y = "Porcentaje",
fill = "Tipo de Material") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

ggplot(conteo, aes(x = Nido, y = Conteo, fill = Material)) +
geom_bar(stat = "identity", position = "fill") +
theme_minimal() +
labs(title = "Porcentaje de plástico por Nido",
x = "Identificador del Nido",
y = "Cantidad de plástico",
fill = "Tipo de Material") +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

tabla_proporciones <- conteo %>%
group_by(Nido) %>%
mutate(Total_Nido = sum(Conteo),
Proporcion = Conteo / Total_Nido,
Porcentaje = Proporcion * 100) %>%
ungroup()
# Ver el resultado
print(tabla_proporciones)
## # A tibble: 50 × 6
## Nido Material Conteo Total_Nido Proporcion Porcentaje
## <chr> <chr> <dbl> <dbl> <dbl> <dbl>
## 1 P18 Felpa 2 3 0.667 66.7
## 2 P18 Fragmentos 1 3 0.333 33.3
## 3 P87_N1 Felpa 10 16 0.625 62.5
## 4 P87_N1 Fibras 6 16 0.375 37.5
## 5 P87_N2 Felpa 1 10 0.1 10
## 6 P87_N2 Fibras 9 10 0.9 90
## 7 P87_N3 Felpa 1 4 0.25 25
## 8 P87_N3 Fibras 3 4 0.75 75
## 9 P177_N1 Felpa 4 433 0.00924 0.924
## 10 P177_N1 Fragmentos 51 433 0.118 11.8
## # ℹ 40 more rows