library(ggplot2)
png('/results/figN.png')
# Custom color sequence in hex format
custom_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728")
#custom_colors <- c("#f0bba3", "#99fe99", "#9999fd", "#faadbd")

# other colors
# purple: #9999fd
# red salmon: #faadbd
# green highlight: #99fe99
# beige: #f0bba3

# 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 * 4),  # Each model has 5 values for each of the 4 scenarios
    Scenarios = rep(rep(c("N = 40", "N = 80", "N = 100", "N = 120"), each = 5), times = 4),  # 4 models and 4 scenarios
    Value = c(
        # Numerical examples for each model and scenario
        # Model1
        c(4291.01,
          3754.24,
          4449.93,
          3131.53,
          3392.01,
          10341,
          7948.98,
          7815.13,
          6879.92,
          6986.28,
          9571.51,
          10244.17,
          9574.25,
          8878.99,
          7512.67,
          14103.66,
          11212.06,
          11160.15,
          10424.92,
          9451.92),
        # Model2
        c(3921.93,
          3505.46,
          4121.87,
          2965.58,
          3522.83,
          6680.84,
          8017.31,
          6070.56,
          6441.55,
          5660.75,
          3313.35,
          4517.39,
          5383.4,
          8208.46,
          7905.46,
          8871.93,
          11598.28,
          9458.63,
          9031.13,
          8215.49),
        
        # Model3
        c(244.73,
          939.03,
          842.4,
          1035.13,
          263.05,
          1593.86,
          1697.36,
          710.72,
          627.42,
          566.86,
          1229.863554,
          2160.83,
          1022.23,
          886.32,
          1457.28,
          1367.23,
          2298.95,
          1316.7,
          1120.46,
          1774.14),
        
        # Model4
        c(151.61,
          832.25,
          675.62,
          787.12,
          102.8,
          1134.21,
          1251.45,
          242.82,
          351.83,
          217.07,
          1082.54,
          1812.32,
          540.18,
          636.27,
          736.59,
          641.27,
          2001.24,
          828.27,
          989.14,
          1131.68
        )
    )
)

# 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("N = 40", "N = 80", "N = 100", "N = 120"))

# 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)) +
    # stat_summary(fun=mean, geom="point", shape=20, size=4, color="red", fill=data$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), expand = c(0, 0))  # Remove y-axis expansion
# dev.off()
box
#ggsave("testtestN.png", box)
dev.off()
