{"spec_id":"coefficient-confidence","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' coefficient-confidence: Coefficient Plot with Confidence Intervals\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 91/100 | Created: 2026-05-18\n\nlibrary(ggplot2)\nlibrary(dplyr)\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   <- c(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                 \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# --- Data -------------------------------------------------------------------\n# Simulated housing price regression coefficients with confidence intervals\ncoefficients <- data.frame(\n  variable = c(\"Square Footage\", \"Bedrooms\", \"Bathrooms\", \"Age\",\n               \"Lot Size\", \"Garage Spaces\", \"Distance to School\",\n               \"Property Tax Rate\", \"Basement Area\", \"Year Built\"),\n  coefficient = c(0.85, 0.42, -0.18, -0.15, 0.28, 0.35, -0.52, -0.08, 0.22, 0.12),\n  ci_lower = c(0.72, 0.28, -0.35, -0.29, 0.15, 0.21, -0.68, -0.22, 0.08, -0.05),\n  ci_upper = c(0.98, 0.56, -0.01, -0.01, 0.41, 0.49, -0.36, 0.06, 0.36, 0.29),\n  significant = c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE)\n) %>%\n  # Order by coefficient magnitude (descending)\n  arrange(desc(abs(coefficient))) %>%\n  mutate(variable = factor(variable, levels = variable))\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(coefficients, aes(x = coefficient, y = variable,\n                              color = significant, fill = significant)) +\n  # Reference line at zero\n  geom_vline(xintercept = 0, linetype = \"solid\", color = INK_SOFT,\n             linewidth = 0.5, alpha = 0.5) +\n  # Confidence interval error bars\n  geom_errorbarh(aes(xmin = ci_lower, xmax = ci_upper),\n                 height = 0.3, linewidth = 1.2, alpha = 0.8) +\n  # Coefficient points\n  geom_point(size = 5, alpha = 0.9) +\n  # Color scale: significant vs non-significant\n  scale_color_manual(\n    name = \"Statistically Significant\",\n    values = c(\"TRUE\" = IMPRINT[1], \"FALSE\" = INK_SOFT),\n    labels = c(\"TRUE\" = \"Yes\", \"FALSE\" = \"No\")\n  ) +\n  scale_fill_manual(\n    name = \"Statistically Significant\",\n    values = c(\"TRUE\" = IMPRINT[1], \"FALSE\" = INK_SOFT),\n    labels = c(\"TRUE\" = \"Yes\", \"FALSE\" = \"No\")\n  ) +\n  labs(\n    title = \"coefficient-confidence · r · ggplot2 · anyplot.ai\",\n    x = \"Coefficient Estimate\",\n    y = \"Predictor Variable\"\n  ) +\n  theme_minimal(base_size = 14) +\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.2),\n    panel.grid.minor = element_blank(),\n    axis.title       = element_text(color = INK, size = 20),\n    axis.text        = element_text(color = INK_SOFT, size = 16),\n    axis.text.y      = element_text(color = INK_SOFT, size = 16),\n    plot.title       = element_text(color = INK, size = 24, face = \"plain\"),\n    legend.position  = \"bottom\",\n    legend.background = element_rect(fill = PAGE_BG, color = NA),\n    legend.text      = element_text(color = INK_SOFT, size = 16),\n    legend.title     = element_text(color = INK, size = 18)\n  )\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 16,\n  height   = 9,\n  units    = \"in\",\n  dpi      = 300\n)\n"}