Report Number

library(knitr)
library(kableExtra)
library(dplyr)
## 
## Adjuntando el paquete: 'dplyr'
## The following object is masked from 'package:kableExtra':
## 
##     group_rows
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#Tabla de frecuencia 
TDF_causas_raw <- as.data.frame(table(datos$Cause.Category))
colnames(TDF_causas_raw) <- c("Causa", "ni")
TDF_ordenada <- TDF_causas_raw %>%
  arrange(desc(ni)) 
# --- PASO 3: Preparar la Tabla Final 
# 3.1 Calcular Porcentajes
ni_total <- sum(TDF_ordenada$ni)
hi_num   <- (TDF_ordenada$ni / ni_total) * 100
TDF_ordenada$hi <- sprintf("%.2f", round(hi_num, 2))
TDF_limpia <- TDF_ordenada %>% 
  select(Causa, ni, hi)
Sumatoria <- data.frame(
  Causa = "TOTAL", 
  ni = ni_total, 
  hi = "100.00"
)
TDF_final <- rbind(TDF_limpia, Sumatoria)
colnames(TDF_final) <- c("Causa del Accidente", "ni", "hi (%)")
kable(TDF_final, align = 'c', 
      caption = "Gráfico 1 Frecuencia de Causas de Accidentes") %>%
  kable_styling(full_width = FALSE, position = "center", 
                bootstrap_options = c("striped", "hover", "condensed")) %>%
  row_spec(nrow(TDF_final), bold = TRUE, background = "#f2f2f2")
Gráfico 1 Frecuencia de Causas de Accidentes
Causa del Accidente ni hi (%)
MATERIAL/WELD/EQUIP FAILURE 1435 51.34
CORROSION 592 21.18
INCORRECT OPERATION 378 13.52
ALL OTHER CAUSES 118 4.22
NATURAL FORCE DAMAGE 118 4.22
EXCAVATION DAMAGE 97 3.47
OTHER OUTSIDE FORCE DAMAGE 57 2.04
TOTAL 2795 100.00
library(ggplot2)
library(dplyr)

datos_grafico <- TDF_limpia %>%
  mutate(
    ni = as.numeric(ni),
    Etiqueta_Leyenda = paste0(Causa, " (", hi, "%)"),
    Etiqueta_Leyenda = factor(Etiqueta_Leyenda, levels = unique(Etiqueta_Leyenda)) 
  )
ggplot(datos_grafico, aes(x = "", y = ni, fill = Etiqueta_Leyenda)) +  
  geom_bar(stat = "identity", width = 1, color = "white", alpha = 0.6) +
  
  coord_polar("y", start = 0) +
  theme_void() + 
  theme(
    legend.position = "right",             
    legend.text = element_text(size = 7),  
    legend.title = element_text(size = 9, face = "bold") 
  ) +
  
  labs(title = "Gráfico 2 Distribución de Report Numbers por Causa",
       fill = "Leyenda") + 
  scale_fill_brewer(palette = "Blues", direction = -1)

library(ggplot2)
library(dplyr)
#Gráfico de barras
datos_barras <- TDF_limpia %>%
  filter(Causa != "TOTAL") %>%
  mutate(
    ni = as.numeric(ni),
    Causa = reorder(Causa, ni) 
  )
ggplot(datos_barras, aes(x = Causa, y = ni)) +
  geom_bar(stat = "identity", fill = "blue", width = 0.7) +
  
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  labs(title = "Gráfica 3 Cantidad de Causas de Accidentes",
       subtitle = "Número de reportes",
       x = "Causa",
       y = "Número de Accidentes") +
  scale_y_continuous(expand = expansion(mult = c(0, 0.05)))

library(ggplot2)
library(dplyr)
#Gráfico barras
datos_barras <- TDF_limpia %>%
  filter(Causa != "TOTAL") %>%
  mutate(
    ni = as.numeric(ni),
    Causa = reorder(Causa, ni) 
  )
ggplot(datos_barras, aes(x = Causa, y = ni)) +
  geom_bar(stat = "identity", fill = "blue", width = 0.7) +
  
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  
  labs(title = "Gráfica 4 Causas generales de Accidentes",
       subtitle = "Número de reportes",
       x = "Causa",
       y = "Número de Accidentes") +
  scale_y_continuous(
    limits = c(0, 4000),  
    expand = expansion(mult = c(0, 0.05))
  )