{"spec_id":"scatter-regression-linear","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' scatter-regression-linear: Scatter Plot with Linear Regression\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 91/100 | Created: 2026-08-05\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\"\nGRID_COLOR  <- grDevices::adjustcolor(INK, alpha.f = 0.15)\nIMPRINT_PALETTE <- c(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                     \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\")\n\n# --- Data -----------------------------------------------------------------\nn <- 150\nad_spend <- runif(n, 5, 50)\nsales_revenue <- 2.4 * ad_spend + 18 + rnorm(n, 0, 12)\ndf <- tibble::tibble(ad_spend = ad_spend, sales_revenue = sales_revenue)\n\nfit <- lm(sales_revenue ~ ad_spend, data = df)\nslope <- coef(fit)[[\"ad_spend\"]]\nintercept <- coef(fit)[[\"(Intercept)\"]]\nr_squared <- summary(fit)$r.squared\n\nequation_label <- sprintf(\"y = %.2fx + %.2f\\nR² = %.3f\", slope, intercept, r_squared)\n\n# --- Title (fontsize scales with length, see plot-generator.md) -----------\ntitle_text <- paste0(\n  \"Advertising Spend vs Sales Revenue · scatter-regression-linear · \",\n  \"r · ggplot2 · anyplot.ai\"\n)\ntitle_len <- nchar(title_text)\ntitle_size <- if (title_len > 67) round(12 * 67 / title_len) else 12\ntitle_size <- max(title_size, 8)\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(df, aes(x = ad_spend, y = sales_revenue)) +\n  geom_smooth(\n    method = \"lm\", formula = y ~ x, se = TRUE, level = 0.95,\n    color = IMPRINT_PALETTE[3], fill = IMPRINT_PALETTE[3],\n    linewidth = 1.4, alpha = 0.22\n  ) +\n  geom_point(\n    shape = 21, fill = IMPRINT_PALETTE[1], color = PAGE_BG,\n    size = 3, stroke = 0.3, alpha = 0.7\n  ) +\n  geom_rug(\n    sides = \"bl\", color = IMPRINT_PALETTE[1], alpha = 0.35,\n    linewidth = 0.3, length = unit(0.015, \"npc\")\n  ) +\n  annotate(\n    \"label\",\n    x = min(df$ad_spend), y = max(df$sales_revenue),\n    label = equation_label, hjust = 0, vjust = 1,\n    size = 3.2, color = INK, fill = ELEVATED_BG, label.size = 0.25,\n    label.padding = unit(0.5, \"lines\")\n  ) +\n  labs(\n    title = title_text,\n    x = \"Advertising Spend ($ thousands)\",\n    y = \"Sales Revenue ($ thousands)\"\n  ) +\n  scale_x_continuous(expand = expansion(mult = c(0.02, 0.05))) +\n  scale_y_continuous(expand = expansion(mult = c(0.05, 0.1))) +\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.x = element_blank(),\n    panel.grid.major.y = element_line(color = GRID_COLOR, linewidth = 0.3),\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.line         = element_line(color = INK_SOFT),\n    plot.title        = element_text(color = INK, size = title_size, face = \"bold\"),\n    panel.border      = 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"}