{"spec_id":"area-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' area-basic: Basic Area Chart\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 90/100 | Created: 2026-05-28\n\nlibrary(ggplot2)\nlibrary(scales)\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\"\n# Raise alpha in dark theme — alpha=0.35 over #1A1A17 composites to near-opaque forest green\nFILL_ALPHA  <- if (THEME == \"light\") 0.40 else 0.50\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n  \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# --- Data -------------------------------------------------------------------\n# Daily website visitors (thousands) over 13 weeks starting Monday Jan 2 2023\ndays <- seq.Date(as.Date(\"2023-01-02\"), by = \"day\", length.out = 91)\nday_num <- seq_along(days)\nday_of_week <- as.integer(format(days, \"%u\"))  # 1=Mon ... 7=Sun\nweekend <- day_of_week %in% c(6L, 7L)\n\nbase_visitors  <- 8.5 + 0.04 * day_num\nweekly_effect  <- ifelse(weekend, -2.5, 0.8)\nnoise          <- rnorm(91, 0, 0.5)\nvisitors_k     <- round(pmax(1, base_visitors + weekly_effect + noise), 1)\n\ndf <- data.frame(date = days, visitors = visitors_k)\navg_visitors   <- mean(df$visitors)\n\n# --- Plot -------------------------------------------------------------------\nplot_title <- \"area-basic · r · ggplot2 · anyplot.ai\"\n\np <- ggplot(df, aes(x = date, y = visitors)) +\n  # Period average reference — drawn first so area fill sits above it\n  geom_hline(\n    yintercept = avg_visitors,\n    color      = INK_SOFT,\n    linewidth  = 0.45,\n    linetype   = \"dashed\",\n    alpha      = 0.7\n  ) +\n  geom_area(fill = IMPRINT_PALETTE[1], alpha = FILL_ALPHA) +\n  geom_line(color = IMPRINT_PALETTE[1], linewidth = 0.9) +\n  # Loess trend line — makes the underlying upward growth arc explicit\n  geom_smooth(\n    method    = \"loess\",\n    formula   = y ~ x,\n    se        = FALSE,\n    color     = INK_SOFT,\n    linewidth = 0.8,\n    linetype  = \"solid\",\n    span      = 0.65\n  ) +\n  scale_x_date(date_labels = \"%b %d\", date_breaks = \"2 weeks\") +\n  scale_y_continuous(\n    labels = label_comma(suffix = \"K\", accuracy = 1),\n    limits = c(0, NA),\n    expand = expansion(mult = c(0, 0.08))\n  ) +\n  labs(\n    title = plot_title,\n    x     = \"Date (2023)\",\n    y     = \"Daily Visitors\"\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.x = element_blank(),\n    panel.grid.minor.x = element_blank(),\n    panel.grid.major.y = element_line(color = INK, linewidth = 0.2),\n    panel.grid.minor.y = element_blank(),\n    panel.border       = element_blank(),\n    axis.line.x        = element_line(color = INK_SOFT, linewidth = 0.4),\n    axis.ticks.x       = element_line(color = INK_SOFT, linewidth = 0.3),\n    axis.ticks.y       = element_blank(),\n    axis.title         = element_text(color = INK,      size = 10),\n    axis.text          = element_text(color = INK_SOFT, size = 8),\n    axis.text.x        = element_text(angle = 30, hjust = 1),\n    plot.title         = element_text(color = INK,      size = 12, face = \"bold\"),\n    plot.margin        = margin(20, 30, 15, 20, unit = \"pt\")\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"}