{"spec_id":"area-stacked","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' area-stacked: Stacked Area Chart\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 92/100 | Updated: 2026-08-18\n\nlibrary(ggplot2)\nlibrary(dplyr)\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\n# Imprint palette, canonical order 1-5 (see prompts/default-style-guide.md\n# \"Categorical Palette\")\nIMPRINT_PALETTE <- c(\n  \"#009E73\",  # 1 — Industrial (largest, brand green)\n  \"#C475FD\",  # 2 — Transportation\n  \"#4467A3\",  # 3 — Residential\n  \"#BD8233\",  # 4 — Commercial\n  \"#AE3030\"   # 5 — Agriculture\n)\n\n# --- Data -----------------------------------------------------------------\n# Annual energy consumption by sector, largest contributor first so the\n# stack reads largest-at-bottom (per spec notes).\nyears   <- 2010:2024\nsectors <- c(\"Industrial\", \"Transportation\", \"Residential\", \"Commercial\", \"Agriculture\")\nbase_twh  <- c(220, 150, 130, 90, 40)\ntrend_twh <- c(-1.5, 2.2, 1.6, 1.1, 0.3)  # electrification shifts share toward transport/residential\n\nconsumption <- vapply(seq_along(sectors), function(i) {\n  pmax(base_twh[i] + trend_twh[i] * (years - years[1]) + rnorm(length(years), 0, 4), 5)\n}, numeric(length(years)))\n\ndf <- tibble(\n  year       = rep(years, times = length(sectors)),\n  sector     = factor(rep(sectors, each = length(years)), levels = sectors),\n  twh        = as.vector(consumption)\n)\n\n# Transportation has the steepest growth trend (electrification shifting\n# demand its way) — a visibly bolder separator stroke gives it a focal point\n# among the five stacked layers without adding a callout annotation. Fills\n# stay fully opaque (no alpha) so the panel gridlines never bleed through.\nlinewidth_by_sector <- c(Industrial = 0.3, Transportation = 1.0, Residential = 0.3,\n                          Commercial = 0.3, Agriculture = 0.3)\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(df, aes(x = year, y = twh, fill = sector, linewidth = sector)) +\n  geom_area(position = position_stack(reverse = TRUE), color = PAGE_BG) +\n  scale_fill_manual(values = IMPRINT_PALETTE, name = \"Sector\",\n                     guide = guide_legend(reverse = TRUE)) +\n  scale_linewidth_manual(values = linewidth_by_sector, guide = \"none\") +\n  scale_x_continuous(breaks = seq(2010, 2024, by = 2), expand = c(0, 0)) +\n  scale_y_continuous(labels = label_number(),\n                      expand = expansion(mult = c(0, 0.05))) +\n  labs(\n    title = \"area-stacked · r · ggplot2 · anyplot.ai\",\n    x     = \"Year\",\n    y     = \"Energy Consumption (TWh)\"\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   = element_blank(),\n    panel.grid.major.y = element_line(color = INK, linewidth = 0.15),\n    axis.line          = element_line(color = INK_SOFT, linewidth = 0.3),\n    axis.ticks         = element_blank(),\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    legend.background  = element_blank(),\n    legend.key         = element_blank(),\n    legend.text        = element_text(color = INK_SOFT, size = 8),\n    legend.title       = element_text(color = INK, size = 10),\n    legend.position     = \"right\"\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"}