{"spec_id":"box-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' box-basic: Basic Box Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 92/100 | Created: 2026-05-28\n\nlibrary(ggplot2)\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\"\nELEVATED_BG <- if (THEME == \"light\") \"#FFFDF6\" else \"#242420\"\nINK         <- if (THEME == \"light\") \"#1A1A17\" else \"#F0EFE8\"\nINK_SOFT    <- if (THEME == \"light\") \"#4A4A44\" else \"#B8B7B0\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\nIMPRINT_PALETTE <- c(\n    \"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n    \"#AE3030\", \"#2ABCCD\", \"#954477\", \"#99B314\"\n)\n\n# --- Data -------------------------------------------------------------------\ndepartments <- c(\"Engineering\", \"Marketing\", \"Sales\", \"Design\", \"Research\")\ndept_means  <- c(95000, 72000, 78000, 82000, 105000)\ndept_sds    <- c(18000, 12000, 15000, 13000, 20000)\nn_per_dept  <- 80\n\ndept_data <- lapply(seq_along(departments), function(i) {\n    data.frame(\n        department = departments[i],\n        salary     = rnorm(n_per_dept, mean = dept_means[i], sd = dept_sds[i])\n    )\n})\n\ndf <- do.call(rbind, dept_data)\ndf$department <- factor(df$department, levels = departments)\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(df, aes(x = department, y = salary, fill = department)) +\n    # Individual salary points behind boxes — exposes distribution shape\n    geom_jitter(\n        aes(color = department),\n        width       = 0.18,\n        alpha       = 0.25,\n        size        = 0.9,\n        show.legend = FALSE\n    ) +\n    # Box plot overlaid on jitter; suppress default outliers (jitter shows all)\n    geom_boxplot(\n        color         = INK_SOFT,\n        linewidth     = 0.6,\n        outlier.shape = NA,\n        width         = 0.55,\n        alpha         = 0.85\n    ) +\n    # Diamond marker at the department mean — reveals mean/median divergence\n    stat_summary(\n        fun    = mean,\n        geom   = \"point\",\n        shape  = 23,\n        size   = 2.8,\n        fill   = PAGE_BG,\n        color  = INK_SOFT,\n        stroke = 0.8\n    ) +\n    scale_fill_manual(values = IMPRINT_PALETTE[1:5], guide = \"none\") +\n    scale_color_manual(values = IMPRINT_PALETTE[1:5], guide = \"none\") +\n    scale_y_continuous(\n        labels = label_dollar(scale = 0.001, suffix = \"k\"),\n        expand = expansion(mult = c(0.05, 0.08))\n    ) +\n    labs(\n        title    = \"box-basic · r · ggplot2 · anyplot.ai\",\n        subtitle = \"Research leads in both median pay and spread; ◆ marks the department mean\",\n        x        = \"Department\",\n        y        = \"Annual Salary (USD)\"\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.border       = element_blank(),\n        panel.grid.major.y = element_line(color = INK_SOFT, linewidth = 0.2),\n        panel.grid.major.x = element_blank(),\n        panel.grid.minor   = element_blank(),\n        axis.line.x        = element_line(color = INK_SOFT, linewidth = 0.4),\n        axis.line.y        = 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(\n            color  = INK,\n            size   = 12,\n            margin = margin(b = 2)\n        ),\n        plot.subtitle      = element_text(\n            color  = INK_MUTED,\n            size   = 8,\n            margin = margin(b = 10)\n        ),\n        plot.margin = margin(t = 16, r = 20, b = 16, l = 16)\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"}