{"spec_id":"bar-permutation-importance","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' bar-permutation-importance: Permutation Feature Importance Plot\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 94/100 | Created: 2026-05-17\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(tidyr)\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   <- c(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                 \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# --- Data -------------------------------------------------------------------\n# Simulate permutation importance from a machine learning model\n# 15 features with realistic importance scores and variability\nfeatures <- c(\n  \"Glucose\", \"Blood Pressure\", \"Skin Thickness\", \"Insulin\", \"BMI\",\n  \"Diabetes Pedigree\", \"Age\", \"Pregnancies\", \"Feature 9\", \"Feature 10\",\n  \"Feature 11\", \"Feature 12\", \"Feature 13\", \"Feature 14\", \"Feature 15\"\n)\n\nimportance_mean <- c(\n  0.085, 0.062, 0.041, 0.038, 0.127,\n  0.045, 0.093, 0.023, 0.018, 0.012,\n  0.009, 0.007, 0.005, 0.003, 0.001\n)\n\nimportance_std <- c(\n  0.012, 0.008, 0.006, 0.007, 0.015,\n  0.006, 0.011, 0.004, 0.003, 0.002,\n  0.002, 0.001, 0.001, 0.001, 0.0005\n)\n\ndf <- tibble::tibble(\n  feature = factor(features, levels = rev(features[order(importance_mean)])),\n  importance_mean = importance_mean,\n  importance_std = importance_std\n) %>%\n  arrange(desc(importance_mean))\n\n# --- Plot -------------------------------------------------------------------\nanyplot_theme <- 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.x = element_line(color = INK_SOFT, linewidth = 0.3, linetype = \"solid\"),\n    panel.grid.major.y = element_blank(),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_blank(),\n    axis.title        = element_text(color = INK, size = 20),\n    axis.text.x       = element_text(color = INK_SOFT, size = 16),\n    axis.text.y       = element_text(color = INK_SOFT, size = 16),\n    axis.line.x       = element_line(color = INK_SOFT, linewidth = 0.5),\n    axis.line.y       = element_blank(),\n    axis.ticks.y      = element_blank(),\n    plot.title        = element_text(color = INK, size = 24, face = \"plain\"),\n    plot.margin       = margin(t = 20, r = 20, b = 20, l = 20)\n  )\n\np <- ggplot(df, aes(x = importance_mean, y = reorder(feature, importance_mean))) +\n  # Vertical reference line at x=0\n  geom_vline(xintercept = 0, color = INK_SOFT, linewidth = 0.5, linetype = \"solid\") +\n  # Bars with color gradient based on importance\n  geom_col(\n    aes(fill = importance_mean),\n    width = 0.7,\n    color = NA\n  ) +\n  # Error bars showing variability\n  geom_errorbarh(\n    aes(xmin = importance_mean - importance_std,\n        xmax = importance_mean + importance_std),\n    height = 0.3,\n    color = INK_SOFT,\n    linewidth = 0.5,\n    alpha = 0.7\n  ) +\n  # Continuous color gradient for importance\n  scale_fill_gradient(\n    low = IMPRINT[1],\n    high = IMPRINT[2],\n    name = \"Mean Importance\",\n    labels = label_number(accuracy = 0.001)\n  ) +\n  labs(\n    x = \"Permutation Importance (decrease in model score)\",\n    y = \"Feature\",\n    title = \"bar-permutation-importance · ggplot2 · anyplot.ai\"\n  ) +\n  anyplot_theme +\n  theme(\n    legend.position = \"right\",\n    legend.background = element_rect(fill = ELEVATED_BG, color = INK_SOFT, linewidth = 0.5),\n    legend.text = element_text(color = INK_SOFT, size = 16),\n    legend.title = element_text(color = INK, size = 18),\n    legend.margin = margin(t = 10, r = 10, 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    = 16,\n  height   = 9,\n  units    = \"in\",\n  dpi      = 300\n)\n"}