{"spec_id":"contour-decision-boundary","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' contour-decision-boundary: Decision Boundary Classifier Visualization\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 88/100 | Created: 2026-09-04\n\nlibrary(ggplot2)\nlibrary(class)\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\"\n\n# Imprint palette — first 3 categorical slots (one per species)\nIMPRINT_PALETTE <- c(\"#009E73\", \"#C475FD\", \"#4467A3\")\n\n# --- Data ----------------------------------------------------------------\n# Iris petal measurements: a real, well-separated 3-class dataset for a\n# k-nearest-neighbors decision surface.\ntrain_df <- tibble::tibble(\n  petal_length = iris$Petal.Length,\n  petal_width  = iris$Petal.Width,\n  species      = iris$Species\n)\n\n# Dense mesh grid spanning the feature space, for the decision surface\ngrid_res <- 150\nx_range  <- range(train_df$petal_length)\ny_range  <- range(train_df$petal_width)\nx_pad    <- diff(x_range) * 0.08\ny_pad    <- diff(y_range) * 0.08\n\ngrid_df <- expand.grid(\n  petal_length = seq(x_range[1] - x_pad, x_range[2] + x_pad, length.out = grid_res),\n  petal_width  = seq(y_range[1] - y_pad, y_range[2] + y_pad, length.out = grid_res)\n)\n\nk_neighbors <- 9\ngrid_df$predicted <- class::knn(\n  train = train_df[, c(\"petal_length\", \"petal_width\")],\n  test  = grid_df[, c(\"petal_length\", \"petal_width\")],\n  cl    = train_df$species,\n  k     = k_neighbors\n)\n\n# Leave-one-out predictions on the training points flag misclassifications\ntrain_df$predicted <- class::knn.cv(\n  train = train_df[, c(\"petal_length\", \"petal_width\")],\n  cl    = train_df$species,\n  k     = k_neighbors\n)\ntrain_df$status <- ifelse(\n  train_df$predicted == train_df$species,\n  \"Correctly classified\",\n  \"Misclassified\"\n)\n\n# --- Plot ----------------------------------------------------------------\ntitle_text <- \"Iris Species by Petal Size · contour-decision-boundary · r · ggplot2 · anyplot.ai\"\n\np <- ggplot() +\n  geom_tile(\n    data = grid_df,\n    aes(x = petal_length, y = petal_width, fill = predicted),\n    alpha = 0.32\n  ) +\n  geom_point(\n    data = train_df,\n    aes(x = petal_length, y = petal_width, color = species, shape = status),\n    size = 2.5, stroke = 0.9\n  ) +\n  scale_fill_manual(values = IMPRINT_PALETTE, guide = \"none\") +\n  scale_color_manual(values = IMPRINT_PALETTE, name = \"Species\") +\n  scale_shape_manual(\n    values = c(\"Correctly classified\" = 16, \"Misclassified\" = 4),\n    name = \"Prediction\"\n  ) +\n  labs(\n    title = title_text,\n    x = \"Petal Length (cm)\",\n    y = \"Petal Width (cm)\"\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        = 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 = 10, face = \"bold\"),\n    legend.background = element_rect(fill = ELEVATED_BG, color = INK_SOFT),\n    legend.text       = element_text(color = INK_SOFT, size = 8),\n    legend.title      = element_text(color = INK, size = 10),\n    legend.key        = element_rect(fill = PAGE_BG, color = NA)\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"}