Feynman Diagram for Particle Interactions in ggplot2 (R)

The same plot in 14 other libraries — Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; Julia: Makie.jl; JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts, MUI X Charts. Compare all 15 side by side: Feynman Diagram for Particle Interactions in Python, R, Julia and JavaScript.

A Feynman diagram visualizes interactions between subatomic particles in quantum field theory. Different line styles represent different particle types: straight lines for fermions (electrons, quarks), wavy lines for photons, curly/looped lines for gluons, and dashed lines for scalar bosons (e.g., Higgs). Lines meet at vertices representing interaction points. Invented by Richard Feynman, these diagrams are both a computational tool and a cultural icon of modern physics.

Feynman Diagram for Particle Interactions rendered with ggplot2

Renders

R source (ggplot2)

#' anyplot.ai
#' feynman-basic: Feynman Diagram for Particle Interactions
#' Library: ggplot2 4.0.3 | R 4.6.1
#' Quality: 86/100 | Updated: 2026-12-01

library(ggplot2)
library(dplyr)
library(tibble)
library(ragg)

# --- Theme tokens -----------------------------------------------------------
THEME       <- Sys.getenv("ANYPLOT_THEME", "light")
PAGE_BG     <- if (THEME == "light") "#FAF8F1" else "#1A1A17"
INK         <- if (THEME == "light") "#1A1A17" else "#F0EFE8"
INK_SOFT    <- if (THEME == "light") "#4A4A44" else "#B8B7B0"
INK_MUTED   <- if (THEME == "light") "#6B6A63" else "#A8A79F"

# Imprint palette — semantic assignments for all 4 particle types
COL_FERMION <- "#009E73"  # pos 1 — quarks/fermions (green)
COL_BOSON   <- "#C475FD"  # pos 2 — scalar Higgs boson (lavender)
COL_PHOTON  <- "#4467A3"  # pos 3 — photons (blue, EM radiation)
COL_GLUON   <- "#BD8233"  # pos 4 — gluons (ochre, QCD)

# --- Helper: wavy photon line (sinusoidal, perpendicular to path) -----------
wavy_line <- function(x0, y0, x1, y1, n_waves = 3, amplitude = 0.35, n_pts = 300) {
  t   <- seq(0, 1, length.out = n_pts)
  dx  <- x1 - x0;  dy  <- y1 - y0
  len <- sqrt(dx^2 + dy^2)
  px  <- -dy / len; py  <-  dx / len
  wave <- amplitude * sin(n_waves * 2 * pi * t)
  tibble(x = x0 + t * dx + wave * px,
         y = y0 + t * dy + wave * py)
}

# --- Helper: curly gluon line (prolate cycloid — closed looped coils) ------
gluon_line <- function(x0, y0, x1, y1, n_loops = 5, radius = 0.3, n_pts = 600) {
  t   <- seq(0, 1, length.out = n_pts)
  dx  <- x1 - x0;  dy  <- y1 - y0
  len <- sqrt(dx^2 + dy^2)
  ux  <- dx / len; uy  <- dy / len
  px  <- -uy;      py  <- ux
  phi    <- n_loops * 2 * pi * t
  along  <- t * len - radius * 1.6 * sin(phi)
  across <- radius * 1.6 * (1 - cos(phi)) / 2 * 1.4
  tibble(x = x0 + along * ux + across * px,
         y = y0 + along * uy + across * py)
}

# --- Diagram: gg -> H -> gamma gamma (Higgs via gluon fusion) ---------------
# Coordinate space: x in [0, 16], y in [0, 9]  (16:9 aspect ratio)
#
# Two curly gluons enter a top-quark triangle (left), a dashed Higgs line joins
# it to a second top-quark triangle (right), and two wavy photons leave it.
# 6 vertices and 11 lines, all four line types in one diagram.

VT <- c(4.6, 6.0)    # left triangle: top gluon vertex
VB <- c(4.6, 3.0)    # left triangle: bottom gluon vertex
VR <- c(7.4, 4.5)    # left triangle: Higgs emission vertex
VH <- c(10.6, 4.5)   # right triangle: Higgs absorption vertex
PT <- c(13.4, 6.0)   # right triangle: top photon vertex
PB <- c(13.4, 3.0)   # right triangle: bottom photon vertex

# Gluon lines (curly, ochre) — incoming from left
gluons <- bind_rows(
  gluon_line(0.8, 7.4, VT[1], VT[2], n_loops = 5) |> mutate(group = "g_top"),
  gluon_line(VB[1], VB[2], 0.8, 1.6, n_loops = 5) |> mutate(group = "g_bot")
)

# Fermion (top quark) loops — each side runs around its triangle in one direction
quark_loops <- tibble(
  x    = c(VT[1], VR[1], VB[1], PT[1], PB[1], VH[1]),
  y    = c(VT[2], VR[2], VB[2], PT[2], PB[2], VH[2]),
  xend = c(VR[1], VB[1], VT[1], PB[1], VH[1], PT[1]),
  yend = c(VR[2], VB[2], VT[2], PB[2], VH[2], PT[2])
)

# Arrowheads sit mid-side so the direction reads unbroken through every vertex
quark_arrows <- quark_loops |>
  mutate(mx = (x + xend) / 2, my = (y + yend) / 2,
         ax = mx - (xend - x) * 0.02, ay = my - (yend - y) * 0.02,
         axend = mx + (xend - x) * 0.02, ayend = my + (yend - y) * 0.02)

# Higgs propagator (dashed, lavender) — between the two triangles
higgs_seg <- tibble(x = VR[1], y = VR[2], xend = VH[1], yend = VH[2])

