A hexagonal binning plot that visualizes the density of 2D point data by aggregating points into hexagonal bins. The color intensity of each hexagon represents the count of points within it, making it ideal for revealing density patterns in large datasets where traditional scatter plots would show overlapping points. Hexagonal bins provide better visual representation than square pixels due to their isotropy (equal distance to neighboring cells in all directions).

#' anyplot.ai
#' hexbin-basic: Basic Hexbin Plot
#' Library: ggplot2 3.5.1 | R 4.4.1
#' Quality: 91/100 | Created: 2026-05-29
library(ggplot2)
library(ragg)
set.seed(42)
# Theme tokens (Imprint palette, theme-adaptive chrome)
THEME <- Sys.getenv("ANYPLOT_THEME", "light")
PAGE_BG <- if (THEME == "light") "#FAF8F1" else "#1A1A17"
ELEVATED_BG <- if (THEME == "light") "#FFFDF6" else "#242420"
INK <- if (THEME == "light") "#1A1A17" else "#F0EFE8"
INK_SOFT <- if (THEME == "light") "#4A4A44" else "#B8B7B0"
GRID_COLOR <- if (THEME == "light") "#1A1A1726" else "#F0EFE826"
# Data — simulated GPS mobility data: three urban activity clusters
x <- c(
rnorm(5000, mean = 0.4, sd = 1.1), # downtown core
rnorm(3000, mean = -2.6, sd = 0.6), # transit hub
rnorm(2000, mean = 3.0, sd = 0.8) # university campus
)
y <- c(
rnorm(5000, mean = 0.2, sd = 0.9), # downtown core
rnorm(3000, mean = 2.3, sd = 0.5), # transit hub
rnorm(2000, mean = -1.6, sd = 0.8) # university campus
)
df <- data.frame(
easting = x,
northing = y
)
plot_title <- "hexbin-basic · r · ggplot2 · anyplot.ai"
n_chars <- nchar(plot_title)
title_fs <- if (n_chars > 67) round(12 * 67 / n_chars) else 12
# Plot
p <- ggplot(df, aes(x = easting, y = northing)) +
geom_hex(bins = 35, color = NA) +
scale_fill_gradient(
low = "#009E73",
high = "#4467A3",
name = "Count",
guide = guide_colorbar(
barwidth = unit(0.6, "cm"),
barheight = unit(4.5, "cm"),
ticks = FALSE
)
) +
annotate("text", x = 0.4, y = 0.9, label = "Downtown Core",
color = INK_SOFT, size = 2.8, fontface = "italic", hjust = 0.5) +
annotate("text", x = -2.6, y = 2.9, label = "Transit Hub",
color = INK_SOFT, size = 2.8, fontface = "italic", hjust = 0.5) +
annotate("text", x = 3.0, y = -2.3, label = "University Campus",
color = INK_SOFT, size = 2.8, fontface = "italic", hjust = 0.5) +
labs(
title = plot_title,
x = "Easting (km from city center)",
y = "Northing (km from city center)"
) +
theme_minimal(base_size = 8) +
theme(
plot.background = element_rect(fill = PAGE_BG, color = PAGE_BG),
panel.background = element_rect(fill = PAGE_BG, color = NA),
panel.border = element_blank(),
panel.grid.major = element_line(color = GRID_COLOR, linewidth = 0.4),
panel.grid.minor = element_blank(),
axis.title = element_text(color = INK, size = 10),
axis.text = element_text(color = INK_SOFT, size = 8),
axis.line = element_line(color = INK_SOFT, linewidth = 0.4),
axis.ticks = element_blank(),
plot.title = element_text(
color = INK,
size = title_fs,
face = "bold",
margin = margin(b = 8)
),
legend.background = element_rect(fill = ELEVATED_BG, color = NA),
legend.text = element_text(color = INK_SOFT, size = 8),
legend.title = element_text(color = INK, size = 10),
plot.margin = margin(12, 15, 12, 12, "pt")
)
# Save
ggsave(
filename = sprintf("plot-%s.png", THEME),
plot = p,
device = ragg::agg_png,
width = 8,
height = 4.5,
units = "in",
dpi = 400
)
Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/hexbin-basic/ggplot2/code. Any spec id and library id listed in llms-full.txt fit the same URL shape; every URL below is complete and callable.
{
"spec_id": "hexbin-basic",
"language": "r",
"library": "ggplot2",
"page": "https://anyplot.ai/hexbin-basic/r/ggplot2",
"hub": "https://anyplot.ai/hexbin-basic",
"code_json": "https://api.anyplot.ai/specs/hexbin-basic/ggplot2/code",
"spec_json": "https://api.anyplot.ai/specs/hexbin-basic",
"render_light_png": "https://storage.googleapis.com/anyplot-images/plots/hexbin-basic/r/ggplot2/plot-light.png",
"render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/hexbin-basic/r/ggplot2/plot-dark.png",
"quality_score": 91.0,
"license": "MIT",
"guide": "https://anyplot.ai/llms.txt"
}Part of Basic Hexbin Plot on anyplot.ai.