Skip to contents

An S4 class representing hexified data. Contains the original user data plus cell assignments from the hexification process.

Usage

# S4 method for class 'HexData'
grid_info(x)

# S4 method for class 'HexData'
cells(x)

# S4 method for class 'HexData'
n_cells(x)

# S4 method for class 'HexData'
nrow(x)

# S4 method for class 'HexData'
ncol(x)

# S4 method for class 'HexData'
dim(x)

# S4 method for class 'HexData'
names(x)

# S4 method for class 'HexData'
x$name

# S4 method for class 'HexData'
x$name <- value

# S4 method for class 'HexData'
x[i, j, ..., drop = FALSE]

# S4 method for class 'HexData'
x[[i]]

# S4 method for class 'HexData,ANY,missing'
x[[i, j]] <- value

# S4 method for class 'HexData'
show(object)

# S4 method for class 'HexData'
summary(object, ...)

# S3 method for class 'hexify_data_summary'
print(x, ...)

# S4 method for class 'HexData'
as.data.frame(x, row.names = NULL, optional = FALSE, ...)

# S4 method for class 'HexData'
as.list(x, ...)

# S3 method for class 'HexData'
as_tibble(x, ...)

# S3 method for class 'HexData'
st_as_sf(
  x,
  ...,
  geometry = c("point", "polygon"),
  shape = c("hexagon", "gosper", "descendants"),
  depth = 3L
)

# S3 method for class 'HexData'
st_crs(x, ...)

# S4 method for class 'HexData,missing'
plot(
  x,
  y,
  basemap = TRUE,
  clip_basemap = TRUE,
  basemap_fill = "gray90",
  basemap_border = "gray50",
  basemap_lwd = 0.5,
  grid_fill = "#E69F00",
  grid_border = "#5D4E37",
  grid_lwd = 0.8,
  grid_alpha = 0.7,
  fill = NULL,
  show_points = FALSE,
  point_size = "auto",
  point_color = "red",
  point_alpha = 1,
  jitter = TRUE,
  crop = TRUE,
  crop_expand = 0.1,
  main = NULL,
  shape = c("hexagon", "gosper", "descendants"),
  depth = 3L,
  ...
)

Arguments

x

A HexData object; for print(), a data summary as summary() returns one

name

Column name

value

Replacement value

i, j

Row/column indices

...

For plot(), passed on to base plot(); ignored by the other methods

drop

Logical, whether to drop dimensions

object

A HexData object

row.names

Optional row names

optional

Logical (ignored)

geometry

For st_as_sf(): "point" (default) gives each row the centre of its cell, "polygon" the cell's boundary

shape, depth

Cell shape for polygon geometry and for plot(), as for cell_to_sf

y

Ignored (for S4 method compatibility)

basemap

Basemap specification:

  • TRUE or "world": Use built-in world map

  • FALSE or NULL: No basemap

  • "world_hires": High-resolution map from 'rnaturalearth' (requires the package)

  • An sf object: User-supplied vector map

  • A SpatRaster: User-supplied raster, drawn in grayscale

clip_basemap

Clip basemap to data extent (default TRUE). Clipping temporarily disables S2 spherical geometry to avoid edge-crossing errors.

basemap_fill

Fill color for basemap (default "gray90")

basemap_border

Border color for basemap (default "gray50")

basemap_lwd

Line width for basemap borders (default 0.5)

grid_fill

Fill color for grid cells (default "#E69F00" - amber/orange)

grid_border

Border color for grid cells (default "#5D4E37" - dark brown)

grid_lwd

Line width for cell borders (default 0.8)

grid_alpha

Transparency for cell fill (0-1, default 0.7)

fill

Column name for fill mapping (optional)

show_points

Show original points on top of cells (default FALSE). Points are jittered within their assigned hexagon.

point_size

Size of points. Can be:

  • A number (direct cex value)

  • A preset defining what fraction of a hex cell one point covers: "tiny" (~2\ "large" (~20\

point_color

Color of points (default "red")

point_alpha

Transparency for points (0-1, default 1)

jitter

If TRUE (default), points are randomly scattered within their assigned hexagon. If FALSE, all points in a cell are plotted at the cell centroid instead.

crop

Crop to data extent (default TRUE)

crop_expand

Expansion factor for crop (default 0.1)

main

Plot title

Value

  • show(): the object, invisibly, after printing two lines: rows, columns and cells, and the grid

  • summary(): a list of class hexify_data_summary carrying rows, columns, column_names, n_cells, type, the grid summary and a preview of the first rows; printing it lists all of these and returns it invisibly

  • grid_info(): HexGridInfo object containing grid specification

  • cells(): vector of unique cell IDs

  • n_cells(): integer count of unique cells

  • nrow(), ncol(), dim(): integer dimensions

  • names(): character vector of column names (including virtual cell columns)

  • $, [[: the requested column or cell data as a vector

  • $<-, [[<-: the modified HexData object

  • [: subsetted HexData object or extracted data

  • as.data.frame(): data frame with original data plus cell columns

  • as.list(): named list containing data, grid, cell_id, and cell_center

  • as_tibble(): the original data (without geometry) as a tibble

  • plot(): the object, invisibly

  • st_as_sf(): an sf object, one feature per row

  • st_crs(): the grid's coordinate reference system, as for a HexGridInfo

Details

HexData objects are created by hexify. The original data is preserved in the data slot, while cell assignments are stored separately in cell_id and cell_center.

Use as.data.frame() to get a combined data frame with cell columns.

Unlike [.data.frame, drop defaults to FALSE: selecting a single column returns a HexData object (preserving grid/ cell_id/cell_center) rather than dropping to a bare vector. Pass drop = TRUE explicitly to get data.frame-style dropping.

Slots

data

Data frame or sf object. The original user data (untouched).

grid

HexGridInfo object. The grid specification used.

cell_id

Cell IDs for each row of data: bit64::integer64 for ISEA grids, character for H3 grids.

cell_center

Matrix. Two-column matrix (lon, lat) of cell centers.

Plotting data

plot() draws the cells the data falls in, with an optional basemap. It builds polygons for the cells present in the data when it draws them, rather than storing them, to keep memory use low.

See also

hexify for creating HexData objects, HexGridInfo-class for grid specifications

hexify_heatmap for ggplot2 plotting

Examples

df <- data.frame(lon = c(0, 10, 20), lat = c(45, 50, 55), n = 1:3)
result <- hexify(df, lon = "lon", lat = "lat", area_km2 = 1000)
result
summary(result)
summary(result)$column_names
cells(result)
n_cells(result)
head(as.data.frame(result))
result$cell_id
result[1:2, ]
st_crs(result)

# sf points at the cell centres, or the cells as polygons
sf_pts <- st_as_sf(result)
sf_poly <- st_as_sf(result, geometry = "polygon")

if (requireNamespace("tibble", quietly = TRUE)) {
  tibble::as_tibble(result)
}
# \donttest{
df <- data.frame(lon = runif(50, -5, 5), lat = runif(50, 45, 50))
result <- hexify(df, lon = "lon", lat = "lat", area_km2 = 2000)

# Basic plot
plot(result, basemap = FALSE)

# With basemap and custom styling
plot(result, grid_fill = "lightblue", grid_border = "darkblue")
# }