Skip to contents

The viz_choro function creates a choropleth map from a spatial data frame by classifying a numeric variable and mapping it to a color palette. It supports multiple classification methods and automatic legend generation.

Usage

viz_choro(
  map,
  data = NULL,
  var,
  method = "quantile",
  nb = 6,
  breaks = NULL,
  colors = NULL,
  middle = FALSE,
  sd = 1,
  reverse = FALSE,
  missing = "white",
  legend = TRUE,
  leg_type = "vertical",
  leg_pos = c(10, 10),
  ...
)

Arguments

map

A geovizr map object created using viz_create().

data

A spatial dataframe Use data to enable iteration over features.

var

character. Name of the numeric variable used for classification. You can also use fill or stroke directly instead of var.

method

character. Optional. Classification method (default "quantile"). One of: "quantile", "q6", "equal", "jenks", "msd", "geometric", "headtail", "pretty", "arithmetic", "nestedmeans".

nb

numeric. Optional. Number of classes (default 6).

breaks

numeric vector. Optional. Manual class breaks. Overrides nb and method.

colors

character or vector. Optional. Color palette or vector of colors. Can use named palettes from the Dicopal library. See https://observablehq.com/@neocartocnrs/dicopal-library

middle

logical. Optionnal Only for the MSD method: is the mean in a central class or not? (default FALSE)

sd

number. Only for the MSD method: the number of standard deviations taken into account (default 1)

reverse

logical. Optional. Reverse color palette (default FALSE).

missing

character or logical. Optional. Color for missing values (default "white").

legend

logical. Optional. Whether to display a legend (default TRUE).

leg_type

character. Optional. Legend orientation (default "vertical"). One of: "horizontal", "vertical".

leg_pos

numeric vector. Optional. Legend position (default c(10, 10)).

...

Additional parameters passed to path rendering. With the leg_ prefix, you can configure the legend. For example: leg_title, leg_subtitle, leg_note, etc.

Value

A modified `geoviz` map object with a new layer added. Rendering is performed using viz_render().

Examples

library(sf)
world <- st_read(
  system.file("gpkg/world.gpkg", package = "geovizr"),
  quiet = TRUE
)

viz_create(
  projection = "EqualEarth", background = "white",
  zoomable = TRUE
) |>
  viz_choro(
    data = world, var = "gdppc", method = "quantile", nb = 5,
    leg_values_round = 0, leg_title = "GDP\nper\ncapita",
    leg_subtitle = "(in $/inh.)", colors = "PinkYl"
  ) |>
  viz_render()