# Photon lines (wavy, blue) — leave the right triangle
photons <- bind_rows(
  wavy_line(PT[1], PT[2], 15.6, 7.6, n_waves = 3) |> mutate(group = "gamma_top"),
  wavy_line(PB[1], PB[2], 15.6, 1.4, n_waves = 3) |> mutate(group = "gamma_bot")
)

# All six interaction vertices
vertices <- tibble(
  x = c(VT[1], VB[1], VR[1], VH[1], PT[1], PB[1]),
  y = c(VT[2], VB[2], VR[2], VH[2], PT[2], PB[2])
)

# Particle labels — colored to match their line type
labels_df <- tibble(
  x     = c(0.5,  0.5,  9.0,  15.75, 15.75, 6.2, 6.2, 4.15, 11.8, 11.8, 13.85),
  y     = c(7.6,  1.4,  4.75, 7.8,   1.2,   5.7, 3.3, 4.5,  5.7,  3.3,  4.5),
  label = c("g",  "g",  "H",  "γ",   "γ",   "t", "t", "t",  "t",  "t",  "t"),
  hjust = c(1.0,  1.0,  0.5,  0.0,   0.0,   0.5, 0.5, 0.5,  0.5,  0.5,  0.5),
  vjust = c(0.5,  0.5,  0.0,  0.5,   0.5,   0.5, 0.5, 0.5,  0.5,  0.5,  0.5),
  col   = c(rep(COL_GLUON, 2), COL_BOSON, rep(COL_PHOTON, 2), rep(COL_FERMION, 6))
)

# --- Title ------------------------------------------------------------------
plot_title    <- "feynman-basic · r · ggplot2 · anyplot.ai"
plot_subtitle <- "Higgs Boson Production via Gluon Fusion:  gg → H → γγ"

# --- Build plot -------------------------------------------------------------
p <- ggplot() +
  # Gluon curly lines (ochre) — two incoming gluons
  geom_path(data = gluons, aes(x = x, y = y, group = group),
            color = COL_GLUON, linewidth = 1.0) +
  # Top-quark fermion loops — solid triangle sides (green)
  geom_segment(data = quark_loops,
               aes(x = x, y = y, xend = xend, yend = yend),
               color = COL_FERMION, linewidth = 1.0) +
  # Mid-side arrowheads giving the loop direction
  geom_segment(data = quark_arrows,
               aes(x = ax, y = ay, xend = axend, yend = ayend),
               color = COL_FERMION, linewidth = 1.0,
               arrow = arrow(length = unit(0.22, "cm"), type = "closed", ends = "last")) +
  # Higgs scalar propagator (lavender dashed)
  geom_segment(data = higgs_seg, aes(x = x, y = y, xend = xend, yend = yend),
               color = COL_BOSON, linewidth = 1.2, linetype = "dashed") +
  # Photon wavy lines (blue) — two outgoing photons from H decay
  geom_path(data = photons, aes(x = x, y = y, group = group),
            color = COL_PHOTON, linewidth = 1.0) +
  # Interaction vertex dots (theme-adaptive INK color)
  geom_point(data = vertices, aes(x = x, y = y),
             color = INK, size = 3.5, shape = 16) +
  # Particle labels — colored italic, using scale_color_identity()
  geom_text(data = labels_df,
            aes(x = x, y = y, label = label,
                hjust = hjust, vjust = vjust, color = col),
            size = 5.5, fontface = "italic") +
  scale_color_identity() +
  # Time direction arrow at bottom
  annotate("segment", x = 5.5, xend = 10.5, y = 0.35, yend = 0.35,
           color = INK_MUTED, linewidth = 0.7,
           arrow = arrow(length = unit(0.15, "cm"), type = "open")) +
  annotate("text", x = 8.0, y = 0.02, label = "time",
           color = INK_MUTED, size = 4.0, hjust = 0.5) +
  coord_cartesian(xlim = c(-0.5, 16.5), ylim = c(-0.2, 9.3)) +
  labs(title = plot_title, subtitle = plot_subtitle) +
  theme_void() +
  theme(
    plot.background = element_rect(fill = PAGE_BG, color = PAGE_BG),
    plot.title      = element_text(color = INK,      size = 12, hjust = 0.5,
                                   margin = margin(t = 15, b = 5)),
    plot.subtitle   = element_text(color = INK_SOFT, size = 9,  hjust = 0.5,
                                   margin = margin(b = 10)),
    plot.margin     = margin(t = 10, r = 30, b = 20, l = 30)
  )

# --- Save -------------------------------------------------------------------
ggsave(
  filename = sprintf("plot-%s.png", THEME),
  plot     = p,
  device   = ragg::agg_png,
  width    = 8,
  height   = 4.5,
  units    = "in",
  dpi      = 400
)

Retrieve this implementation

Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/feynman-basic/ggplot2/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.

{
  "spec_id": "feynman-basic",
  "language": "r",
  "library": "ggplot2",
  "page": "https://anyplot.ai/feynman-basic/r/ggplot2",
  "hub": "https://anyplot.ai/feynman-basic",
  "code_json": "https://api.anyplot.ai/specs/feynman-basic/ggplot2/code",
  "spec_json": "https://api.anyplot.ai/specs/feynman-basic",
  "render_light_png": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/r/ggplot2/plot-light.png",
  "render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/feynman-basic/r/ggplot2/plot-dark.png",
  "quality_score": 86.0,
  "license": "MIT",
  "guide": "https://anyplot.ai/llms.txt"
}

Part of Feynman Diagram for Particle Interactions on anyplot.ai.

Other implementations