# Load necessary libraries
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
library(tidyr)
library(ggplot2)

# Data for large universities
large_universities_data <- data.frame(
  University = c("Calabria", "Pavia", "Perugia", "Parma", "Cagliari", "Salerno", 
                 "Milano Bicocca", "Roma Tor Vergata", "Modena e Reggio Emilia", 
                 "Genova", "Verona", "Messina", "Ferrara", "Roma Tre", 
                 "Campania Vanvitelli", "Bari", "Chieti e Pescara", "Catania"),
  Services = c(110, 78, 76, 67, 80, 74, 71, 73, 72, 73, 68, 72, 69, 69, 71, 77, 72, 71),
  Scholarships = c(110, 86, 84, 80, 109, 93, 86, 80, 72, 66, 69, 86, 76, 69, 87, 86, 69, 75),
  Structures = c(86, 95, 88, 100, 86, 94, 85, 89, 89, 88, 85, 79, 83, 92, 83, 83, 93, 82),
  Communication = c(94, 95, 102, 102, 91, 105, 99, 87, 91, 91, 95, 106, 82, 90, 90, 78, 90, 91),
  Internationalization = c(78, 91, 91, 84, 76, 70, 77, 84, 76, 83, 79, 74, 76, 78, 73, 66, 71, 69),
  Employability = c(75, 92, 85, 90, 77, 79, 96, 94, 101, 93, 96, 67, 96, 82, 71, 72, 66, 72)
)

# Data for medium universities
medium_universities_data <- data.frame(
  University = c("Trento", "Udine", "Sassari", "Marche", "Siena", "Venezia Cà Foscari", "Trieste", 
                 "Brescia", "Urbino Carlo Bo", "Salento", "Bergamo", "Piemonte Orientale", 
                 "Napoli Parthenope", "Insubria", "L'Aquila", "Foggia", "Catanzaro"),
  Services = c(77, 84, 81, 77, 91, 77, 84, 81, 86, 93, 75, 68, 79, 81, 70, 72, 73),
  Scholarships = c(86, 90, 105, 84, 83, 76, 83, 70, 80, 96, 66, 68, 90, 66, 74, 87, 108),
  Structures = c(102, 96, 110, 106, 103, 80, 90, 86, 85, 92, 91, 102, 89, 75, 77, 83, 82),
  Communication = c(99, 110, 90, 97, 90, 106, 99, 107, 101, 90, 91, 99, 83, 93, 97, 82, 83),
  Internationalization = c(110, 80, 88, 79, 89, 105, 81, 76, 72, 70, 85, 76, 77, 86, 80, 87, 67),
  Employability = c(93, 99, 76, 103, 87, 89, 95, 107, 85, 67, 95, 88, 83, 98, 93, 77, 67)
)

# Create DataFrames
df_large_universities <- large_universities_data
df_medium_universities <- medium_universities_data

# Original weights (equal weight for all indicators)
original_weights <- c(
  Services = 1/6,
  Scholarships = 1/6,
  Structures = 1/6,
  Communication = 1/6,
  Internationalization = 1/6,
  Employability = 1/6
)

# Adjusted weights for 40% employability emphasis
adjusted_weights_employability_40 <- c(
  Services = 0.12,
  Scholarships = 0.12,
  Structures = 0.12,
  Communication = 0.12,
  Internationalization = 0.12,
  Employability = 0.4
)

# Function to calculate the weighted score
calculate_weighted_score <- function(row, weights) {
  weighted_score <- sum(row * weights)
  return(weighted_score)
}

# Calculate the original and adjusted weighted scores for large universities
df_large_universities <- df_large_universities %>%
  rowwise() %>%
  mutate(
    Original_Score = calculate_weighted_score(c_across(Services:Employability), original_weights),
    Adjusted_Score_40 = calculate_weighted_score(c_across(Services:Employability), adjusted_weights_employability_40)
  ) %>%
  ungroup()

# Calculate the original and adjusted weighted scores for medium universities
df_medium_universities <- df_medium_universities %>%
  rowwise() %>%
  mutate(
    Original_Score = calculate_weighted_score(c_across(Services:Employability), original_weights),
    Adjusted_Score_40 = calculate_weighted_score(c_across(Services:Employability), adjusted_weights_employability_40)
  ) %>%
  ungroup()

# Rank the universities based on original and adjusted scores
df_large_universities <- df_large_universities %>%
  arrange(desc(Original_Score)) %>%
  mutate(Original_Rank = row_number()) %>%
  arrange(desc(Adjusted_Score_40)) %>%
  mutate(Adjusted_Rank = row_number())

df_medium_universities <- df_medium_universities %>%
  arrange(desc(Original_Score)) %>%
  mutate(Original_Rank = row_number()) %>%
  arrange(desc(Adjusted_Score_40)) %>%
  mutate(Adjusted_Rank = row_number())

# Function to create the comparison chart for ranks
create_comparison_chart <- function(df, title) {
  df_combined <- df %>%
    select(University, Original_Rank, Adjusted_Rank)
  
  ggplot(df_combined) +
    geom_point(aes(x = Original_Rank, y = University, color = 'Original Rank'), size = 3) +
    geom_point(aes(x = Adjusted_Rank, y = University, color = 'Adjusted Rank'), size = 3) +
    geom_segment(aes(x = Original_Rank, xend = Adjusted_Rank, y = University, yend = University), color = 'grey') +
    scale_color_manual(values = c('blue', 'red')) +
    labs(title = title, x = 'Rank (lower is better)', y = 'University') +
    theme_minimal() +
    theme(axis.text.y = element_text(size = 8)) +
    scale_x_reverse()
}

# Create visualizations for large universities
create_comparison_chart(df_large_universities, "Large Universities: Original vs Adjusted Rankings")

# Create visualizations for medium universities
create_comparison_chart(df_medium_universities, "Medium Universities: Original vs Adjusted Rankings")

# Function to create score comparison chart
create_score_comparison_chart <- function(df, title) {
  df_long <- df %>%
    pivot_longer(cols = c(Original_Score, Adjusted_Score_40), names_to = "Score_Type", values_to = "Score")
  
  ggplot(df_long, aes(x = University, y = Score, fill = Score_Type)) +
    geom_bar(stat = "identity", position = "dodge") +
    labs(title = title, x = "Universities", y = "Score") +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 90, hjust = 1))
}

# Create score comparison visualizations for large universities
create_score_comparison_chart(df_large_universities, "Large Universities: Original vs Adjusted Scores")

# Create score comparison visualizations for medium universities
create_score_comparison_chart(df_medium_universities, "Medium Universities: Original vs Adjusted Scores")