choropleth_ggplot2: Choropleth map of an sf object with ggplot2

View source: R/map_ggplot2.R

choropleth_ggplot2R Documentation

Choropleth map of an sf object with ggplot2

Description

Creates a choropleth map from an object of class sf. If the chosen variable can be classified into discrete intervals using Fisher's natural breaks, the polygons are shaded by cluster. Otherwise, the variable is visualized on a continuous scale.

Usage

choropleth_ggplot2(
  sf_object,
  value = output,
  n = 7,
  dig.lab = 2,
  legend_title = "Class",
  option = "D",
  direction = 1
)

Arguments

sf_object

An object of class sf containing polygon geometries.

value

Column in sf_object used to shade the polygons (default = output).

n

Integer. Number of clusters to use in Fisher classification (default = 7).

dig.lab

Integer. Number of digits to display in legend labels (default = 2).

legend_title

Character. Title for the legend (default = "Class").

option

Character string indicating the colormap option passed to viridis. Options are:

  • "magma" (or "A")

  • "inferno" (or "B")

  • "plasma" (or "C")

  • "viridis" (or "D", default)

  • "cividis" (or "E")

direction

Numeric. Order of colors in the scale. If 1 (default), colors go from darkest to lightest. If -1, the order is reversed.

Details

The function first attempts to classify the chosen variable into n clusters using Fisher's natural breaks (classInt::classIntervals()). If this fails (e.g. due to insufficient unique values), the function falls back to a continuous scale.

Value

A ggplot object containing the choropleth map.

Author(s)

Martin Haringa

Examples

test <- points_to_polygon(
  nl_postcode2,
  insurance,
  sum(amount, na.rm = TRUE)
)
choropleth_ggplot2(test, value = output)


spatialrisk documentation built on Sept. 14, 2025, 5:08 p.m.