{"spec_id":"histogram-cumulative","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' histogram-cumulative: Cumulative Histogram\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 84/100 | Created: 2026-09-05\n\nlibrary(ggplot2)\nlibrary(ragg)\n\nset.seed(42)\n\n# --- Theme tokens (see prompts/default-style-guide.md \"Theme-adaptive Chrome\") ----\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\"\nBRAND    <- \"#009E73\"  # Imprint palette position 1 — always first series\n\n# --- Data: net fill weight of cereal boxes on a packaging line (target 500 g) ----\nbox_weights <- rnorm(600, mean = 500, sd = 9)\n\nbin_width <- 3\nbreaks <- seq(\n  floor(min(box_weights) / bin_width) * bin_width,\n  ceiling(max(box_weights) / bin_width) * bin_width,\n  by = bin_width\n)\ncounts <- hist(box_weights, breaks = breaks, plot = FALSE)$counts\n\n# Right bin edge + running total up to that edge; the leading zero anchors the\n# step at the histogram's left boundary so geom_step starts from the ground.\ncum_df <- data.frame(\n  weight    = c(breaks[1], breaks[-1]),\n  cum_count = c(0, cumsum(counts))\n)\nn_total <- length(box_weights)\n\n# --- Plot -------------------------------------------------------------------\nx_max <- max(cum_df$weight)\n\np <- ggplot(cum_df, aes(x = weight, y = cum_count)) +\n  geom_hline(yintercept = n_total, linetype = \"dashed\", color = BRAND, linewidth = 0.6, alpha = 0.45) +\n  geom_step(color = BRAND, linewidth = 1.1, direction = \"hv\") +\n  geom_point(data = cum_df[-1, ], color = BRAND, size = 2.75) +\n  annotate(\n    \"text\",\n    x = x_max, y = n_total, label = sprintf(\"Total (n = %d)\", n_total),\n    color = INK_SOFT, size = 2.8, hjust = 1, vjust = -0.7\n  ) +\n  labs(\n    title = \"histogram-cumulative · r · ggplot2 · anyplot.ai\",\n    x     = \"Box fill weight (g)\",\n    y     = \"Cumulative count\"\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.minor.y = element_blank(),\n    panel.grid.major.y = element_line(color = INK, linewidth = 0.25),\n    axis.line         = element_line(color = INK_SOFT),\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  )\n\n# --- Save (PNG, both themes) --------------------------------------------------\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"}