{"spec_id":"radar-multi","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' radar-multi: Multi-Series Radar Chart\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 91/100 | Updated: 2026-08-20\n\nlibrary(ggplot2)\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\nIMPRINT_PALETTE <- c(\n  \"#009E73\", # 1 — brand green, always first series\n  \"#C475FD\", # 2 — lavender\n  \"#4467A3\", # 3 — blue\n  \"#BD8233\", # 4 — ochre\n  \"#AE3030\", # 5 — matte red\n  \"#2ABCCD\", # 6 — cyan\n  \"#954477\", # 7 — rose\n  \"#99B314\"  # 8 — lime\n)\n\n# coord_polar() munges straight polygon edges into arcs when interpolating\n# between vertices; is_linear = TRUE keeps the radar spokes and value rings\n# as straight-edged polygons, matching the spec's \"closed polygon\" per axis.\n# clip = \"off\" lets the manually-placed category labels below sit outside\n# the r=100 ring without being clipped by the panel boundary.\ncoord_radar <- function(start = 0, direction = 1, clip = \"off\") {\n  ggproto(\"CoordRadar\", CoordPolar,\n    theta = \"x\", r = \"y\", start = start, direction = sign(direction),\n    is_linear = function(coord) TRUE,\n    clip = clip\n  )\n}\n\n# --- Data -----------------------------------------------------------------\nattributes <- c(\"Battery Life\", \"Camera\", \"Performance\", \"Display\", \"Value\", \"Durability\")\n\nradar_df <- tibble::tibble(\n  phone = rep(c(\"Aurora X12\", \"Nimbus S8\", \"Ridgeline Pro\"), each = length(attributes)),\n  attribute = factor(rep(attributes, times = 3), levels = attributes),\n  score = c(\n    72, 65, 88, 80, 55, 70,   # Aurora X12   — performance-focused flagship\n    90, 78, 60, 68, 82, 75,   # Nimbus S8    — battery + value pick\n    58, 92, 75, 95, 45, 85    # Ridgeline Pro — camera + display, premium price\n  )\n)\n\n# Alternating ring bands (drawn back-to-front: wide band first, then a\n# narrower band painted in the page color to punch out the \"gap\") give the\n# grid a touch of depth beyond the mandated gridlines alone. Strengthened\n# from alpha=0.09 (still barely perceptible) to 0.16 for a visible band.\nring_hi <- data.frame(attribute = factor(attributes, levels = attributes), score = 80)\nring_lo <- data.frame(attribute = factor(attributes, levels = attributes), score = 60)\n\n# Category labels are drawn manually (rather than via the default polar\n# axis.text.x, which ggplot2 always renders exactly at the panel's outer\n# boundary) at score = 116 — clear of the r=100 gridline. The default\n# axis.text.x placement previously made the boundary ring/spoke lines cut\n# straight through 3 of the 6 labels (\"Camera\", \"Value\", \"Durability\").\nlabel_df <- data.frame(attribute = factor(attributes, levels = attributes), score = 116)\n\n# Each phone's single strongest attribute, ringed with an open halo below,\n# reinforces the caption's \"each phone leads on a different axis\" claim\n# directly in the encoding — not just as caption text.\nleader_idx <- tapply(seq_len(nrow(radar_df)), radar_df$phone, function(idx) idx[which.max(radar_df$score[idx])])\nleader_df <- radar_df[unlist(leader_idx), ]\n\n# --- Plot -------------------------------------------------------------------\np <- ggplot(radar_df, aes(x = attribute, y = score, group = phone, color = phone, fill = phone)) +\n  geom_polygon(data = ring_hi, aes(x = attribute, y = score), inherit.aes = FALSE, fill = INK, alpha = 0.16) +\n  geom_polygon(data = ring_lo, aes(x = attribute, y = score), inherit.aes = FALSE, fill = PAGE_BG) +\n  geom_polygon(alpha = 0.25, linewidth = 1.0) +\n  geom_point(size = 3.8) +\n  geom_point(\n    data = leader_df, aes(x = attribute, y = score, color = phone),\n    inherit.aes = FALSE, shape = 1, size = 7.5, stroke = 1.6, show.legend = FALSE\n  ) +\n  geom_text(\n    data = label_df, aes(x = attribute, y = score, label = attribute),\n    inherit.aes = FALSE, color = INK, size = 10 / .pt\n  ) +\n  coord_radar(start = -pi / 2) +\n  # limits stay open-ended (NA) so the panel's true range grows to fit the\n  # score = 116 label layer above; breaks stop at 100 so no extra gridline\n  # is drawn out there — only the mandated 20/40/60/80/100 rings render.\n  scale_y_continuous(limits = c(0, NA), breaks = seq(0, 100, 20), expand = expansion(mult = c(0, 0.06))) +\n  scale_color_manual(values = IMPRINT_PALETTE) +\n  scale_fill_manual(values = IMPRINT_PALETTE) +\n  labs(\n    title = \"radar-multi · r · ggplot2 · anyplot.ai\",\n    caption = \"Each phone leads on a different axis — no single winner across all six attributes\",\n    x = NULL, y = \"Score (0-100)\", color = NULL, fill = 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.border       = element_blank(),\n    axis.line          = element_blank(),\n    axis.ticks         = element_blank(),\n    panel.grid.major   = element_line(color = INK, linewidth = 0.3),\n    panel.grid.minor   = element_blank(),\n    # category labels are drawn via geom_text (label_df) instead, so the\n    # built-in polar axis text — always pinned to the panel's outer edge — is\n    # suppressed here to avoid a second, overlapping set of labels.\n    axis.text.x        = element_blank(),\n    axis.text.y        = element_text(color = INK_SOFT, size = 8),\n    axis.title.y       = element_text(color = INK_SOFT, size = 7, hjust = 0.85, margin = margin(r = 4)),\n    plot.title         = element_text(color = INK, size = 12, face = \"bold\", hjust = 0.5),\n    plot.caption       = element_text(color = INK_SOFT, size = 7, hjust = 0.5, margin = margin(t = 8)),\n    legend.position    = \"bottom\",\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_blank()\n  ) +\n  guides(fill = guide_legend(override.aes = list(alpha = 0.5)))\n\n# --- Save -------------------------------------------------------------------\nggsave(\n  filename = sprintf(\"plot-%s.png\", THEME),\n  plot     = p,\n  device   = ragg::agg_png,\n  width    = 6,\n  height   = 6,\n  units    = \"in\",\n  dpi      = 400\n)\n"}