# Cargar paquetes necesarios
install.packages("dplyr")
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.5'
## (as 'lib' is unspecified)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
setwd("/cloud/project")
# Cargar datos
datos <- read.csv("Datos Mineros.csv",
header = TRUE,
sep = ";",
dec = ".",
fileEncoding = "latin1")
library(dplyr)
library(stringr)
library(gt)
# Extraer la variable
CAUSAS_DEL_DAÑO <- datos$INJURY_SOURCE
# Convertimos a dataframe
df_causas <- data.frame(variable = CAUSAS_DEL_DAÑO)
# AGRUPACIÓN POR PALABRAS CLAVE
df_causas$grupo <- case_when(
str_detect(df_causas$variable, regex("NO VALUE", ignore_case = TRUE)) ~ "SIN VALOR",
str_detect(df_causas$variable, regex("METAL", ignore_case = TRUE)) ~ "METALES / PIEZAS",
str_detect(df_causas$variable, regex("GROUND|ROCK|COAL|ORE|CAVING", ignore_case = TRUE)) ~ "ROCA / MINERAL",
str_detect(df_causas$variable, regex("MINE FLOOR|BOTTOM|FOOTWAL", ignore_case = TRUE)) ~ "PISO / SUPERFICIE",
str_detect(df_causas$variable, regex("MACHINES", ignore_case = TRUE)) ~ "MAQUINARIA / EQUIPO",
str_detect(df_causas$variable, regex("DUST", ignore_case = TRUE)) ~ "POLVO / MINERAL MOLIDO",
TRUE ~ "MISCELÁNEO"
)
# TABLA DE FRECUENCIAS AGRUPADA
TDFgrupoCausas <- df_causas %>%
group_by(grupo) %>%
summarise(ni = n()) %>%
mutate(hi = round((ni / sum(ni)) * 100, 0)) %>%
arrange(desc(ni)) # Orden descendente por frecuencia
# Agregar totales
total_ni <- sum(TDFgrupoCausas$ni)
total_hi <- 100
TDFgrupoCausasCompleto <- rbind(
TDFgrupoCausas,
data.frame(grupo = "TOTAL", ni = total_ni, hi = total_hi)
)
print(TDFgrupoCausasCompleto)
## # A tibble: 8 × 3
## grupo ni hi
## <chr> <int> <dbl>
## 1 MISCELÁNEO 2088 42
## 2 ROCA / MINERAL 916 19
## 3 METALES / PIEZAS 855 17
## 4 SIN VALOR 591 12
## 5 MAQUINARIA / EQUIPO 202 4
## 6 PISO / SUPERFICIE 163 3
## 7 POLVO / MINERAL MOLIDO 130 3
## 8 TOTAL 4945 100
# TABLA GT FINAL AGRUPADA
tabla_causas_grupo <- TDFgrupoCausasCompleto %>%
gt() %>%
tab_header(
title = md("Tabla N° 1"),
subtitle = md("Distribución de Frecuencias Agrupadas de INJURY_SOURCE")
) %>%
tab_source_note(
source_note = md("Autor: Grupo1")
) %>%
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"
) %>%
tab_style(
style = cell_text(weight = "bold"),
locations = cells_body(
rows = grupo == "TOTAL"
)
)
tabla_causas_grupo
| Tabla N° 1 |
| Distribución de Frecuencias Agrupadas de INJURY_SOURCE |
| grupo |
ni |
hi |
| MISCELÁNEO |
2088 |
42 |
| ROCA / MINERAL |
916 |
19 |
| METALES / PIEZAS |
855 |
17 |
| SIN VALOR |
591 |
12 |
| MAQUINARIA / EQUIPO |
202 |
4 |
| PISO / SUPERFICIE |
163 |
3 |
| POLVO / MINERAL MOLIDO |
130 |
3 |
| TOTAL |
4945 |
100 |
| Autor: Grupo1 |
# GRÁFICOS DE DISTRIBUCIÓN DE FRECUENCIA AGRUPADA
Colores <- colorRampPalette(c("lightskyblue", "darkblue"))
# 1. Diagrama de barras ni
barplot(TDFgrupoCausas$ni,
main = "Frecuencia de INJURY_SOURCE por Grupo",
xlab = "Grupo",
ylab = "Cantidad (ni)",
col = "steelblue",
names.arg = TDFgrupoCausas$grupo,
las = 2,
cex.names = 0.8)

# GRÁFICO DE BARRAS: FRECUENCIAS ABSOLUTAS POR GRUPO (GLOBAL)
barplot(TDFgrupoCausas$ni,
main = "Gráfica Nº2: Frecuencia de INJURY_SOURCE por Grupo (Global)",
xlab = "Grupo de INJURY_SOURCE",
ylab = "Cantidad (ni)",
col = "darkorange",
names.arg = TDFgrupoCausas$grupo,
cex.names = 0.8,
las = 2,
ylim = c(0, max(TDFgrupoCausas$ni) * 1.2))

# 2. Diagrama de barras hi
barplot(TDFgrupoCausas$hi,
main = "Porcentaje de INJURY_SOURCE por Grupo",
xlab = "Grupo",
ylab = "Porcentaje (%)",
col = "seagreen",
names.arg = TDFgrupoCausas$grupo,
las = 2,
cex.names = 0.8,
ylim = c(0, max(TDFgrupoCausas$hi)*1.2))

# GRÁFICO DE BARRAS: FRECUENCIAS RELATIVAS POR GRUPO (GLOBAL)
barplot(TDFgrupoCausas$hi,
main = "Gráfica Nº4: Porcentaje de INJURY_SOURCE por Grupo (Global)",
xlab = "Grupo de INJURY_SOURCE",
ylab = "Porcentaje (%)",
col = "tomato",
names.arg = TDFgrupoCausas$grupo,
cex.names = 0.9, # Tamaño de etiquetas eje X
las = 2, # Gira los nombres de las categorías
ylim = c(0, 100)) # Eje Y fijo de 0 a 100%

# 3. Gráfico circular
Colores <- colorRampPalette(c("lightskyblue", "darkblue"))
# Etiquetas con porcentaje
etiquetas <- paste(TDFgrupoCausas$hi, "%", sep="")
# Gráfico de pastel
pie(TDFgrupoCausas$hi,
radius = 0.7,
col = Colores(nrow(TDFgrupoCausas)), # Número de colores según la cantidad de grupos
main = "Gráfica Nº5: Porcentaje de INJURY_SOURCE por Grupo",
labels = etiquetas)
# Leyenda
legend(x = 1, y = 0, # Ajusta estos valores según el gráfico
title = "Grupos",
legend = TDFgrupoCausas$grupo,
fill = Colores(nrow(TDFgrupoCausas)),
cex = 0.5,
title.cex = 1)
