{"spec_id":"polar-bar","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' polar-bar: Polar Bar Chart (Wind Rose)\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 90/100 | Created: 2026-09-05\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(tidyr)\nlibrary(scales)\nlibrary(ragg)\n\nset.seed(42)\n\n# Theme tokens (see prompts/default-style-guide.md \"Background\" + \"Theme-adaptive Chrome\")\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\"\n\n# Imprint categorical palette — first series is always brand green\nIMPRINT_PALETTE <- c(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\", \"#AE3030\")\n\n# Data — synthetic meteorological wind rose: 16 compass directions, 5 speed bins\ndirections <- c(\n  \"N\", \"NNE\", \"NE\", \"ENE\", \"E\", \"ESE\", \"SE\", \"SSE\",\n  \"S\", \"SSW\", \"SW\", \"WSW\", \"W\", \"WNW\", \"NW\", \"NNW\"\n)\nspeed_bins <- c(\"0-5 kt\", \"5-10 kt\", \"10-15 kt\", \"15-20 kt\", \"20+ kt\")\n\n# Prevailing westerlies: weight toward SW/W, light toward N/NE\ndirection_weights <- c(2, 3, 4, 5, 6, 5, 7, 9, 8, 10, 14, 12, 10, 7, 5, 3)\nwind_direction <- sample(directions,\n  size = 3000, replace = TRUE,\n  prob = direction_weights / sum(direction_weights)\n)\nwind_speed <- sample(speed_bins,\n  size = 3000, replace = TRUE,\n  prob = c(0.30, 0.27, 0.20, 0.14, 0.09)\n)\n\nwind_counts <- tibble(\n  direction = factor(wind_direction, levels = directions),\n  speed     = factor(wind_speed, levels = speed_bins)\n) %>%\n  count(direction, speed, name = \"n\")\n\nwind_df <- expand_grid(\n  direction = factor(directions, levels = directions),\n  speed     = factor(speed_bins, levels = speed_bins)\n) %>%\n  left_join(wind_counts, by = c(\"direction\", \"speed\")) %>%\n  mutate(\n    n = replace_na(n, 0),\n    frequency = n / sum(n) * 100\n  )\n\n# Plot — bars radiate from center, angle = direction, stacked by speed bin.\n# position_stack(reverse = TRUE) puts the largest bin (0-5 kt) innermost and\n# the thin high-speed bins outermost, where the bigger radius gives them more\n# circumference to render against — otherwise they collapse to invisible\n# slivers near the center.\np <- ggplot(wind_df, aes(x = direction, y = frequency, fill = speed)) +\n  geom_col(width = 1, color = PAGE_BG, linewidth = 0.3, position = position_stack(reverse = TRUE)) +\n  coord_polar(theta = \"x\", start = -pi / 16, clip = \"off\") +\n  scale_fill_manual(values = IMPRINT_PALETTE, name = \"Wind speed\") +\n  scale_y_continuous(labels = label_percent(scale = 1), expand = expansion(mult = c(0, 0.03))) +\n  labs(title = \"polar-bar · r · ggplot2 · anyplot.ai\", x = NULL, y = NULL) +\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  = element_line(color = INK_SOFT, linewidth = 0.2),\n    panel.grid.minor  = element_blank(),\n    panel.border      = element_blank(),\n    axis.line         = element_blank(),\n    axis.ticks        = element_blank(),\n    axis.text.x       = element_text(color = INK_SOFT, size = 9, margin = margin(t = 3)),\n    axis.text.y       = element_text(color = INK_SOFT, size = 7),\n    plot.title        = element_text(color = INK, size = 12, hjust = 0.5),\n    plot.margin       = margin(t = 10, r = 10, b = 6, l = 10),\n    legend.background = element_rect(fill = PAGE_BG, color = NA),\n    legend.text       = element_text(color = INK_SOFT, size = 8),\n    legend.title      = element_text(color = INK, size = 10),\n    legend.key.size   = unit(0.4, \"cm\"),\n    legend.margin     = margin(t = 0, b = 0),\n    legend.box.spacing = unit(2, \"pt\"),\n    legend.position   = \"bottom\"\n  )\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"}