{"spec_id":"tree-decision","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' tree-decision: Decision Tree Visualization with Probabilities\n#' Library: ggplot2 4.0.3 | R 4.6.1\n#' Quality: 85/100 | Updated: 2026-12-04\n\nlibrary(ggplot2)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens (Imprint palette)\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\"\n\n# Imprint palette positions used\nCOL_DEC  <- \"#009E73\"  # position 1 — decision nodes (brand green)\nCOL_CHC  <- \"#C475FD\"  # position 2 — chance nodes (violet)\nCOL_TERM <- \"#4467A3\"  # position 3 — terminal nodes (blue)\nCHC_INK  <- \"#1A1A17\"  # dark text on the light violet chance nodes, both themes\n\n# Tech startup product launch decision tree\n# Stage 1: Launch vs. Skip (Skip = license the idea out, partner interest uncertain)\n# Stage 2 (if Launch): Market Strong (p=0.6) vs. Weak (p=0.4)\n# Stage 3 (if Strong): Expand vs. Maintain (Maintain faces steady or slumping demand)\n#\n# EMV rollback (in $K):\n#   EMV(C1) = 0.7 * 150 + 0.3 * 90  = 132\n#   EMV(D1) = max(Expand 300, Maintain 132) = 300  -> Expand wins\n#   EMV(C0) = 0.6 * 300 + 0.4 * 20  = 188\n#   EMV(C2) = 0.5 * 60 + 0.5 * 0    = 30\n#   EMV(D0) = max(Launch 188, Skip 30) = 188        -> Launch wins\n\nnodes <- data.frame(\n  id     = c(\"D0\",  \"C0\",  \"D1\",  \"C1\",  \"C2\",  \"T1\",  \"T2\",  \"T3\",  \"T4\",  \"T5\",  \"T6\"),\n  type   = c(\"dec\", \"chc\", \"dec\", \"chc\", \"chc\", \"term\",\"term\",\"term\",\"term\",\"term\",\"term\"),\n  x      = c(1.0,   2.9,   4.8,   6.7,   2.9,   6.7,   8.6,   8.6,   4.8,   4.8,   4.8),\n  y      = c(-0.1,  0.8,   1.5,   0.9,  -0.9,   2.2,   1.3,   0.5,   0.0,  -0.5,  -1.3),\n  payoff = c(NA,    NA,    NA,    NA,    NA,    300,   150,   90,    20,    60,    0),\n  pruned = c(FALSE, FALSE, FALSE, TRUE,  TRUE,  FALSE, TRUE,  TRUE,  FALSE, TRUE,  TRUE),\n  stringsAsFactors = FALSE\n)\n\nedges <- data.frame(\n  from   = c(\"D0\",     \"D0\",   \"C0\",           \"C0\",          \"D1\",     \"D1\",\n             \"C1\",             \"C1\",             \"C2\",             \"C2\"),\n  to     = c(\"C0\",     \"C2\",   \"D1\",           \"T4\",          \"T1\",     \"C1\",\n             \"T2\",             \"T3\",             \"T5\",             \"T6\"),\n  label  = c(\"Launch\", \"Skip\", \"p=0.6\\nStrong\", \"p=0.4\\nWeak\", \"Expand\", \"Maintain\",\n             \"p=0.7\\nSteady\",   \"p=0.3\\nSlump\",    \"p=0.5\\nPartner\",  \"p=0.5\\nNone\"),\n  prob   = c(NA, NA, 0.6, 0.4, NA, NA, 0.7, 0.3, 0.5, 0.5),\n  pruned = c(FALSE, TRUE,  FALSE,          FALSE,         FALSE,    TRUE,\n             TRUE,             TRUE,             TRUE,             TRUE),\n  mark   = c(FALSE, TRUE,  FALSE,          FALSE,         FALSE,    TRUE,\n             FALSE,            FALSE,            FALSE,            FALSE),\n  stringsAsFactors = FALSE\n)\n\n# EMV of each chance node from its branches, shown inside the node\n# (decision nodes take the best child: D1 = max(T1, C1), D0 = max(C0, C2))\nemv <- c(\n  C1 = sum(c(150, 90) * c(0.7, 0.3)),\n  C2 = sum(c(60, 0)   * c(0.5, 0.5))\n)\nemv[\"C0\"] <- 0.6 * 300 + 0.4 * 20\nemv[\"D1\"] <- max(300, emv[[\"C1\"]])\nemv[\"D0\"] <- max(emv[[\"C0\"]], emv[[\"C2\"]])\nnodes$emv_lbl <- ifelse(nodes$id %in% names(emv), paste0(\"$\", round(emv[nodes$id]), \"K\"), NA)\n\n# Attach node coordinates to edges\nfi       <- match(edges$from, nodes$id)\nti       <- match(edges$to,   nodes$id)\nedges$x0 <- nodes$x[fi];  edges$y0 <- nodes$y[fi]\nedges$x1 <- nodes$x[ti];  edges$y1 <- nodes$y[ti]\n\n# Branch label at 38% from source; offset above/below the line depending on slope\nedges$lx <- edges$x0 + 0.38 * (edges$x1 - edges$x0)\nedges$ly <- edges$y0 + 0.38 * (edges$y1 - edges$y0)\nedges$dy <- ifelse(edges$y1 >= edges$y0, 0.27, -0.27)\n\n# Pruned-mark position at 70% from source\nedges$px <- edges$x0 + 0.70 * (edges$x1 - edges$x0)\nedges$py <- edges$y0 + 0.70 * (edges$y1 - edges$y0)\n\n# Right-pointing triangle polygons for terminal nodes\nhw <- 0.14\nterm_rows <- which(nodes$type == \"term\")\ntri_frames <- lapply(seq_along(term_rows), function(i) {\n  ni <- term_rows[i]\n  data.frame(\n    x      = c(nodes$x[ni] - hw, nodes$x[ni] - hw, nodes$x[ni] + hw),\n    y      = c(nodes$y[ni] - hw * 0.85, nodes$y[ni] + hw * 0.85, nodes$y[ni]),\n    pruned = nodes$pruned[ni],\n    grp    = i\n  )\n})\ntri_data <- do.call(rbind, tri_frames)\n\n# Subsets for conditional styling\ne_live  <- edges[!edges$pruned, ]\ne_dead  <- edges[edges$pruned, ]\ne_mark  <- edges[edges$mark, ]\nt_live  <- tri_data[!tri_data$pruned, ]\nt_dead  <- tri_data[tri_data$pruned, ]\nn_dec   <- nodes[nodes$type == \"dec\", ]\nn_chc   <- nodes[nodes$type == \"chc\" & !nodes$pruned, ]\nn_chcd  <- nodes[nodes$type == \"chc\" &  nodes$pruned, ]\nn_emv   <- nodes[!is.na(nodes$emv_lbl) & nodes$type == \"dec\", ]\nn_emvc  <- nodes[!is.na(nodes$emv_lbl) & nodes$type == \"chc\" & !nodes$pruned, ]\nn_emvcd <- nodes[!is.na(nodes$emv_lbl) & nodes$type == \"chc\" &  nodes$pruned, ]\nn_paylv <- nodes[nodes$type == \"term\" & !nodes$pruned, ]\nn_paydt <- nodes[nodes$type == \"term\" &  nodes$pruned, ]\n\nKEY_Y <- -1.85\n\n# Faded fills for pruned nodes: opaque blends toward the page so edges never show through\nfade <- function(col, amount) {\n  rgb(t(col2rgb(col) * (1 - amount) + col2rgb(PAGE_BG) * amount), maxColorValue = 255)\n}\nFADE_CHC  <- fade(COL_CHC, 0.65)\nFADE_TERM <- fade(COL_TERM, 0.70)\n\np <- ggplot() +\n  # Edges: active\n  geom_segment(\n    data = e_live,\n    aes(x = x0, y = y0, xend = x1, yend = y1),\n    color = INK_SOFT, linewidth = 1.0\n  ) +\n  # Edges: pruned (dashed, muted)\n  geom_segment(\n    data = e_dead,\n    aes(x = x0, y = y0, xend = x1, yend = y1),\n    color = INK_MUTED, linewidth = 0.5, linetype = \"dashed\", alpha = 0.55\n  ) +\n  # Terminal triangles: active\n  geom_polygon(\n    data = t_live,\n    aes(x = x, y = y, group = grp),\n    fill = COL_TERM, color = INK_SOFT\n  ) +\n  # Terminal triangles: pruned (faded)\n  geom_polygon(\n    data = t_dead,\n    aes(x = x, y = y, group = grp),\n    fill = FADE_TERM, color = INK_MUTED\n  ) +\n  # Chance nodes (circles): active and pruned\n  geom_point(\n    data = n_chc,\n    aes(x = x, y = y),\n    shape = 21, size = 14.5, fill = COL_CHC, color = INK_SOFT, stroke = 0.5\n  ) +\n  geom_point(\n    data = n_chcd,\n    aes(x = x, y = y),\n    shape = 21, size = 14.5, fill = FADE_CHC, color = INK_MUTED, stroke = 0.5\n  ) +\n  # Decision nodes (squares)\n  geom_point(\n    data = n_dec,\n    aes(x = x, y = y),\n    shape = 22, size = 14.5, fill = COL_DEC, color = INK_SOFT, stroke = 0.5\n  ) +\n  # EMV values inside decision/chance nodes\n  geom_text(\n    data = n_emv,\n    aes(x = x, y = y, label = emv_lbl),\n    color = \"white\", size = 2.7, fontface = \"bold\"\n  ) +\n  geom_text(\n    data = n_emvc,\n    aes(x = x, y = y, label = emv_lbl),\n    color = CHC_INK, size = 2.7, fontface = \"bold\"\n  ) +\n  geom_text(\n    data = n_emvcd,\n    aes(x = x, y = y, label = emv_lbl),\n    color = INK_SOFT, size = 2.7, fontface = \"bold\"\n  ) +\n  # Branch labels: active\n  geom_label(\n    data = e_live,\n    aes(x = lx, y = ly + dy, label = label),\n    color = INK_SOFT, fill = ELEVATED_BG, size = 2.9, lineheight = 0.85,\n    label.padding = unit(0.13, \"lines\"), linewidth = 0\n  ) +\n  # Branch labels: pruned\n  geom_label(\n    data = e_dead,\n    aes(x = lx, y = ly + dy, label = label),\n    color = INK_MUTED, fill = ELEVATED_BG, size = 2.9, lineheight = 0.85,\n    label.padding = unit(0.13, \"lines\"), linewidth = 0\n  ) +\n  # Double-strike pruned mark on the rejected options\n  geom_text(\n    data = e_mark,\n    aes(x = px, y = py),\n    label = \"//\", color = INK_MUTED, size = 3.8\n  ) +\n  # Payoff labels: active terminals (right of triangle)\n  geom_text(\n    data = n_paylv,\n    aes(x = x + hw + 0.09, y = y, label = paste0(\"$\", payoff, \"K\")),\n    color = INK, size = 3.4, fontface = \"bold\", hjust = 0\n  ) +\n  # Payoff labels: pruned terminals\n  geom_text(\n    data = n_paydt,\n    aes(x = x + hw + 0.09, y = y, label = paste0(\"$\", payoff, \"K\")),\n    color = INK_MUTED, size = 3.2, hjust = 0, alpha = 0.6\n  ) +\n  # Key: Decision node\n  annotate(\"point\", x = 1.0, y = KEY_Y, shape = 22, size = 5,\n           fill = COL_DEC, color = INK_SOFT) +\n  annotate(\"text\", x = 1.18, y = KEY_Y, label = \"Decision\",\n           hjust = 0, size = 3.0, color = INK_SOFT) +\n  # Key: Chance node\n  annotate(\"point\", x = 2.75, y = KEY_Y, shape = 21, size = 5,\n           fill = COL_CHC, color = INK_SOFT) +\n  annotate(\"text\", x = 2.93, y = KEY_Y, label = \"Chance\",\n           hjust = 0, size = 3.0, color = INK_SOFT) +\n  # Key: Terminal node (small right-pointing triangle)\n  annotate(\"polygon\",\n           x = c(4.35 - 0.07, 4.35 - 0.07, 4.35 + 0.07),\n           y = c(KEY_Y - 0.06, KEY_Y + 0.06, KEY_Y),\n           fill = COL_TERM, color = INK_SOFT) +\n  annotate(\"text\", x = 4.50, y = KEY_Y, label = \"Terminal\",\n           hjust = 0, size = 3.0, color = INK_SOFT) +\n  # Key: pruned (rejected) branch, dashed with the double strike\n  annotate(\"segment\", x = 5.95, xend = 6.55, y = KEY_Y, yend = KEY_Y,\n           color = INK_MUTED, linewidth = 0.5, linetype = \"dashed\", alpha = 0.7) +\n  annotate(\"text\", x = 6.25, y = KEY_Y, label = \"//\", color = INK_MUTED, size = 3.8) +\n  annotate(\"text\", x = 6.70, y = KEY_Y, label = \"Pruned (rejected)\",\n           hjust = 0, size = 3.0, color = INK_SOFT) +\n  coord_cartesian(xlim = c(0.35, 9.9), ylim = c(-2.15, 2.65), expand = FALSE) +\n  labs(title = \"tree-decision · r · ggplot2 · anyplot.ai\") +\n  theme_void() +\n  theme(\n    plot.background  = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    panel.background = element_rect(fill = PAGE_BG, color = NA),\n    plot.title       = element_text(\n      color = INK, size = 12, hjust = 0.5,\n      margin = margin(t = 14, b = 8)\n    ),\n    plot.margin = margin(t = 5, r = 40, b = 20, l = 10)\n  )\n\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"}