{"spec_id":"stem-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' stem-basic: Basic Stem Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 86/100 | Created: 2026-07-25\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\"\nINK      <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nGRID_COLOR <- adjustcolor(INK, alpha.f = 0.15)\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\", # 1 - first categorical series (brand green)\n  \"#C475FD\", \"#4467A3\", \"#BD8233\",\n  \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# --- Data --------------------------------------------------------------------\n# Discrete-time impulse response of a damped, decaying oscillator - the\n# canonical stem-plot use case in signal processing.\nn <- 40\ndf <- tibble::tibble(\n  n = 0:(n - 1),\n  amplitude = exp(-0.09 * n) * cos(0.55 * n) + rnorm(n, sd = 0.015)\n)\n\n# --- Plot ----------------------------------------------------------------\ntitle_txt <- \"stem-basic · r · ggplot2 · anyplot.ai\"\n\np <- ggplot(df, aes(x = n, y = amplitude)) +\n  geom_hline(yintercept = 0, color = INK_SOFT, linewidth = 0.4) +\n  geom_segment(\n    aes(x = n, xend = n, y = 0, yend = amplitude),\n    color = IMPRINT_PALETTE[1], linewidth = 1.2\n  ) +\n  geom_point(\n    fill = IMPRINT_PALETTE[1],\n    color = if (THEME == \"light\") \"#FFFDF6\" else \"#242420\",\n    shape = 21, size = 3.2, stroke = 0.9\n  ) +\n  scale_x_continuous(breaks = seq(0, n - 1, by = 5)) +\n  labs(\n    title = title_txt,\n    x = \"Sample index n\",\n    y = \"Amplitude\"\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.y = element_line(color = GRID_COLOR, linewidth = 0.3),\n    panel.grid.minor  = element_blank(),\n    panel.grid.major.x = 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 = 12, face = \"bold\"),\n    plot.margin       = margin(t = 12, r = 16, b = 8, l = 8)\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"}