{"spec_id":"hexbin-basic","library":"ggplot2","language":"r","code":"#' anyplot.ai\n#' hexbin-basic: Basic Hexbin Plot\n#' Library: ggplot2 4.0.3 | R 4.6.1\n#' Quality: 82/100 | Updated: 2026-11-27\n\nlibrary(ggplot2)\nlibrary(ragg)\n\n# Theme tokens (Imprint palette, theme-adaptive chrome)\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\"\nGRID_COLOR  <- if (THEME == \"light\") \"#1A1A1726\" else \"#F0EFE826\"\n\n# Imprint sequential ramp for fields (filled cells), stops per theme\nSEQ_FIELDS <- if (THEME == \"light\") {\n    list(colours = c(\"#66E2B2\", \"#009E73\", \"#024582\", \"#351E45\"), values = c(0, 0.369, 0.803, 1))\n} else {\n    list(colours = c(\"#404C02\", \"#009E73\", \"#CDD9FF\"), values = c(0, 0.487, 1))\n}\n\n# Columns: set these to your table's column names\nEASTING  <- \"Easting (km from city center)\"   # role: x\nNORTHING <- \"Northing (km from city center)\"  # role: y\nCOUNT    <- \"Count\"                           # role: legend title\n\n# Data: replace this block with your own table, for example df <- read.csv(\"gps.csv\", check.names = FALSE)\nset.seed(42)\ndf <- tibble::tibble(\n    !!EASTING := c(\n        rnorm(5000, mean =  0.4, sd = 1.1),\n        rnorm(3000, mean = -2.6, sd = 0.6),\n        rnorm(2000, mean =  3.0, sd = 0.8)\n    ),\n    !!NORTHING := c(\n        rnorm(5000, mean =  0.2, sd = 0.9),\n        rnorm(3000, mean =  2.3, sd = 0.5),\n        rnorm(2000, mean = -1.6, sd = 0.8)\n    )\n)\n\nplot_title <- \"hexbin-basic · r · ggplot2 · anyplot.ai\"\nn_chars    <- nchar(plot_title)\ntitle_fs   <- if (n_chars > 67) round(12 * 67 / n_chars) else 12\n\n# Hexagonal binning: 35 hexagons across the easting range (pointy-top, axial coordinates)\nBINS   <- 35\nx      <- df[[EASTING]]\ny      <- df[[NORTHING]]\nhex_r  <- diff(range(x)) / BINS / sqrt(3)\nfq     <- (sqrt(3) / 3 * x - y / 3) / hex_r\nfr     <- (2 / 3 * y) / hex_r\nfs     <- -fq - fr\nrq     <- round(fq)\nrr     <- round(fr)\nrs     <- round(fs)\ndq     <- abs(rq - fq)\ndr     <- abs(rr - fr)\nds     <- abs(rs - fs)\nfix_q  <- dq > dr & dq > ds\nfix_r  <- !fix_q & dr > ds\nrq     <- ifelse(fix_q, -rr - rs, rq)\nrr     <- ifelse(fix_r, -rq - rs, rr)\ncells  <- as.data.frame(table(q = rq, r = rr), responseName = \"n\")\ncells  <- cells[cells$n > 0, ]\ncells$q <- as.numeric(as.character(cells$q))\ncells$r <- as.numeric(as.character(cells$r))\nangles <- (30 + 60 * 0:5) * pi / 180\nhexes  <- data.frame(\n    cell = rep(seq_len(nrow(cells)), each = 6),\n    px   = rep(hex_r * sqrt(3) * (cells$q + cells$r / 2), each = 6) + hex_r * cos(angles),\n    py   = rep(hex_r * 1.5 * cells$r, each = 6) + hex_r * sin(angles),\n    n    = rep(cells$n, each = 6)\n)\n\n# Plot\np <- ggplot(hexes, aes(x = px, y = py, group = cell, fill = n)) +\n    geom_polygon(color = NA) +\n    scale_fill_gradientn(\n        colours = SEQ_FIELDS$colours,\n        values  = SEQ_FIELDS$values,\n        guide   = guide_colorbar(\n            barwidth  = unit(0.6, \"cm\"),\n            barheight = unit(4.5, \"cm\"),\n            ticks     = FALSE\n        )\n    ) +\n    labs(title = plot_title, x = EASTING, y = NORTHING, fill = COUNT) +\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        panel.grid.major  = element_line(color = GRID_COLOR, linewidth = 0.4),\n        panel.grid.minor  = element_blank(),\n        axis.title        = element_text(color = INK, size = 10),\n        axis.text         = element_text(color = INK_SOFT, size = 8),\n        axis.line         = element_line(color = INK_SOFT, linewidth = 0.4),\n        axis.ticks        = element_blank(),\n        plot.title        = element_text(\n            color  = INK,\n            size   = title_fs,\n            face   = \"bold\",\n            margin = margin(b = 8)\n        ),\n        legend.background = element_rect(fill = ELEVATED_BG, color = NA),\n        legend.text       = element_text(color = INK_SOFT, size = 8),\n        legend.title      = element_text(color = INK, size = 10),\n        plot.margin       = margin(12, 15, 12, 12, \"pt\")\n    )\n\n# Save\nggsave(\n    filename = sprintf(\"plot-%s.png\", THEME),\n    plot     = p,\n    device   = ragg::agg_png,\n    width    = 8,\n    height   = 4.5,\n    units    = \"in\",\n    dpi      = 400\n)\n"}