{"spec_id":"acf-pacf","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' acf-pacf: Autocorrelation and Partial Autocorrelation (ACF/PACF) Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 88/100 | Created: 2026-06-10\n\nlibrary(ggplot2)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens — Imprint palette (see prompts/default-style-guide.md)\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\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\",  # 1 — brand green (first series)\n  \"#C475FD\",  # 2 — lavender\n  \"#4467A3\",  # 3 — blue\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: synthetic AR(2) time series (n = 300 observations)\n# phi1 = 0.7, phi2 = -0.3 produces geometrically decaying ACF and PACF cutoff at lag 2\nts_data <- arima.sim(model = list(ar = c(0.7, -0.3)), n = 300, sd = 0.5)\nn_obs   <- length(ts_data)\nn_lags  <- 36\nci      <- 1.96 / sqrt(n_obs)\n\n# Compute ACF (lags 0–36) and PACF (lags 1–36)\nacf_out  <- acf(ts_data,  lag.max = n_lags, plot = FALSE)\npacf_out <- pacf(ts_data, lag.max = n_lags, plot = FALSE)\n\nacf_df <- data.frame(\n  lag  = 0:n_lags,\n  corr = as.numeric(acf_out$acf),\n  type = \"ACF\"\n)\npacf_df <- data.frame(\n  lag  = 1:n_lags,\n  corr = as.numeric(pacf_out$acf),\n  type = \"PACF\"\n)\ndf      <- rbind(acf_df, pacf_df)\ndf$type <- factor(df$type, levels = c(\"ACF\", \"PACF\"))\ndf$significant <- abs(df$corr) > ci\n\nplot_title <- \"acf-pacf · r · ggplot2 · anyplot.ai\"\n\n# Plot\np <- ggplot(df, aes(x = lag, y = corr)) +\n  geom_hline(yintercept = 0,   color = INK_SOFT,  linewidth = 0.4) +\n  geom_hline(yintercept =  ci, color = INK_MUTED, linetype = \"dashed\", linewidth = 0.5) +\n  geom_hline(yintercept = -ci, color = INK_MUTED, linetype = \"dashed\", linewidth = 0.5) +\n  geom_segment(\n    aes(xend = lag, yend = 0, color = significant),\n    linewidth = 1.2\n  ) +\n  scale_color_manual(\n    values = c(\"TRUE\" = IMPRINT_PALETTE[1], \"FALSE\" = INK_MUTED),\n    guide  = \"none\"\n  ) +\n  facet_wrap(~ type, ncol = 1, scales = \"free_y\") +\n  scale_x_continuous(breaks = seq(0, n_lags, by = 5)) +\n  labs(\n    x     = \"Lag\",\n    y     = \"Autocorrelation\",\n    title = plot_title\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.2),\n    panel.grid.minor = element_blank(),\n    panel.border          = element_blank(),\n    axis.line.x.bottom    = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.line.y.left      = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.title       = element_text(color = INK,        size = 10),\n    axis.text        = element_text(color = INK_SOFT,   size = 8),\n    plot.title       = element_text(color = INK,        size = 12,\n                                    margin = margin(b = 8)),\n    strip.text       = element_text(color = INK,        size = 10, face = \"bold\"),\n    strip.background = element_rect(fill = ELEVATED_BG, color = INK_SOFT),\n    plot.margin      = margin(t = 12, r = 12, b = 12, l = 12)\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"}