Skip to contents

stat_unpack_polygon() expands compact list-column coordinates stored as numeric vectors in x and y columns into long format suitable for geom_polygon() and geom_shape().

Usage

stat_unpack_polygon(
  mapping = NULL,
  data = NULL,
  geom = "polygon",
  position = "identity",
  na.rm = TRUE,
  show.legend = NA,
  inherit.aes = TRUE,
  ...
)

Arguments

na.rm

logical. If TRUE, missing values are removed. Default is TRUE.

...

Additional arguments passed to the base stat.

Details

This stat is designed for memory-efficient storage of polygon coordinates where each polygon occupies a single row, with x and y columns containing numeric vectors of coordinates. It unpacks these into the long format expected by ggplot2 polygon geoms during the statistical transformation layer.

The stat requires a grouping column (typically id or similar) to distinguish between separate polygons when multiple polygons are present in a single dataset. If no grouping is provided, each row is treated as a separate polygon.

Aesthetics

stat_unpack_polygon() requires the following aesthetics:

  • x — numeric vector (list-column) or numeric values

  • y — numeric vector (list-column) or numeric values

It can accept any aesthetics supported by geom_polygon() or geom_shape(), such as fill, colour, size, alpha, linetype.

See also

Examples

# Create compact polygon data with list-column coordinates
polygon_data <- data.frame(
  id = c(1, 2),
  x = list(c(0, 1, 1, 0), c(2, 3, 3.5, 2)),
  y = list(c(0, 0, 1, 1), c(0, 0, 1.5, 1)),
  fill = c("red", "blue")
)

# Plot with stat_unpack_polygon
# ggplot(polygon_data, aes(x = x, y = y, fill = fill)) +
#   geom_polygon(stat = "unpack_polygon") +
#   coord_fixed()