| hexify_heatmap | R Documentation |
Creates a ggplot2-based visualization of hexagonal grid cells, optionally colored by a value column. Supports continuous and discrete color scales, projection transformation, and customizable styling.
hexify_heatmap(
data,
value = NULL,
basemap = NULL,
crs = NULL,
colors = NULL,
breaks = NULL,
labels = NULL,
hex_border = "#5D4E37",
hex_lwd = 0.3,
hex_alpha = 0.7,
basemap_fill = "gray90",
basemap_border = "gray50",
basemap_lwd = 0.5,
mask_outside = FALSE,
aperture = 3L,
xlim = NULL,
ylim = NULL,
title = NULL,
legend_title = NULL,
na_color = "gray90",
theme_void = TRUE
)
data |
A HexData object from |
value |
Column name (as string) to use for fill color. If NULL, cells are drawn with a uniform fill color. If not specified but data has a 'count' or 'n' column, that will be used automatically. |
basemap |
Optional basemap. Can be:
|
crs |
Target CRS for the map projection. Can be:
|
colors |
Color palette for the heatmap. Can be:
|
breaks |
Numeric vector of break points for binning continuous values,
or NULL for continuous scale. Use |
labels |
Labels for the breaks (length should be one less than breaks). If NULL, labels are auto-generated. |
hex_border |
Border color for hexagons |
hex_lwd |
Line width for hexagon borders |
hex_alpha |
Transparency for hexagon fill (0-1) |
basemap_fill |
Fill color for basemap polygons |
basemap_border |
Border color for basemap polygons |
basemap_lwd |
Line width for basemap borders |
mask_outside |
Logical. If TRUE and basemap is provided, mask hexagon portions that fall outside the basemap polygons. |
aperture |
Grid aperture (default 3), used if data is from hexify() |
xlim |
Optional x-axis limits (in target CRS units) as c(min, max) |
ylim |
Optional y-axis limits (in target CRS units) as c(min, max) |
title |
Plot title |
legend_title |
Title for the color legend |
na_color |
Color for NA values |
theme_void |
Logical. If TRUE (default), use a minimal theme without axes, gridlines, or background. |
This function provides publication-quality heatmap visualizations of hexagonal grids using ggplot2. It returns a ggplot object that can be further customized with standard ggplot2 functions.
A ggplot2 object that can be further customized or saved.
The function supports three types of color scales:
Set breaks = NULL for a continuous gradient
Provide breaks vector to bin values into categories
If value column is a factor, discrete colors are used
Common projections:
'WGS84' (unprojected lat/lon)
LAEA Europe
Web Mercator
Robinson (world maps)
Mollweide (equal-area world maps)
plot_grid for base R plotting,
cell_to_sf to generate polygons manually
Other visualization:
plot_world()
library(hexify)
# Sample data with counts
cities <- data.frame(
lon = c(16.37, 2.35, -3.70, 12.5, 4.9),
lat = c(48.21, 48.86, 40.42, 41.9, 52.4),
count = c(100, 250, 75, 180, 300)
)
result <- hexify(cities, lon = "lon", lat = "lat", area_km2 = 5000)
# Simple plot (uniform fill, no value mapping)
hexify_heatmap(result)
library(ggplot2)
# With world basemap
hexify_heatmap(result, basemap = "world")
# Heatmap with value mapping
hexify_heatmap(result, value = "count")
# With world basemap and custom colors
hexify_heatmap(result, value = "count",
basemap = "world",
colors = "YlOrRd",
title = "City Density")
# Binned values with custom breaks
hexify_heatmap(result, value = "count",
basemap = "world",
breaks = c(-Inf, 100, 200, Inf),
labels = c("Low", "Medium", "High"),
colors = c("#fee8c8", "#fdbb84", "#e34a33"))
# Different projection (LAEA Europe)
hexify_heatmap(result, value = "count",
basemap = "world",
crs = 3035,
xlim = c(2500000, 6500000),
ylim = c(1500000, 5500000))
# Customize further with ggplot2
hexify_heatmap(result, value = "count", basemap = "world") +
labs(caption = "Data source: Example") +
theme(legend.position = "bottom")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.