The same plot in 14 other libraries — Python: Altair, Bokeh, lets-plot, Matplotlib, Plotly, plotnine, Pygal, Seaborn; Julia: Makie.jl; JavaScript: Chart.js, D3.js, Apache ECharts, Highcharts, MUI X Charts. Compare all 15 side by side: Voronoi Diagram for Spatial Partitioning in Python, R, Julia and JavaScript.
A Voronoi diagram partitions a plane into regions based on the distance to a set of seed points, where each region contains all points closer to its seed than to any other. This visualization is essential for understanding spatial relationships, proximity analysis, and territorial boundaries. It reveals natural clustering patterns and helps identify areas of influence around data points.

#' anyplot.ai
#' voronoi-basic: Voronoi Diagram for Spatial Partitioning
#' Library: ggplot2 3.5.1 | R 4.4.1
#' Quality: 90/100 | Created: 2026-05-17
library(ggplot2)
library(dplyr)
library(tidyr)
library(ragg)
set.seed(42)
# --- Theme tokens -----------------------------------------------------------
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"
IMPRINT <- c("#009E73", "#C475FD", "#4467A3", "#BD8233",
"#AE3030", "#2ABCCD", "#954477")
# --- Data: Seed points for Voronoi diagram --------------------------------
n_points <- 25
seed_points <- data.frame(
id = 1:n_points,
x = runif(n_points, 0, 100),
y = runif(n_points, 0, 100)
)
# --- Compute Voronoi cells using distance-based approach ------------------
# Create a fine grid and assign each grid point to its nearest seed
grid_resolution <- 200
grid_df <- expand.grid(
x = seq(0, 100, length.out = grid_resolution),
y = seq(0, 100, length.out = grid_resolution)
)
# Calculate distances from each grid point to all seeds
distances <- matrix(NA, nrow = nrow(grid_df), ncol = n_points)
for (i in seq_len(n_points)) {
distances[, i] <- sqrt(
(grid_df$x - seed_points$x[i])^2 +
(grid_df$y - seed_points$y[i])^2
)
}
# Assign each grid point to the nearest seed
grid_df$region <- apply(distances, 1, which.min)
grid_df$cell_id <- seed_points$id[grid_df$region]
# Join with seed point colors
grid_df <- grid_df %>%
mutate(
color_idx = ((cell_id - 1) %% 7) + 1,
color = IMPRINT[color_idx]
)
# --- Plot -------------------------------------------------------------------
p <- ggplot(grid_df, aes(x = x, y = y, fill = color)) +
geom_raster() +
geom_point(
data = seed_points,
aes(x = x, y = y),
inherit.aes = FALSE,
color = INK,
size = 5,
shape = 21,
fill = "white",
stroke = 1.5
) +
scale_fill_identity() +
scale_x_continuous(limits = c(0, 100), expand = c(0, 0)) +
scale_y_continuous(limits = c(0, 100), expand = c(0, 0)) +
coord_fixed() +
labs(
title = "voronoi-basic · ggplot2 · anyplot.ai",
x = "X",
y = "Y"
) +
theme_minimal(base_size = 14) +
theme(
plot.background = element_rect(fill = PAGE_BG, color = PAGE_BG),
panel.background = element_rect(fill = PAGE_BG, color = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(color = INK_SOFT, fill = NA, linewidth = 0.8),
axis.title = element_text(color = INK, size = 20),
axis.text = element_text(color = INK_SOFT, size = 16),
plot.title = element_text(color = INK, size = 24, face = "bold"),
legend.position = "none"
)
# --- Save -------------------------------------------------------------------
ggsave(
filename = sprintf("plot-%s.png", THEME),
plot = p,
device = ragg::agg_png,
width = 16,
height = 9,
units = "in",
dpi = 300
)
Runnable source as JSON, for any HTTP client: https://api.anyplot.ai/specs/voronoi-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": "voronoi-basic",
"language": "r",
"library": "ggplot2",
"page": "https://anyplot.ai/voronoi-basic/r/ggplot2",
"hub": "https://anyplot.ai/voronoi-basic",
"code_json": "https://api.anyplot.ai/specs/voronoi-basic/ggplot2/code",
"spec_json": "https://api.anyplot.ai/specs/voronoi-basic",
"render_light_png": "https://storage.googleapis.com/anyplot-images/plots/voronoi-basic/r/ggplot2/plot-light.png",
"render_dark_png": "https://storage.googleapis.com/anyplot-images/plots/voronoi-basic/r/ggplot2/plot-dark.png",
"quality_score": 90.0,
"license": "MIT",
"guide": "https://anyplot.ai/llms.txt"
}Part of Voronoi Diagram for Spatial Partitioning on anyplot.ai.