{"spec_id":"datamatrix-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' datamatrix-basic: Basic Data Matrix 2D Barcode\n#' Library: ggplot2 3.5.1 | R 4.4.1\n#' Quality: 90/100 | Created: 2026-05-20\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\"\n\n# Barcode module colors (data colors — constant across themes)\nCELL_DARK  <- \"#1A1A17\"\nCELL_LIGHT <- \"#FAF8F1\"\n\n# Data Matrix parameters\nN  <- 16L   # 16x16 symbol (ECC 200)\nQZ <- 2L    # quiet zone width in cells\n\n# Build barcode matrix (row 1 = top, col 1 = left)\nmat <- matrix(0L, nrow = N, ncol = N)\n\n# Interior: pseudo-encoded data cells\nmat[2:(N - 1), 2:(N - 1)] <- matrix(\n  sample(0:1, (N - 2L)^2, replace = TRUE),\n  nrow = N - 2L\n)\n\n# Finder pattern (L-shape): left column and bottom row — all dark\nmat[, 1L] <- 1L\nmat[N, ]  <- 1L\n\n# Timing patterns: top row (col 1 = dark) and right column (row N = dark)\nmat[1L, ] <- as.integer(seq_len(N) %% 2L == 1L)\nmat[, N]  <- as.integer(seq_len(N) %% 2L == N %% 2L)\n\n# Embed barcode in quiet zone\nN_total    <- N + 2L * QZ\npadded_mat <- matrix(0L, nrow = N_total, ncol = N_total)\npadded_mat[(QZ + 1L):(QZ + N), (QZ + 1L):(QZ + N)] <- mat\n\n# Tidy data frame (flip y so matrix row 1 appears at the top of the plot)\ndf <- expand.grid(col = seq_len(N_total), row = seq_len(N_total)) %>%\n  mutate(\n    value = padded_mat[cbind(row, col)],\n    x     = col,\n    y     = N_total + 1L - row\n  )\n\n# Plot\np <- ggplot(df, aes(x = x, y = y, fill = factor(value))) +\n  geom_tile(color = NA) +\n  scale_fill_manual(\n    values = c(\"0\" = CELL_LIGHT, \"1\" = CELL_DARK),\n    guide  = \"none\"\n  ) +\n  coord_fixed(ratio = 1) +\n  labs(\n    title    = \"datamatrix-basic · r · ggplot2 · anyplot.ai\",\n    subtitle = 'Content: \"MED-DEVICE:SN-2024-08751\"  |  16x16  |  ECC 200'\n  ) +\n  theme_void() +\n  theme(\n    plot.background = element_rect(fill = PAGE_BG, color = PAGE_BG),\n    plot.title      = element_text(\n      color  = INK,      size   = 12, face = \"bold\",\n      hjust  = 0.5,      margin = margin(b = 4)\n    ),\n    plot.subtitle   = element_text(\n      color  = INK_SOFT, size   = 9,\n      hjust  = 0.5,      margin = margin(b = 16)\n    ),\n    plot.margin     = margin(24, 24, 24, 24)\n  )\n\n# Save (square canvas: 6 in x 6 in x 400 dpi = 2400x2400 px)\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"}