{"spec_id":"chessboard-pieces","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' chessboard-pieces: Chess Board with Pieces for Position Diagrams\n#' Library: ggplot2 4.0.3 | R 4.6.1\n#' Quality: 82/100 | Updated: 2026-11-17\n\nlibrary(ggplot2)\nlibrary(dplyr)\nlibrary(ragg)\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\"\n\n# Board and piece colors are data colors: identical in both themes\nLIGHT_SQUARE <- \"#F0D9B5\"\nDARK_SQUARE  <- \"#A8794F\"\nWHITE_PIECE  <- \"#FFFFFF\"\nBLACK_PIECE  <- \"#1A1A17\"\n\n# Columns: set these to your table's column names\nSQUARE <- \"square\"  # role: category (algebraic notation, e.g. e4)\nPIECE  <- \"piece\"   # role: category (K/Q/R/B/N/P white, k/q/r/b/n/p black)\n\n# Data: replace this block with your own position, e.g. Scholar's Mate or a puzzle\nset.seed(42)\npieces <- c(\n  a1 = \"R\", b1 = \"N\", c1 = \"B\", d1 = \"Q\", e1 = \"K\", f1 = \"B\", g1 = \"N\", h1 = \"R\",\n  a2 = \"P\", b2 = \"P\", c2 = \"P\", d2 = \"P\", e2 = \"P\", f2 = \"P\", g2 = \"P\", h2 = \"P\",\n  a7 = \"p\", b7 = \"p\", c7 = \"p\", d7 = \"p\", e7 = \"p\", f7 = \"p\", g7 = \"p\", h7 = \"p\",\n  a8 = \"r\", b8 = \"n\", c8 = \"b\", d8 = \"q\", e8 = \"k\", f8 = \"b\", g8 = \"n\", h8 = \"r\"\n)\ndf <- tibble::tibble(\n  !!SQUARE := names(pieces),\n  !!PIECE  := unname(pieces)\n)\n\n# Plot: reads only df and the constants above\nfiles <- letters[1:8]\nboard <- expand.grid(col = 1:8, row = 1:8) |>\n  mutate(light = (col + row) %% 2 == 1)  # h1 (col 8, row 1) is a light square\n\n# Solid glyphs (U+265A-265F) carry the piece color; outline glyphs (U+2654-2659)\n# drawn on top give white pieces a dark contour on light squares\nsolid_glyph   <- c(k = \"♚\", q = \"♛\", r = \"♜\", b = \"♝\", n = \"♞\", p = \"♟\")\noutline_glyph <- c(k = \"♔\", q = \"♕\", r = \"♖\", b = \"♗\", n = \"♘\", p = \"♙\")\n\nplaced <- df |>\n  mutate(\n    col   = match(substr(.data[[SQUARE]], 1, 1), files),\n    row   = as.integer(substr(.data[[SQUARE]], 2, 2)),\n    kind  = tolower(.data[[PIECE]]),\n    white = .data[[PIECE]] != kind,\n    solid = solid_glyph[kind],\n    ink   = ifelse(white, WHITE_PIECE, BLACK_PIECE)\n  )\n\nPIECE_SIZE <- 11  # geom_text size in mm: about 70% of a square\n\np <- ggplot() +\n  geom_tile(data = board, aes(x = col, y = row, fill = light), color = NA) +\n  scale_fill_manual(values = c(`TRUE` = LIGHT_SQUARE, `FALSE` = DARK_SQUARE), guide = \"none\") +\n  geom_text(data = placed, aes(x = col, y = row, label = solid, color = ink),\n            size = PIECE_SIZE, family = \"DejaVu Sans\") +\n  geom_text(data = filter(placed, white), aes(x = col, y = row, label = outline_glyph[kind]),\n            size = PIECE_SIZE, family = \"DejaVu Sans\", color = BLACK_PIECE) +\n  scale_color_identity() +\n  scale_x_continuous(breaks = 1:8, labels = files, expand = c(0, 0)) +\n  scale_y_continuous(breaks = 1:8, labels = as.character(1:8), expand = c(0, 0)) +\n  coord_fixed(xlim = c(0.5, 8.5), ylim = c(0.5, 8.5)) +\n  labs(title = \"chessboard-pieces · 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       = element_blank(),\n    axis.text        = element_text(color = INK_SOFT, size = 12),\n    axis.ticks       = element_blank(),\n    plot.title       = element_text(color = INK, size = 13, face = \"bold\", hjust = 0.5),\n    plot.margin      = margin(12, 12, 12, 12)\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"}