{"spec_id":"bar-feature-importance","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' bar-feature-importance: Feature Importance Bar Chart\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 89/100 | Created: 2026-09-02\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\"\nINK_MUTED   <- if (THEME == \"light\") \"#6B6A63\" else \"#A8A79F\"\n\n# Imprint sequential colormap (single-polarity: brand green -> blue)\nIMPRINT_SEQ_LOW  <- \"#009E73\"\nIMPRINT_SEQ_HIGH <- \"#4467A3\"\n\n# --- Data -----------------------------------------------------------------\n# Feature importances from a gradient-boosting model predicting loan default\nfeatures <- c(\n  \"Credit Score\", \"Debt-to-Income Ratio\", \"Annual Income\",\n  \"Loan Amount\", \"Employment Length\", \"Payment History\",\n  \"Credit Utilization\", \"Number of Open Accounts\", \"Loan Purpose\",\n  \"Home Ownership\", \"Interest Rate\", \"Delinquencies (2yr)\",\n  \"Account Age\", \"Revolving Balance\", \"Inquiries (6mo)\"\n)\n\nn <- length(features)\nimportance_raw <- sort(rexp(n, rate = 3), decreasing = TRUE)\nimportance <- round(importance_raw / sum(importance_raw), 4)\nstd <- round(importance * runif(n, 0.10, 0.30), 4)\n\ndf <- tibble::tibble(feature = features, importance = importance, std = std) |>\n  arrange(importance) |>\n  mutate(feature = factor(feature, levels = feature))\n\n# --- Plot -------------------------------------------------------------------\ntitle_text <- \"bar-feature-importance · r · ggplot2 · anyplot.ai\"\n\np <- ggplot(df, aes(x = importance, y = feature, fill = importance)) +\n  geom_col(width = 0.68) +\n  geom_errorbar(\n    aes(xmin = importance - std, xmax = importance + std),\n    width = 0.28, color = INK_SOFT, linewidth = 0.5\n  ) +\n  geom_text(\n    aes(x = importance + std, label = sprintf(\"%.3f\", importance)),\n    hjust = -0.25, size = 3.0, color = INK, family = \"sans\"\n  ) +\n  scale_fill_gradient(low = IMPRINT_SEQ_LOW, high = IMPRINT_SEQ_HIGH, guide = \"none\") +\n  scale_x_continuous(\n    labels = scales::number_format(accuracy = 0.01),\n    expand = expansion(mult = c(0, 0.18))\n  ) +\n  labs(\n    title = title_text,\n    x = \"Feature Importance\",\n    y = NULL\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_line(color = INK_MUTED, linewidth = 0.2),\n    panel.grid.major.y = element_blank(),\n    panel.grid.minor   = element_blank(),\n    axis.title.x      = element_text(color = INK, size = 10, margin = margin(t = 8)),\n    axis.title.y      = element_blank(),\n    axis.text.x       = element_text(color = INK_SOFT, size = 8),\n    axis.text.y       = element_text(color = INK_SOFT, size = 9),\n    axis.ticks        = element_blank(),\n    plot.title        = element_text(color = INK, size = 12, margin = margin(b = 12)),\n    plot.margin       = margin(t = 14, r = 20, b = 10, l = 10)\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"}