library(readxl)
library(plotly)
library(dplyr)
library(ggridges)
library(tidyr)
# Leer el archivo Excel 
admi  <- read_excel("Admision 2025-1.xlsx")
# Tabla con pesos
pesos <- data.frame(
  Area = c("RM","RV","Arit","Alg","Geo","Trig","Bio","Qui","Fis","Eco","His","Geog"),
  Preguntas = c(25,25,5,5,4,4,6,6,6,4,5,5),
  Peso = c(25,25,5,5,4,4,6,6,6,4,5,5)
)


plot_ly(pesos, labels = ~Area, values = ~Peso, type = 'pie',
        textposition = 'auto', 
        marker = list(colors = viridis::viridis(nrow(pesos)))) %>%
  layout(title = "Distribución porcentual de preguntas del examen")
admi$NOTAFINAL <- round((admi$Final*20)/100,2)
admi$RAZ.VERBAL <- round((admi$RV*20)/25,2)
admi$RAZ.MAT <- round((admi$RM*20)/25,2)
admi$ARIT <- round((admi$Arit*20)/5,2)
admi$ALG <- round((admi$Alg*20)/5,2)
admi$GEOM <- round((admi$Geo*20)/4,2)
admi$TRIG <- round((admi$Trig*20)/4,2)
admi$BIO <- round((admi$Bio*20)/6,2)
admi$QUI <- round((admi$Qui*20)/6,2)
admi$FIS <- round((admi$Fis*20)/6,2)
admi$ECO <- round((admi$Eco*20)/4,2)
admi$GEOG <- round((admi$Geog*20)/5,2)
admi$HIST <- round((admi$His*20)/5,2)

Análisis de Postulantes

df_sexo <- admi %>%
  count(SEXO)

plot_ly(
  data = df_sexo,
  x = ~reorder(SEXO, -n),
  y = ~n,
  type = "bar",
  text = ~n,
  textposition = "auto",
  marker = list(color = c("#440154", "#31688E"))
) %>%
  layout(
    title = "Número de postulantes por sexo",
    xaxis = list(title = "Sexo"),
    yaxis = list(title = "Frecuencia"),
    showlegend = FALSE
  )
df_edad_sexo <- admi %>%
  count(EDAD, SEXO) %>%
  arrange(EDAD)

plot_ly(
  data = df_edad_sexo,
  x = ~EDAD,
  y = ~n,
  color = ~SEXO,
  colors = c("#440154", "#31688E"),  
  type = "bar"
) %>%
  layout(
    title = "Distribución de edad de postulantes por sexo",
    xaxis = list(title = "Edad"),
    yaxis = list(title = "Frecuencia"),
    barmode = "group"  
  )
plot_ly(
  data = admi,
  x = ~SEXO,
  y = ~EDAD,
  color = ~SEXO,
  type = "box",
  boxpoints = "outliers",
  colors = c("#440154", "#31688E")  
) %>%
  layout(
    title = "Boxplot de edad de postulantes por sexo",
    xaxis = list(title = "Sexo"),
    yaxis = list(title = "Edad"),
    showlegend = FALSE
  )
modalidad_data <- admi %>%
  count(MODALIDAD) %>%
  mutate(pct = round(n / sum(n) * 100, 1),
         label = MODALIDAD)

plot_ly(modalidad_data,
        labels = ~label,
        values = ~n,
        type = 'pie',
        textposition = 'inside',
        marker = list(colors = viridis::viridis(nrow(modalidad_data))),
        hole = 0.4) %>%
  layout(title = "Modalidad de los postulantes")
opciones_df <- admi %>% select(OPCION.1, OPCION.2) %>% 
  pivot_longer(cols = everything(), names_to = "TipoOpcion", values_to = "Carrera") %>% 
  mutate(TipoOpcion = ifelse(TipoOpcion == "OPCION.1", "Primera opción", "Segunda opción")) %>% 
  count(Carrera, TipoOpcion)


plot_ly( data = opciones_df,
         x = ~n, 
         y = ~reorder(Carrera, n), 
         color = ~TipoOpcion, 
         colors = c("Primera opción" = "#35B779", "Segunda opción" = "#26828E"), type = "bar", 
         orientation = "h" ) %>% 
  layout( title = "Postulantes por carrera según opción de preferencia", 
          xaxis = list(title = "Número de postulantes"), 
          yaxis = list(title = "Carrera"), 
          barmode = "group" )
admi <- admi %>%
  mutate(GrupoIngreso = ifelse(Especialidad == "No Ingreso", "No ingresó", "Ingresó"))

df_ingreso <- admi %>%
  count(GrupoIngreso)

plot_ly(
  data = df_ingreso,
  x = ~GrupoIngreso,
  y = ~n,
  type = "bar",
  text = ~n,
  textposition = "auto",
  marker = list(color = c("#3E4989", "#B4DE2C"))
) %>%
  layout(
    title = "Distribución de postulantes según ingreso",
    xaxis = list(title = "Resultado del examen"),
    yaxis = list(title = "Frecuencia")
  )

Análisis de Ingresantes

admi_ingresantes <- admi %>% 
  filter(Especialidad != "No Ingreso")
