{"spec_id":"contour-filled","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' contour-filled: Filled Contour Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 87/100 | Created: 2026-09-04\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(ragg)\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\"\nDIV_MID     <- if (THEME == \"light\") \"#FAF8F1\" else \"#1A1A17\"\n\n# --- Data --------------------------------------------------------------------\n# Classic \"peaks\" surface: two Gaussian peaks and a saddle-shaped valley,\n# a smooth 2D scalar field with a natural zero midpoint (peak vs. valley).\npeaks <- function(x, y) {\n  3 * (1 - x)^2 * exp(-(x^2) - (y + 1)^2) -\n    10 * (x / 5 - x^3 - y^5) * exp(-x^2 - y^2) -\n    1 / 3 * exp(-(x + 1)^2 - y^2)\n}\n\ngrid_axis <- seq(-3, 3, length.out = 80)\nsurface <- expand.grid(x = grid_axis, y = grid_axis) %>%\n  mutate(z = peaks(x, y))\n\n# --- Colors --------------------------------------------------------------\n# imprint_div (diverging): matte-red -> theme-adaptive midpoint -> blue,\n# used because the surface has a meaningful zero midpoint (valley vs. peak).\nn_levels <- 12\nlevel_breaks <- pretty(range(surface$z), n = n_levels)\nband_colors <- colorRampPalette(c(\"#AE3030\", DIV_MID, \"#4467A3\"))(length(level_breaks) - 1)\n\n# --- Storytelling: call out the primary peak and valley -------------------\npeak_row   <- surface[which.max(surface$z), ]\nvalley_row <- surface[which.min(surface$z), ]\n# Offset labels toward the plot center so they stay inside the fixed domain\n# regardless of how close the extremum sits to the -3..3 edge.\npeak_label_y   <- peak_row$y - sign(peak_row$y) * 0.45\nvalley_label_y <- valley_row$y - sign(valley_row$y) * 0.45\n\n# --- Plot ----------------------------------------------------------------\np <- ggplot(surface, aes(x, y, z = z)) +\n  geom_contour_filled(breaks = level_breaks) +\n  geom_contour(breaks = level_breaks, color = INK_SOFT, linewidth = 0.25, alpha = 0.5) +\n  geom_point(\n    data = dplyr::bind_rows(peak_row, valley_row), aes(x, y),\n    inherit.aes = FALSE, shape = 21, size = 2.2, stroke = 0.6,\n    color = INK, fill = PAGE_BG\n  ) +\n  annotate(\"text\", x = peak_row$x, y = peak_label_y, label = \"Peak\",\n           color = INK, size = 2.8, fontface = \"bold\") +\n  annotate(\"text\", x = valley_row$x, y = valley_label_y, label = \"Valley\",\n           color = INK, size = 2.8, fontface = \"bold\") +\n  scale_fill_manual(\n    values = band_colors,\n    guide = guide_legend(reverse = TRUE, title = \"Elevation (m)\")\n  ) +\n  coord_fixed(ratio = 1) +\n  labs(\n    title = \"contour-filled · r · ggplot2 · anyplot.ai\",\n    x = \"X Coordinate (km)\",\n    y = \"Y Coordinate (km)\"\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           = element_blank(),\n    panel.border         = element_rect(color = INK_SOFT, fill = NA, linewidth = 0.25),\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),\n    legend.background    = element_rect(fill = PAGE_BG, color = NA),\n    legend.text          = element_text(color = INK_SOFT, size = 8),\n    legend.title         = element_text(color = INK, size = 10),\n    legend.key           = element_rect(fill = PAGE_BG, color = NA),\n    legend.key.size      = unit(0.35, \"cm\"),\n    legend.key.spacing.y = unit(1, \"pt\")\n  )\n\n# --- Save --------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 6,\n  height   = 6,\n  units    = \"in\",\n  dpi      = 400\n)\n"}