{"spec_id":"histogram-capability","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' histogram-capability: Process Capability Plot with Specification Limits\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 91/100 | Created: 2026-06-20\n\nlibrary(ggplot2)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens (Imprint palette — see prompts/default-style-guide.md)\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\"\n\nIMPRINT_PALETTE <- c(\n  \"#009E73\",  # 1 — brand green  (histogram bars)\n  \"#C475FD\",  # 2 — lavender\n  \"#4467A3\",  # 3 — blue         (normal distribution curve)\n  \"#BD8233\",  # 4 — ochre        (target line)\n  \"#AE3030\",  # 5 — matte red    (LSL / USL spec limits)\n  \"#2ABCCD\",  # 6 — cyan\n  \"#954477\",  # 7 — rose\n  \"#99B314\"   # 8 — lime\n)\n\n# Process parameters — shaft diameter (mm), slightly off-centre mean\nLSL    <- 9.95\nUSL    <- 10.05\nTARGET <- 10.00\nN      <- 200\nMU     <- 10.01\nSIGMA  <- 0.012\n\n# Generate synthetic measurements\nmeasurements <- rnorm(N, mean = MU, sd = SIGMA)\n\n# Capability indices computed from sample statistics\nmu_hat    <- mean(measurements)\nsigma_hat <- sd(measurements)\nCp  <- (USL - LSL) / (6 * sigma_hat)\nCpk <- min(\n  (USL - mu_hat) / (3 * sigma_hat),\n  (mu_hat - LSL) / (3 * sigma_hat)\n)\n\n# Peak of the fitted normal curve (used for annotation y-positioning)\nmax_dens <- dnorm(mu_hat, mean = mu_hat, sd = sigma_hat)\n\ndf <- data.frame(x = measurements)\n\np <- ggplot(df, aes(x = x)) +\n  # Histogram bars scaled to probability density\n  geom_histogram(\n    aes(y = after_stat(density)),\n    bins      = 25,\n    fill      = IMPRINT_PALETTE[1],\n    color     = PAGE_BG,\n    alpha     = 0.75,\n    linewidth = 0.3\n  ) +\n  # Fitted normal distribution overlay\n  stat_function(\n    fun       = dnorm,\n    args      = list(mean = mu_hat, sd = sigma_hat),\n    color     = IMPRINT_PALETTE[3],\n    linewidth = 1.2,\n    n         = 512\n  ) +\n  # Specification limit lines (LSL and USL — semantic red for limits)\n  geom_vline(\n    xintercept = c(LSL, USL),\n    color      = IMPRINT_PALETTE[5],\n    linetype   = \"dashed\",\n    linewidth  = 0.9\n  ) +\n  # Target / nominal value line\n  geom_vline(\n    xintercept = TARGET,\n    color      = IMPRINT_PALETTE[4],\n    linetype   = \"dotdash\",\n    linewidth  = 0.9\n  ) +\n  # LSL label — positioned to the right of the line (low-density tail)\n  annotate(\"text\",\n    x = LSL, y = max_dens * 0.90,\n    label = \"LSL\", color = IMPRINT_PALETTE[5],\n    hjust = -0.15, size = 3.5, fontface = \"bold\"\n  ) +\n  # USL label — positioned to the left of the line\n  annotate(\"text\",\n    x = USL, y = max_dens * 0.90,\n    label = \"USL\", color = IMPRINT_PALETTE[5],\n    hjust = 1.15, size = 3.5, fontface = \"bold\"\n  ) +\n  # Target label — positioned to the left of the line\n  annotate(\"text\",\n    x = TARGET, y = max_dens * 0.75,\n    label = \"Target\", color = IMPRINT_PALETTE[4],\n    hjust = 1.12, size = 3.5, fontface = \"bold\"\n  ) +\n  # Normal fit label — inline on the curve at the right tail\n  annotate(\"text\",\n    x = mu_hat + 2.2 * sigma_hat, y = dnorm(mu_hat + 2.2 * sigma_hat, mu_hat, sigma_hat),\n    label = \"Normal fit\", color = IMPRINT_PALETTE[3],\n    hjust = 0, vjust = -0.4, size = 3.2, fontface = \"italic\"\n  ) +\n  # Cp / Cpk annotation box in the upper-right region\n  annotate(\"label\",\n    x          = USL - 0.001,\n    y          = max_dens * 0.55,\n    label      = sprintf(\"Cp  = %.2f\\nCpk = %.2f\", Cp, Cpk),\n    color      = INK,\n    fill       = ELEVATED_BG,\n    hjust      = 1,\n    label.size = 0.3,\n    size       = 3.8\n  ) +\n  labs(\n    title = \"Shaft Diameter · histogram-capability · r · ggplot2 · anyplot.ai\",\n    x     = \"Shaft Diameter (mm)\",\n    y     = \"Density\"\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 = element_line(color = INK_SOFT, linewidth = 0.2),\n    panel.grid.minor = element_blank(),\n    panel.border     = element_blank(),\n    axis.line        = element_line(color = INK_SOFT, linewidth = 0.4),\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                                    margin = margin(b = 10)),\n    plot.margin      = margin(t = 15, r = 20, b = 10, l = 10)\n  )\n\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"}