{"spec_id":"scatter-regression-polynomial","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' scatter-regression-polynomial: Scatter Plot with Polynomial Regression\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 90/100 | Created: 2026-08-11\n\nlibrary(ggplot2)\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\"\n\n# Imprint categorical palette — 8 hues, theme-independent\nIMPRINT_PALETTE <- c(\n  \"#009E73\", # 1 — brand green (ALWAYS first series)\n  \"#C475FD\", # 2 — lavender\n  \"#4467A3\", # 3 — blue (used here for the fitted curve)\n  \"#BD8233\", # 4 — ochre\n  \"#AE3030\", # 5 — matte red\n  \"#2ABCCD\", # 6 — cyan\n  \"#954477\", # 7 — rose\n  \"#99B314\"  # 8 — lime\n)\n\n# --- Data -----------------------------------------------------------------\n# Nitrogen fertilizer applied to a corn field vs. resulting yield — a classic\n# diminishing-returns relationship: yield climbs with more nitrogen, then\n# plateaus and declines once over-fertilization sets in (quadratic pattern).\nn <- 120\nfertilizer_kg_ha <- runif(n, min = 0, max = 300)\nnoise <- rnorm(n, mean = 0, sd = 0.8)\ncorn_yield_t_ha <- -0.00018 * fertilizer_kg_ha^2 +\n  0.075 * fertilizer_kg_ha +\n  2.5 +\n  noise\n\ndf <- tibble::tibble(\n  fertilizer_kg_ha = fertilizer_kg_ha,\n  corn_yield_t_ha   = corn_yield_t_ha\n)\n\n# Fit the degree-2 polynomial to report R² and the equation coefficients\npoly_fit <- lm(corn_yield_t_ha ~ poly(fertilizer_kg_ha, 2, raw = TRUE), data = df)\nr_squared <- summary(poly_fit)$r.squared\ncoefs <- coef(poly_fit)\n\nannotation_label <- paste0(\n  sprintf(\"y = %.5fx² + %.3fx + %.2f\", coefs[3], coefs[2], coefs[1]),\n  \"\\n\",\n  sprintf(\"R² = %.3f\", r_squared)\n)\nann_x <- min(df$fertilizer_kg_ha) + 0.03 * diff(range(df$fertilizer_kg_ha))\nann_y <- max(df$corn_yield_t_ha) - 0.02 * diff(range(df$corn_yield_t_ha))\n\n# --- Plot -------------------------------------------------------------------\ntitle_text <- \"Corn Yield vs. Nitrogen Fertilizer · scatter-regression-polynomial · r · ggplot2 · anyplot.ai\"\n\np <- ggplot(df, aes(x = fertilizer_kg_ha, y = corn_yield_t_ha)) +\n  geom_smooth(\n    method = \"lm\",\n    formula = y ~ poly(x, 2, raw = TRUE),\n    se = TRUE,\n    color = IMPRINT_PALETTE[3],\n    fill = IMPRINT_PALETTE[3],\n    alpha = 0.18,\n    linewidth = 1.2\n  ) +\n  geom_point(color = IMPRINT_PALETTE[1], size = 2.5, alpha = 0.65) +\n  annotate(\n    \"text\",\n    x = ann_x, y = ann_y,\n    label = annotation_label,\n    hjust = 0, vjust = 1,\n    size = 3.2, lineheight = 1.15,\n    color = INK\n  ) +\n  labs(\n    title = title_text,\n    x = \"Nitrogen Fertilizer (kg/ha)\",\n    y = \"Corn Yield (t/ha)\"\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 = INK_SOFT, linewidth = 0.15),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_blank(),\n    axis.title        = element_text(color = INK, size = 10),\n    axis.text         = element_text(color = INK_SOFT, size = 8),\n    axis.line         = element_line(color = INK_SOFT),\n    axis.ticks        = element_blank(),\n    plot.title        = element_text(color = INK, size = 9, face = \"bold\"),\n    plot.margin       = margin(t = 12, r = 20, b = 10, l = 10)\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"}