###### Universidad Central del Ecuador######
###### Carrera de Petróleos #####
library(readxl)
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(ggplot2)
library(readr)
# Cargar datos
datasetf <- read_csv("datasetf.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
## dat <- vroom(...)
## problems(dat)
## Rows: 2795 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (18): Accident Date/Time, Operator Name, Pipeline/Facility Name, Pipelin...
## dbl (18): Report Number, Supplemental Number, Accident Year, Operator ID, Ac...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Agrupar por Pipeline Type y asignar nombre a los NA
Tabla_type <- datasetf %>%
count(Pipeline_Type = `Pipeline Type`, name = "ni") %>%
# Asignar nombre "NO ESPECIFICADO" a los NA
mutate(Pipeline_Type = ifelse(is.na(Pipeline_Type), "NO ESPECIFICADO", Pipeline_Type)) %>%
arrange(desc(ni))
# TDFfrecuencias
Tabla_type$hi <- Tabla_type$ni / sum(Tabla_type$ni)
Tabla_type$hi_porcentaje <- round(Tabla_type$hi * 100, 3)
# Agregar TOTAL
total_fila_type <- data.frame(
Pipeline_Type = "TOTAL",
ni = sum(Tabla_type$ni),
hi = 1,
hi_porcentaje = 100.000
)
Tabla_completa_type <- rbind(Tabla_type, total_fila_type)
Tabla_final_type <- Tabla_completa_type[, c("Pipeline_Type", "ni", "hi_porcentaje")]
names(Tabla_final_type)[names(Tabla_final_type) == "hi_porcentaje"] <- "hi"#CAMBIO DE NOMBRE
View(Tabla_final_type)
Tabla_final_type
## # A tibble: 6 × 3
## Pipeline_Type ni hi
## <chr> <int> <dbl>
## 1 ABOVEGROUND 1475 52.8
## 2 UNDERGROUND 985 35.2
## 3 TANK 301 10.8
## 4 NO ESPECIFICADO 18 0.644
## 5 TRANSITION AREA 16 0.572
## 6 TOTAL 2795 100
# Filtrar datos (sin TOTAL)
datos_grafica_type <- Tabla_final_type[Tabla_final_type$Pipeline_Type != "TOTAL", ]
# Gráfico 1: Barras para ni
par(mar = c(7, 4, 4, 2) + 0.1)
barplot(datos_grafica_type$ni,
names.arg = datos_grafica_type$Pipeline_Type,
col = "#4ECDC4",
main = "Gráfica No.1: Distribución por Tipo de Pipeline",
ylab = "Cantidad",
las = 2,
cex.names = 0.7,
ylim = c(0, max(datos_grafica_type$ni) * 1.1))

# Gráfico 2: Barras para hi
barplot(datos_grafica_type$hi,
names.arg = datos_grafica_type$Pipeline_Type,
col = "#4ECDC4",
main = "Gráfica No.2: Distribución por Tipo de Pipeline",
ylab = "Cantidad",
las = 2,
cex.names = 0.7,
ylim = c(0, max(datos_grafica_type$hi) * 1.1))

# Gráfico 3: Porcentaje (hi * 100)
barplot(datos_grafica_type$hi * 100,
names.arg = datos_grafica_type$Pipeline_Type,
col = "#4ECDC4",
main = "Gráfica Nº3: Distribución por Tipo de Pipeline",
las = 2,
cex.names = 0.7,
ylab = "Cantidad")

# Gráfico 4: Pastel
par(mar = c(2, 2, 2, 14))
azules_degradado <- c("#1f77b4", "#279ece", "#4fb4d8", "#7bcde8", "#a6e3f7", "#d4f1f9")
pie(datos_grafica_type$hi * 100,
labels = NA,
col = azules_degradado,
main = "Gráfica Nº4:
Distribución por Tipo de LÃquido",
radius = 1)
# Leyenda
legend(x = 1.2, y = 1,
legend = paste0(datos_grafica_type$Pipeline_Type, " - ",
round(datos_grafica_type$hi * 100, 1), "%",
" (", datos_grafica_type$ni, " casos)"),
fill = azules_degradado,
bty = "t",
cex = 0.7,
xpd = TRUE)
