{"spec_id":"qq-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' qq-basic: Basic Q-Q Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 93/100 | Created: 2026-07-24\n\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\"\nIMPRINT_PALETTE <- c(\n    \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n    \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# --- Data -----------------------------------------------------------------\n# Shaft diameter measurements (mm) from a machining quality-control line —\n# checking the normality assumption before running a process-capability test.\nn <- 150\nshaft_diameter_mm <- rnorm(n, mean = 25.00, sd = 0.08)\ndf <- tibble::tibble(sample = shaft_diameter_mm)\n\n# --- Confidence envelope ----------------------------------------------------\n# Pointwise 95% normal-theory band around the reference line (the classic\n# car::qqPlot approach): the standard error of each order statistic is scaled\n# by the local normal density, giving a band that narrows in the dense center\n# and widens in the tails — exactly where deviations are most informative.\np_i <- (seq_len(n) - 0.5) / n\nz <- qnorm(p_i)\nmu <- mean(shaft_diameter_mm)\nsigma <- sd(shaft_diameter_mm)\nse <- (sigma / dnorm(z)) * sqrt(p_i * (1 - p_i) / n)\nband <- tibble::tibble(\n    z = z,\n    fit = mu + sigma * z,\n    lower = fit - qnorm(0.975) * se,\n    upper = fit + qnorm(0.975) * se\n)\n\n# --- Plot -------------------------------------------------------------------\n# stat_qq_line draws the 45-degree reference line (Imprint \"neutral\" anchor,\n# since it is a baseline/reference element rather than a data series); the\n# ribbon is the 95% pointwise confidence envelope under normality, showing\n# whether the tail deviations exceed ordinary sampling variation.\np <- ggplot(df, aes(sample = sample)) +\n    geom_ribbon(\n        data = band, aes(x = z, ymin = lower, ymax = upper),\n        inherit.aes = FALSE, fill = INK, alpha = 0.08\n    ) +\n    stat_qq_line(color = INK, linewidth = 1.0) +\n    stat_qq(color = IMPRINT_PALETTE[1], size = 2.5, alpha = 0.6) +\n    labs(\n        title = \"qq-basic · r · ggplot2 · anyplot.ai\",\n        subtitle = sprintf(\"Shaft diameter QC measurements (n = %d) vs. normal distribution\", n),\n        x = \"Theoretical Quantiles\",\n        y = \"Sample Quantiles\"\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, linewidth = 0.15),\n        panel.grid.minor = element_blank(),\n        panel.border = 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),\n        plot.subtitle = element_text(color = INK_SOFT, size = 9)\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"}