# Instalar y cargar ggplot2 si no lo tienes instalado
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}

library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
# Crear el dataframe
datos_eind <- data.frame(
  Subgrupo = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
               11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
               21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
               31, 32, 33, 34, 35, 36, 37, 38, 39, 40),
  Paquetes_ni = c(595, 593, 607, 596, 602, 599, 600, 590, 599, 601,
                  598, 600, 597, 594, 595, 597, 599, 596, 607, 601,
                  594, 606, 601, 598, 599, 590, 588, 597, 604, 605,
                  597, 603, 596, 597, 607, 596, 598, 600, 608, 592),
  Paquetes_aire_di = c(15, 5, 8, 10, 6, 5, 5, 7, 2, 4,
                       9, 17, 4, 5, 3, 10, 7, 5, 4, 9,
                       7, 5, 7, 4, 2, 3, 5, 3, 6, 5,
                       7, 9, 5, 3, 8, 15, 4, 6, 8, 5),
  Proporción_pi = c(0.025, 0.008, 0.013, 0.017, 0.010, 0.008, 0.008, 0.012, 0.003, 0.007,
                    0.015, 0.028, 0.007, 0.008, 0.005, 0.017, 0.012, 0.008, 0.007, 0.015,
                    0.012, 0.008, 0.012, 0.007, 0.003, 0.005, 0.009, 0.005, 0.010, 0.008,
                    0.012, 0.015, 0.008, 0.005, 0.013, 0.025, 0.007, 0.010, 0.013, 0.008),
  LimCsup = rep(0.023, 40),
  LimC = rep(0.011, 40),
  LimCinf = rep(-0.002, 40)
)

# Graficar el dataframe
ggplot(datos_eind, aes(x = Subgrupo)) +
  geom_line(aes(y = Proporción_pi, color = "Proporción_pi")) +
  geom_line(aes(y = LimCsup, color = "LimCsup")) +
  geom_line(aes(y = LimC, color = "LimC")) +
  geom_line(aes(y = LimCinf, color = "LimCinf")) +
  labs(title = "Proporción de Paquetes con Límites de Control",
       x = "Subgrupo",
       y = "Proporción / Límites de Control",
       color = "Leyenda") +
  theme_minimal()

# Graficar usando base R
plot(datos_eind$Subgrupo, datos_eind$Proporción_pi, type = "l", col = "blue", ylim = c(-0.005, 0.03),
     main = "Proporción de Paquetes con Límites de Control", xlab = "Subgrupo", ylab = "Proporción / Límites de Control")
lines(datos_eind$Subgrupo, datos_eind$LimCsup, col = "red")
lines(datos_eind$Subgrupo, datos_eind$LimC, col = "green")
lines(datos_eind$Subgrupo, datos_eind$LimCinf, col = "purple")
legend("topright", legend = c("Proporción_pi", "LimCsup", "LimC", "LimCinf"),
       col = c("blue", "red", "green", "purple"), lty = 1)

# Instalar y cargar lattice si no está instalado
if (!requireNamespace("lattice", quietly = TRUE)) {
  install.packages("lattice")
}

library(lattice)
## Warning: package 'lattice' was built under R version 4.3.3
# Graficar usando lattice
xyplot(Proporción_pi + LimCsup + LimC + LimCinf ~ Subgrupo, data = datos_eind, type = "l", 
       auto.key = list(space = "top", columns = 4, lines = TRUE, points = FALSE),
       main = "Proporción de Paquetes con Límites de Control", xlab = "Subgrupo", ylab = "Proporción / Límites de Control")

# Instalar y cargar plotly si no está instalado
if (!requireNamespace("plotly", quietly = TRUE)) {
  install.packages("plotly")
}

library(plotly)
## Warning: package 'plotly' was built under R version 4.3.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# Graficar usando plotly
fig <- plot_ly(datos_eind, x = ~Subgrupo) %>%
  add_lines(y = ~Proporción_pi, name = 'Proporción_pi', line = list(color = 'blue')) %>%
  add_lines(y = ~LimCsup, name = 'LimCsup', line = list(color = 'red')) %>%
  add_lines(y = ~LimC, name = 'LimC', line = list(color = 'green')) %>%
  add_lines(y = ~LimCinf, name = 'LimCinf', line = list(color = 'purple')) %>%
  layout(title = "Proporción de Paquetes con Límites de Control",
         xaxis = list(title = "Subgrupo"),
         yaxis = list(title = "Proporción / Límites de Control"),
         legend = list(title = list(text = "Leyenda")))

fig