{"spec_id":"contour-density","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' contour-density: Density Contour Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 89/100 | Created: 2026-09-04\n\nlibrary(ggplot2)\nlibrary(ragg)\nlibrary(scales)\n\nset.seed(42)\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\"\n\n# --- Data -----------------------------------------------------------------\n# Old Faithful geyser: eruption duration vs. waiting time until the next\n# eruption. The bivariate distribution is famously bimodal, which makes it a\n# clean showcase for density contours (short/frequent vs. long/rare bursts).\ndf <- data.frame(\n  eruption_duration = faithful$eruptions,\n  waiting_time      = faithful$waiting\n)\n\n# stat_density_2d evaluates its KDE grid exactly over the trained scale\n# range (ggplot2 passes `scales$x$dimension()` straight to MASS::kde2d's\n# `lims`), so a data point sitting right at the min/max of that range\n# leaves no room for its contour to close and the outermost isoband gets\n# cut into a jagged notch. The smaller cluster is tighter than the overall\n# spread that the shared bandwidth is fit to, so 8% slack was not enough\n# room for its isoband to taper to zero before hitting the grid edge;\n# 25% is enough for both clusters to close cleanly.\nx_rng  <- range(df$eruption_duration)\ny_rng  <- range(df$waiting_time)\nx_pad  <- diff(x_rng) * 0.25\ny_pad  <- diff(y_rng) * 0.25\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(df, aes(x = eruption_duration, y = waiting_time)) +\n  stat_density_2d(\n    aes(fill = after_stat(level)),\n    geom        = \"polygon\",\n    color       = NA,\n    contour_var = \"density\",\n    n           = 300,\n    bins        = 8\n  ) +\n  geom_point(color = INK, size = 1.0, alpha = 0.25) +\n  scale_fill_gradient(low = \"#009E73\", high = \"#4467A3\", name = \"Density\") +\n  scale_x_continuous(limits = c(x_rng[1] - x_pad, x_rng[2] + x_pad), expand = expansion(mult = 0.02)) +\n  scale_y_continuous(limits = c(y_rng[1] - y_pad, y_rng[2] + y_pad), expand = expansion(mult = 0.02)) +\n  labs(\n    title    = \"Old Faithful Eruptions · contour-density · r · ggplot2 · anyplot.ai\",\n    subtitle = \"Two distinct eruption modes: short/frequent and long/rare bursts\",\n    x        = \"Eruption Duration (min)\",\n    y        = \"Waiting Time to Next Eruption (min)\"\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  = element_line(color = scales::alpha(INK, 0.15), linewidth = 0.4),\n    panel.grid.minor  = element_blank(),\n    axis.title        = element_text(color = INK, size = 10),\n    axis.text         = element_text(color = INK_SOFT, size = 8),\n    axis.ticks        = element_blank(),\n    plot.title        = element_text(color = INK, size = 12, face = \"bold\"),\n    plot.subtitle     = element_text(color = INK_SOFT, size = 9),\n    legend.title      = element_text(color = INK, size = 10),\n    legend.text       = element_text(color = INK_SOFT, size = 8),\n    legend.background = element_blank(),\n    legend.key        = element_blank()\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"}