{"spec_id":"chessboard-pieces","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' chessboard-pieces: Chess Board with Pieces for Position Diagrams\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 86/100 | Created: 2026-05-17\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(ragg)\n\nset.seed(42)\n\n# --- Theme tokens -----------------------------------------------------------\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\"\nIMPRINT   <- c(\"#009E73\", \"#C475FD\", \"#4467A3\", \"#BD8233\",\n                 \"#AE3030\", \"#2ABCCD\", \"#954477\")\n\n# --- Data: Chess board setup ------------------------------------------------\n\n# Create board squares (8x8 grid)\nboard <- expand.grid(\n  col = 0:7,\n  row = 0:7\n) %>%\n  mutate(\n    square_color = if_else((col + row) %% 2 == 0, \"light\", \"dark\"),\n    piece = NA_character_,\n    piece_color = NA_character_\n  )\n\n# Define piece positions for a sample opening position\n# Using algebraic notation: 'a1' = bottom-left, 'h8' = top-right\npositions <- data.frame(\n  square = c(\n    # White pieces\n    \"a1\", \"h1\", \"b1\", \"g1\", \"c1\", \"f1\", \"d1\", \"e1\",  # back rank\n    \"a2\", \"b2\", \"c2\", \"d2\", \"e2\", \"f2\", \"g2\", \"h2\",  # pawns\n    # Black pieces\n    \"a8\", \"h8\", \"b8\", \"g8\", \"c8\", \"f8\", \"d8\", \"e8\",  # back rank\n    \"a7\", \"b7\", \"c7\", \"d7\", \"e7\", \"f7\", \"g7\", \"h7\"   # pawns\n  ),\n  piece = c(\n    # White\n    \"R\", \"R\", \"N\", \"N\", \"B\", \"B\", \"Q\", \"K\",\n    \"P\", \"P\", \"P\", \"P\", \"P\", \"P\", \"P\", \"P\",\n    # Black\n    \"r\", \"r\", \"n\", \"n\", \"b\", \"b\", \"q\", \"k\",\n    \"p\", \"p\", \"p\", \"p\", \"p\", \"p\", \"p\", \"p\"\n  )\n)\n\n# Convert algebraic notation to grid coordinates\nnotation_to_grid <- function(notation) {\n  col <- match(substr(notation, 1, 1), letters[1:8]) - 1\n  row <- as.numeric(substr(notation, 2, 2)) - 1\n  data.frame(col = col, row = row)\n}\n\n# Add pieces to board\nfor (i in seq_len(nrow(positions))) {\n  coords <- notation_to_grid(positions$square[i])\n  piece_char <- positions$piece[i]\n  board$piece[board$col == coords$col & board$row == coords$row] <- piece_char\n  board$piece_color[board$col == coords$col & board$row == coords$row] <-\n    if (piece_char %in% c(\"K\", \"Q\", \"R\", \"B\", \"N\", \"P\")) \"white\" else \"black\"\n}\n\n# --- Plot: Chess board visualization ----------------------------------------\n\n# Board square colors (chess standard: light squares, dark squares)\nlight_square <- \"#F5E6D3\"\ndark_square  <- \"#8B7355\"\n\n# Piece text color (contrasts with board)\npiece_text_color <- \"#2C2C2C\"\n\np <- ggplot(board, aes(x = col, y = row)) +\n  # Draw board squares\n  geom_tile(aes(fill = square_color), color = NA) +\n  scale_fill_manual(values = c(\"light\" = light_square, \"dark\" = dark_square),\n                    guide = \"none\") +\n\n  # Draw pieces as text annotations\n  geom_text(aes(label = piece, color = piece_color),\n            size = 18,\n            vjust = 0.5, hjust = 0.5,\n            family = \"DejaVu Sans\") +\n  scale_color_manual(\n    values = c(\"white\" = \"#F5F5F5\", \"black\" = \"#1A1A1A\"),\n    na.value = NA,\n    guide = \"none\"\n  ) +\n\n  # Labels and title\n  labs(\n    title = \"chessboard-pieces · ggplot2 · anyplot.ai\",\n    x = \"\",\n    y = \"\"\n  ) +\n\n  # Coordinate settings\n  scale_x_continuous(\n    breaks = 0:7,\n    labels = c(\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\"),\n    expand = c(0, 0)\n  ) +\n  scale_y_continuous(\n    breaks = 0:7,\n    labels = c(\"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\"),\n    expand = c(0, 0)\n  ) +\n  coord_fixed(ratio = 1) +\n\n  # Theme styling\n  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        = element_blank(),\n    axis.title        = element_text(color = INK, size = 20),\n    axis.text         = element_text(color = INK_SOFT, size = 16),\n    plot.title        = element_text(color = INK, size = 24, face = \"bold\"),\n    axis.ticks        = element_blank(),\n    plot.margin       = unit(c(1, 1, 1, 1), \"cm\")\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"}