plot_ly(data = admi_ingresantes, 
        x = ~NOTAFINAL, 
        type = "histogram",
        nbinsx = 30,
        marker = list(color = "#B4DE2C")) %>%
  layout(title = "Distribución de nota final en ingresantes",
         xaxis = list(title = "Nota Final"),
         yaxis = list(title = "Frecuencia"))
plot_ly(data = admi_ingresantes, 
        y = ~NOTAFINAL, 
        x = ~SEXO,
        color = ~SEXO,
        type = "box",
        boxpoints = "outliers",
        colors = c("#440154", "#31688E")) %>%
  layout(title = "Nota final de ingresantes por sexo",
         yaxis = list(title = "Nota Final"),
         xaxis = list(title = "Sexo"),
         showlegend = FALSE)
library(tidyr)
# Seleccionamos las columnas de interés
cursos <- c("RAZ.VERBAL", "RAZ.MAT", "ARIT", "ALG", "GEOM", "TRIG", "BIO", "QUI", "FIS", "ECO", "GEOG", "HIST")

admi_cursos <- admi_ingresantes %>%
  select(all_of(cursos)) %>%
  pivot_longer(cols = everything(), names_to = "Curso", values_to = "Nota")
plot_ly(data = admi_cursos,
        x = ~Curso,
        y = ~Nota,
        type = "box",
        boxpoints = "outliers",
        color = ~Curso,
        colors = "viridis") %>%
  layout(title = "Distribución de notas por curso – Ingresantes",
         xaxis = list(title = "Curso"),
         yaxis = list(title = "Nota"),
         showlegend = FALSE)
admi_cursos %>%
  group_by(Curso) %>%
  summarise(Media = round(mean(Nota, na.rm = TRUE),2)) %>%
  plot_ly(x = ~reorder(Curso, -Media), 
          y = ~Media,
          type = "bar",
          text = ~Media,
          marker = list(color = ~Media, colorscale = "Viridis")) %>%
  layout(title = "Promedio de nota por curso – Ingresantes",
         xaxis = list(title = "Curso"),
         yaxis = list(title = "Nota promedio"))
admi %>%
  filter(Especialidad != "No Ingreso") %>%
  count(Especialidad) %>%
  arrange(n) %>%
  plot_ly(x = ~n,
          y = ~reorder(Especialidad, n),
          type = "bar",
          orientation = "h",
          text = ~n,
          textposition = "auto",
          marker = list(color = ~n, colorscale = "Viridis")) %>%
  layout(title = "Cantidad de ingresantes por especialidad",
         xaxis = list(title = "Cantidad"),
         yaxis = list(title = "Especialidad"))
admi_opciones <- admi %>%
  filter(Especialidad != "No Ingreso") %>%
  mutate(
    TipoIngreso = case_when(
      Especialidad == `OPCION.1` ~ "Primera Opción",
      Especialidad == `OPCION.2` ~ "Segunda Opción"
    )
  )
plot_ly(data = admi_opciones %>% 
          filter(TipoIngreso != "Otra Carrera") %>%
          count(TipoIngreso),
        x = ~TipoIngreso,
        y = ~n,
        type = "bar",
        text = ~n,
        textposition = "auto",
        marker = list(color = c("#35B779", "#26828E"))) %>%
  layout(title = "Ingresantes según opción de carrera",
         xaxis = list(title = "Tipo de opción asignada"),
         yaxis = list(title = "Número de ingresantes"))
admi_opciones %>%
  filter(TipoIngreso != "Otra Carrera") %>%
  count(Especialidad, TipoIngreso) %>%
  arrange(desc(n)) %>%
  plot_ly(x = ~n,
          y = ~reorder(Especialidad, n),
          color = ~TipoIngreso,
          colors = c("Primera Opción" = "#35B779", "Segunda Opción" = "#26828E"),
          type = "bar",
          orientation = "h",
          textposition = "auto") %>%
  layout(title = "Ingresantes por especialidad según tipo de opción",
         xaxis = list(title = "Cantidad"),
         yaxis = list(title = "Especialidad"),
         barmode = "stack")
tasa_ingreso <- admi %>%
  mutate(OPCION1_CLEAN = toupper(trimws(OPCION.1)),
         ESPECIALIDAD_CLEAN = toupper(trimws(Especialidad))) %>%
  group_by(OPCION.1) %>%  
  summarise(Postulantes = n(),
            Ingresantes = sum(ESPECIALIDAD_CLEAN == toupper(trimws(OPCION.1)), na.rm = TRUE)) %>%
  mutate(TasaIngreso = round((Ingresantes / Postulantes) * 100, 1))
plot_ly(tasa_ingreso,
        x = ~TasaIngreso,
        y = ~reorder(OPCION.1, TasaIngreso),
        type = "bar",
        orientation = "h",
        marker = list(color = ~TasaIngreso, colorscale = "Viridis"),
        text = ~paste0(TasaIngreso, "%"),
        textposition = "auto") %>%
  layout(title = "Tasa de ingreso por especialidad",
         xaxis = list(title = "Tasa de ingreso (%)"),
         yaxis = list(title = "Especialidad"))