{"spec_id":"line-win-probability","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' line-win-probability: Win Probability Chart\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 90/100 | Created: 2026-06-21\n\nlibrary(ggplot2)\nlibrary(scales)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens\nTHEME       <- Sys.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\nELEVATED_BG <- if (THEME == \"light\") \"#FFFDF6\" else \"#242420\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\n# Imprint categorical palette — first series always #009E73\nIMPRINT_PALETTE <- c(\n  \"#009E73\",  # 1 — brand green (Hawks / home team winning zone)\n  \"#C475FD\",  # 2\n  \"#4467A3\",  # 3\n  \"#BD8233\",  # 4\n  \"#AE3030\",  # 5 — matte red (Wolves / away team winning zone)\n  \"#2ABCCD\",  # 6\n  \"#954477\",  # 7\n  \"#99B314\"   # 8\n)\n\nHOME_COLOR <- IMPRINT_PALETTE[1]  # #009E73: Hawks (home)\nAWAY_COLOR <- IMPRINT_PALETTE[5]  # #AE3030: Wolves (away) — semantic red for loss zone\n\n# Win probability via spline control points encoding the game narrative:\n# Hawks start slightly ahead, Wolves mount a 10-0 run in Q2 to take the lead,\n# Hawks storm back in Q3, then seal it with back-to-back 3-pointers in Q4.\nctrl <- data.frame(\n  t = c(0,    5,    10,   15,   19,   22,   26,   30,   34,   38,   42,   45,   48),\n  p = c(0.50, 0.54, 0.58, 0.52, 0.36, 0.40, 0.52, 0.66, 0.60, 0.68, 0.61, 0.80, 0.87)\n)\n\nn        <- 250\nminute   <- seq(0, 48, length.out = n)\nwp_fn    <- splinefun(ctrl$t, ctrl$p, method = \"natural\")\nwin_prob <- pmin(pmax(wp_fn(minute) + rnorm(n, 0, 0.012), 0.03), 0.97)\ndf       <- data.frame(minute = minute, win_prob = win_prob)\n\n# Key annotated events\nevent_times <- c(19, 29, 44)\nevents <- data.frame(\n  minute    = event_times,\n  win_prob  = approx(df$minute, df$win_prob, xout = event_times)$y,\n  label     = c(\"Wolves 10-0 run\", \"Hawks 8-2 run\\nto regain lead\", \"Back-to-back\\n3-pointers\"),\n  vjust_lbl = c(1.8, -0.5, -0.5)\n)\n\n# Quarter dividers\nqt <- data.frame(minute = c(12, 24, 36), label = c(\"Q2\", \"Q3\", \"Q4\"))\n\nplot_title <- \"line-win-probability · r · ggplot2 · anyplot.ai\"\n\np <- ggplot(df, aes(x = minute, y = win_prob)) +\n  # Home winning zone: fill between 50% and line when Hawks are ahead\n  geom_ribbon(\n    aes(ymin = 0.5, ymax = pmax(win_prob, 0.5)),\n    fill = HOME_COLOR, alpha = 0.18\n  ) +\n  # Away winning zone: fill between line and 50% when Wolves are ahead\n  geom_ribbon(\n    aes(ymin = pmin(win_prob, 0.5), ymax = 0.5),\n    fill = AWAY_COLOR, alpha = 0.18\n  ) +\n  # Quarter dividers\n  geom_vline(\n    data = qt, aes(xintercept = minute),\n    color = INK_MUTED, linewidth = 0.35, linetype = \"dotted\"\n  ) +\n  # 50% reference line\n  geom_hline(\n    yintercept = 0.5,\n    color = INK_SOFT, linewidth = 0.55, linetype = \"dashed\"\n  ) +\n  # Win probability line\n  geom_line(color = HOME_COLOR, linewidth = 1.0) +\n  # Key event markers\n  geom_point(\n    data = events, aes(x = minute, y = win_prob),\n    shape = 21, size = 3.0,\n    fill = ELEVATED_BG, color = INK, stroke = 0.8\n  ) +\n  # Key event labels\n  geom_text(\n    data = events,\n    aes(x = minute, y = win_prob, label = label, vjust = vjust_lbl),\n    color = INK_SOFT, size = 2.4, lineheight = 0.9\n  ) +\n  # Quarter labels at top of panel\n  annotate(\n    \"text\",\n    x = c(0.4, qt$minute), y = 0.963,\n    label = c(\"Q1\", qt$label),\n    color = INK_MUTED, size = 2.6, hjust = c(0, 0.5, 0.5, 0.5)\n  ) +\n  # Zone labels\n  annotate(\"text\", x = 1.5, y = 0.88,\n    label = \"Hawks winning\", color = HOME_COLOR,\n    size = 2.8, hjust = 0, fontface = \"bold\"\n  ) +\n  annotate(\"text\", x = 1.5, y = 0.12,\n    label = \"Wolves winning\", color = AWAY_COLOR,\n    size = 2.8, hjust = 0, fontface = \"bold\"\n  ) +\n  # Final score\n  annotate(\"text\", x = 47.2, y = 0.963,\n    label = \"Final: Hawks 108 – Wolves 101\",\n    color = INK_SOFT, size = 2.6, hjust = 1\n  ) +\n  scale_y_continuous(\n    labels = scales::percent_format(accuracy = 1),\n    limits = c(0, 1),\n    breaks = c(0, 0.25, 0.5, 0.75, 1.0),\n    expand = c(0, 0)\n  ) +\n  scale_x_continuous(\n    limits = c(0, 48),\n    breaks = c(0, 12, 24, 36, 48),\n    labels = c(\"0'\", \"12'\", \"24'\", \"36'\", \"48'\"),\n    expand = c(0.005, 0)\n  ) +\n  labs(\n    title = plot_title,\n    x = \"Game Time (minutes)\",\n    y = \"Hawks Win Probability\"\n  ) +\n  theme_minimal(base_size = 8) +\n  theme(\n    plot.background    = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    panel.background   = element_rect(fill = PAGE_BG, color = NA),\n    panel.grid.major.y = element_line(color = INK_MUTED, linewidth = 0.2),\n    panel.grid.major.x = element_blank(),\n    panel.grid.minor   = element_blank(),\n    panel.border       = element_blank(),\n    axis.line.x        = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.line.y        = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.title         = element_text(color = INK, size = 10),\n    axis.text          = element_text(color = INK_SOFT, size = 8),\n    plot.title         = element_text(color = INK, size = 12, margin = margin(b = 10)),\n    plot.margin        = margin(t = 16, r = 20, b = 12, l = 12)\n  )\n\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 8,\n  height   = 4.5,\n  units    = \"in\",\n  dpi      = 400\n)\n"}