startAggregates <- fullSet %>%
#filter(P1Card %in% c(2,3,4,5,7) & P2Card %in% c(2,3,4,5,7)) %>%
group_by(P1Card, P2Card, Outcome) %>%
summarize(n = n()) %>%
pivot_wider(id_cols = c(P1Card, P2Card), names_from = Outcome, values_from = n, values_fill = 0) %>%
mutate(n = Win + Loss,
conf_low = binom.test(Win + 2, Win + Loss + 4, conf.level = conf_level)$conf.int[1], # ADJUSTED WALD CI
conf_high = binom.test(Win + 2, Win + Loss + 4, conf.level = conf_level)$conf.int[2],
phat = (Win + 2) / (Win + Loss + 4)
) %>%
ungroup() %>%
full_join(optimalWinRates, by = c("P1Card", "P2Card")) %>%
mutate(P2Card=factor(P2Card, levels=c("7", "6", "5", "4","3","2", "1", "0")))
plotData <- startAggregates %>%
filter(P1Card %in% c(2,3,4,5) & P2Card %in% c(2,3,4,5)) %>%
#filter(n >= 4) %>%
pivot_longer(
cols = c(phat, OptimalWinP),
names_to = "Type",
values_to = "WinRate"
) %>%
mutate(
Type = recode(
Type,
phat = "Observed",
OptimalWinP = "Optimal"
),
label = paste0(
cardLabelP1, " vs. ",
cardLabelP2
),
label_n = paste0("n = ", n, ""),
P2Card = factor(P2Card, levels = c("7","6","5","4","3", "2", "1", "0")) # reverse ordering of P2's cards
)
ggplot(plotData,
aes(x = Type, y = WinRate, fill = Type)) +
# Main Bars
geom_col(width = 0.65, color = "black", linewidth = 0.25) +
# CI only for observed bars
geom_errorbar(
data = plotData %>% filter(Type == "Observed"),
aes(x = 0.9, ymin = conf_low, ymax = conf_high),
width = 0.2, size = 0.7
) +
# CI upper label
geom_text(
data = plotData %>% filter(Type == "Observed"),
aes(x = 0.9, y = conf_high,
label = scales::percent(conf_high, accuracy = 1)
),
hjust = 0.55, vjust = -0.3, size = 2.7
) +
# CI lower label
geom_text(
data = plotData %>% filter(Type == "Observed"),
aes(x = 0.9, y = conf_low,
label = scales::percent(conf_low, accuracy = 1)
),
hjust = 0.55,
vjust = 1.25,
size = 2.7
) +
# labels on bars
geom_text(
aes(label = scales::percent(WinRate, accuracy = 1)),
vjust = -0.45,
hjust = -0.2,
size = 3
) +
facet_grid(
P2Card ~ P1Card,
switch = "both"
) +
#Top of subplot text
geom_text(
aes(
x = 1.5, y = 1.09,
label = label
),
inherit.aes = FALSE,
size = 2.5,
fontface = "bold"
) +
# n text
geom_text(
aes(
x = 1.5, y = 0.95,
label = label_n
),
inherit.aes = FALSE,
size = 2.5
) +
scale_y_continuous(expand = expansion(mult = c(0, 0))) +
coord_cartesian(ylim = c(0, 1.18), clip = "off") +
labs(title = "Win Rates for Most Common First Turn Outcomes",
subtitle = "(Adjusted Wald estimates w/ 90% confidence intervals)",
fill = NULL) +
theme_classic(base_size = 11) +
theme(
# remove facet grid labels
strip.text = element_blank(),
strip.background = element_blank(),
# remove axis titles
axis.title.x = element_blank(),
axis.title.y = element_blank(),
# remove axis text (optional for journal figures)
axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
# remove axis text (optional for journal figures)
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position = "top",
legend.direction = "horizontal",
# add box around each facet
panel.border = element_rect(
color = "black",
fill = NA,
linewidth = 0.6
),
panel.spacing = unit(0.6, "lines"),
plot.title = element_text(face = "bold", size = 13, hjust =0.5),
plot.subtitle = element_text(hjust = 0.5, size = 7)
) +
# Color palette (clean + print safe)
scale_fill_manual(
values = c(
"Observed" = "#6BAED6",
"Optimal" = "#D95F0E"
),
labels = c(
"Observed" = "Observed Win Rate",
"Optimal" = "Theoretical MiniMax Win Rate"
),
)