{"spec_id":"feynman-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' feynman-basic: Feynman Diagram for Particle Interactions\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 93/100 | Created: 2026-06-03\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(tibble)\nlibrary(ragg)\n\n# --- Theme tokens -----------------------------------------------------------\nTHEME       <- Sys.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\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 palette — semantic assignments for all 4 particle types\nCOL_FERMION <- \"#009E73\"  # pos 1 — quarks/fermions (green)\nCOL_BOSON   <- \"#C475FD\"  # pos 2 — scalar Higgs boson (lavender)\nCOL_PHOTON  <- \"#4467A3\"  # pos 3 — photons (blue, EM radiation)\nCOL_GLUON   <- \"#BD8233\"  # pos 4 — gluons (ochre, QCD)\n\n# --- Helper: wavy photon line (sinusoidal, perpendicular to path) -----------\nwavy_line <- function(x0, y0, x1, y1, n_waves = 3, amplitude = 0.35, n_pts = 300) {\n  t   <- seq(0, 1, length.out = n_pts)\n  dx  <- x1 - x0;  dy  <- y1 - y0\n  len <- sqrt(dx^2 + dy^2)\n  px  <- -dy / len; py  <-  dx / len\n  wave <- amplitude * sin(n_waves * 2 * pi * t)\n  tibble(x = x0 + t * dx + wave * px,\n         y = y0 + t * dy + wave * py)\n}\n\n# --- Helper: curly gluon line (higher-frequency sinusoid — visually curlier) -\ngluon_line <- function(x0, y0, x1, y1, n_loops = 6, amplitude = 0.42, n_pts = 500) {\n  t   <- seq(0, 1, length.out = n_pts)\n  dx  <- x1 - x0;  dy  <- y1 - y0\n  len <- sqrt(dx^2 + dy^2)\n  px  <- -dy / len; py  <-  dx / len\n  curl <- amplitude * sin(n_loops * 2 * pi * t)\n  tibble(x = x0 + t * dx + curl * px,\n         y = y0 + t * dy + curl * py)\n}\n\n# --- Diagram: gg -> H -> gamma gamma (Higgs via gluon fusion) ---------------\n# Coordinate space: x in [0, 16], y in [0, 9]  (16:9 aspect ratio)\n#\n# Process: two gluons (curly) fuse via a top-quark loop (fermion triangle)\n# producing a Higgs boson (dashed) that decays to two photons (wavy).\n# All 4 Feynman line types are shown.\n\nVT <- c(5.0, 6.2)   # top gluon-quark vertex\nVB <- c(5.0, 2.8)   # bottom gluon-quark vertex\nVR <- c(8.5, 4.5)   # quark-loop / Higgs emission vertex\nVH <- c(12.0, 4.5)  # Higgs decay vertex (H -> gamma gamma)\n\n# Gluon lines (curly, ochre) — incoming from left\ngluons <- bind_rows(\n  gluon_line(0.5, 7.2, VT[1], VT[2], n_loops = 5) |> mutate(group = \"g_top\"),\n  gluon_line(0.5, 1.8, VB[1], VB[2], n_loops = 5) |> mutate(group = \"g_bot\")\n)\n\n# Fermion (top quark) loop — triangle: VT -> VR -> VB -> VT\nquark_loop <- tibble(\n  x    = c(VT[1], VR[1], VB[1]),\n  y    = c(VT[2], VR[2], VB[2]),\n  xend = c(VR[1], VB[1], VT[1]),\n  yend = c(VR[2], VB[2], VT[2])\n)\n\n# Higgs propagator (dashed, lavender) — VR to VH\nhiggs_seg <- tibble(x = VR[1], y = VR[2], xend = VH[1], yend = VH[2])\n\n# Photon lines (wavy, blue) — Higgs decays to two photons\nphotons <- bind_rows(\n  wavy_line(VH[1], VH[2], 15.5, 7.2, n_waves = 3) |> mutate(group = \"gamma_top\"),\n  wavy_line(VH[1], VH[2], 15.5, 1.8, n_waves = 3) |> mutate(group = \"gamma_bot\")\n)\n\n# All four interaction vertices\nvertices <- tibble(\n  x = c(VT[1], VB[1], VR[1], VH[1]),\n  y = c(VT[2], VB[2], VR[2], VH[2])\n)\n\n# Particle labels — colored to match their line type\nlabels_df <- tibble(\n  x     = c(0.2,      0.2,      10.25,    15.7,     15.7,     6.2),\n  y     = c(7.4,      1.6,      5.1,      7.4,      1.6,      4.5),\n  label = c(\"g\",      \"g\",      \"H\",      \"γ\", \"γ\", \"t\"),\n  hjust = c(1.0,      1.0,      0.5,      0.0,      0.0,      0.5),\n  vjust = c(0.5,      0.5,      0.0,      0.5,      0.5,      0.5),\n  col   = c(COL_GLUON, COL_GLUON, COL_BOSON, COL_PHOTON, COL_PHOTON, COL_FERMION)\n)\n\n# --- Title ------------------------------------------------------------------\nplot_title    <- \"feynman-basic · r · ggplot2 · anyplot.ai\"\nplot_subtitle <- \"Higgs Boson Production via Gluon Fusion:  gg → H → γγ\"\n\n# --- Build plot -------------------------------------------------------------\np <- ggplot() +\n  # Gluon curly lines (ochre) — two incoming gluons\n  geom_path(data = gluons, aes(x = x, y = y, group = group),\n            color = COL_GLUON, linewidth = 1.0) +\n  # Top-quark fermion loop — triangle with directional arrows (green)\n  geom_segment(data = quark_loop,\n               aes(x = x, y = y, xend = xend, yend = yend),\n               color = COL_FERMION, linewidth = 1.0,\n               arrow = arrow(length = unit(0.20, \"cm\"), type = \"closed\", ends = \"last\")) +\n  # Higgs scalar propagator (lavender dashed)\n  geom_segment(data = higgs_seg, aes(x = x, y = y, xend = xend, yend = yend),\n               color = COL_BOSON, linewidth = 1.2, linetype = \"dashed\") +\n  # Photon wavy lines (blue) — two outgoing photons from H decay\n  geom_path(data = photons, aes(x = x, y = y, group = group),\n            color = COL_PHOTON, linewidth = 1.0) +\n  # Interaction vertex dots (theme-adaptive INK color)\n  geom_point(data = vertices, aes(x = x, y = y),\n             color = INK, size = 3.5, shape = 16) +\n  # Particle labels — colored italic, using scale_color_identity()\n  geom_text(data = labels_df,\n            aes(x = x, y = y, label = label,\n                hjust = hjust, vjust = vjust, color = col),\n            size = 5.5, fontface = \"italic\") +\n  scale_color_identity() +\n  # Time direction arrow at bottom\n  annotate(\"segment\", x = 5.5, xend = 10.5, y = 0.35, yend = 0.35,\n           color = INK_MUTED, linewidth = 0.7,\n           arrow = arrow(length = unit(0.15, \"cm\"), type = \"open\")) +\n  annotate(\"text\", x = 8.0, y = 0.02, label = \"time\",\n           color = INK_MUTED, size = 4.0, hjust = 0.5) +\n  coord_cartesian(xlim = c(-0.5, 16.5), ylim = c(-0.2, 9.3)) +\n  labs(title = plot_title, subtitle = plot_subtitle) +\n  theme_void() +\n  theme(\n    plot.background = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    plot.title      = element_text(color = INK,      size = 12, hjust = 0.5,\n                                   margin = margin(t = 15, b = 5)),\n    plot.subtitle   = element_text(color = INK_SOFT, size = 9,  hjust = 0.5,\n                                   margin = margin(b = 10)),\n    plot.margin     = margin(t = 10, r = 30, b = 20, l = 30)\n  )\n\n# --- Save -------------------------------------------------------------------\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"}