library(ggplot2)
library(tidyr)

# Your palette
palette <- c(
  valence = "#C2E3F1",
  arousal = "#FFA5A3",
  boxes = "#F7F1E4",
  loss = "#FFE09A",
  borders = "#4F6A72"
)

# Data from Exp B26 (seed 42)
df <- data.frame(
  Language = factor(c('English', 'German', 'Pidgin', 'Chinese'),
                    levels = c('English', 'German', 'Pidgin', 'Chinese')),
  sigma_V = c(0.91, 0.52, 0.91, 0.39),
  sigma_A = c(0.42, 0.79, 0.42, 0.47)
)

df_long <- pivot_longer(df, cols = c(sigma_V, sigma_A),
                         names_to = "Task", values_to = "sigma")

ggplot(df_long, aes(x = Language, y = sigma, fill = Task)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8),
           width = 0.65, color = palette["borders"], linewidth = 0.8) +
  geom_text(aes(label = sprintf("%.2f", sigma)),
            position = position_dodge(width = 0.8),
            vjust = -0.5, size = 3.5, color = palette["borders"]) +
  scale_fill_manual(
    values = c("sigma_V" = palette["valence"], "sigma_A" = palette["arousal"]),
    labels = c(expression(sigma[V]~"(Valence)"), expression(sigma[A]~"(Arousal)"))
  ) +
  labs(y = expression("Learned uncertainty ("*sigma*")"), x = NULL, fill = NULL) +
  theme_minimal(base_size = 12, base_family = "serif") +
  theme(
    panel.grid.major.x = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "top",
    axis.line = element_line(color = palette["borders"], linewidth = 0.5),
    axis.text = element_text(color = palette["borders"]),
    axis.title = element_text(color = palette["borders"]),
    legend.text = element_text(color = palette["borders"])
  ) +
  ylim(0, 1.15)

ggsave("sigma_comparison.pdf", width = 5, height = 3.5)
ggsave("sigma_comparison.png", width = 5, height = 3.5, dpi = 300)

cat("Saved: sigma_comparison.pdf and sigma_comparison.png\n")
