library(ggplot2)
png('/results/figR.png')
# Custom color sequence in hex format
custom_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728")

# Sample data with different scenarios and values (replace this with your actual data)
data <- data.frame(
    Model = rep(c("LinUCB","GOB.Lin","CL-UCB \n (w/o sim. reg.)","CL-UCB"), each = 5 * 2),  # Each model has 5 values for each of the 2 scenarios
    Scenarios = rep(rep(c("K = 3", "K = 5"), each = 5), times = 2),  # 4 models and 2 scenarios
    Value = c(
        # Numerical examples for each model and scenario
        # Model1
        c(9571.51,
          10244.17,
          9574.25,
          8878.99,
          7512.67,
          28973.67,
          25088.06,
          30373.75,
          29478.51,
          28361.61
        ),
        # Model2
        c(3313.35,
          4517.39,
          5383.4,
          8208.46,
          7905.46,
          26894.52,
          22708.92,
          29120.42,
          31604.54,
          26571.69
        ),
        
        # Model3
        c(1229.863554,
          2160.83,
          1022.23,
          886.32,
          1457.28,
          10201.3829,
          14732.34931,
          14615.468,
          20178.93,
          11432.49946
        ),
        
        # Model4
        c(1082.54,
          1812.32,
          540.18,
          636.27,
          736.59,
          10407.1378,
          6428.717554,
          16626.74151,
          14379.025,
          11014.87
        )
    )
)

# Set factor levels for Model and Scenario
data$Model <- factor(data$Model, levels = c("LinUCB","GOB.Lin","CL-UCB \n (w/o sim. reg.)","CL-UCB"))
data$Scenarios <- factor(data$Scenarios, levels = c("K = 3", "K = 5"))

# Arrange the data in the desired order
data <- data[order(data$Model, data$Scenarios), ]
# tiff("testN.tiff", width = 1920, height = 1017, res = 800)
# Create box plots with custom colors and correct order
box <- ggplot(data, aes(x = Model, y = Value, fill = Scenarios)) +
    geom_boxplot() +  # Add a gray solid border
    labs(
        x = "Models",
        y = "Cumulative regret") +
    theme_minimal() +
    scale_fill_manual(values = custom_colors) +  # Use custom colors
    theme(
        legend.position = "inside", legend.position.inside = c(0.87, 0.8),  # Move legend to the top-right
        legend.box.background = element_rect(color = "black", linewidth = 0.5),
        text = element_text(size = 10, face = "bold", color = "black")# Add a border to the legend box
    ) +  # Add gray solid border
    geom_vline(xintercept = c(0.0, 1.5, 2.5, 3.5), linetype = "solid", color = "black") +  # Change line type to solid
    geom_hline(yintercept = 0, linetype = "solid", color = "black") +  # Add y-axis line
    coord_cartesian(ylim = c(0, max(data$Value) + 1000)) + #+
    # scale_x_discrete(expand = c(0.2, 0)) +  # Remove x-axis expansion
    scale_y_continuous(breaks = c(0,2500,5000,7500,10000, 12500, 15000, 17500, 20000, 22500, 25000, 27500, 30000, 32500), expand = c(0, 0))  # Remove y-axis expansion
box
#ggsave("testtestR.png", box)
dev.off()

