Skip to contents

donut_leaflet() returns a leaflet htmlwidget with clickable donut segments, optional origin-destination links or curved trajectories, popups, labels, a legend, and layer controls.

Usage

donut_leaflet(
  data,
  id,
  category,
  value,
  map = NULL,
  lon = NULL,
  lat = NULL,
  input_crs = 4326,
  crs = 3857,
  radius_range = NULL,
  inner_radius = 0.55,
  n = 96,
  colours = NULL,
  flows = NULL,
  from = NULL,
  to = NULL,
  flow_value = NULL,
  flow_group = NULL,
  flow_min = NULL,
  flow_weight_range = c(1, 8),
  flow_curvature = 0.18,
  flow_n = 30,
  flow_arrow = TRUE,
  flow_arrow_size = NULL,
  flow_colour = "grey35",
  flow_colours = NULL,
  flow_legend = TRUE,
  flow_legend_position = "topright",
  flow_opacity = 0.55,
  provider_tiles = "CartoDB.Positron",
  popup = TRUE,
  label = TRUE,
  prefer_canvas = FALSE,
  map_fill = "#f3f4f6",
  map_colour = "#ffffff",
  map_weight = 1,
  map_opacity = 0.9,
  donut_colour = "#ffffff",
  donut_weight = 1,
  donut_opacity = 0.9,
  donut_smooth_factor = 0
)

Arguments

data

A data frame or sf object. Each row is one category for one donut location.

id

Unquoted column identifying each donut location.

category

Unquoted column identifying donut categories.

value

Unquoted numeric column giving non-negative category values.

map

Optional sf object used as a background layer.

lon, lat

Unquoted longitude and latitude columns. Required when data is not an sf object.

input_crs

Coordinate reference system for lon and lat, or for an sf object with missing CRS. Defaults to EPSG:4326.

crs

Target projected CRS used to build interactive donut and trajectory geometries. Defaults to EPSG:3857, Leaflet's default display projection, so donut circles and sector separators remain visually regular on screen.

radius_range

Numeric vector of length 2 giving minimum and maximum donut radii in map units. If NULL, a range is derived from the map extent.

inner_radius

Numeric value in (0, 1) giving the inner radius as a proportion of the outer radius.

n

Number of points used to approximate a complete outer circle.

colours

Optional category colours. Use a named vector for stable category-colour matching.

flows

Optional data frame of origin-destination flows.

from, to

Unquoted columns in flows identifying origin and destination ids. Required when flows is supplied.

flow_value

Optional unquoted numeric column in flows used to scale line widths. If omitted, each flow receives value 1.

flow_group

Optional unquoted column in flows used to colour flow lines and arrowheads by group.

flow_min

Optional minimum flow value to draw.

flow_weight_range

Numeric vector of length 2 controlling interactive flow line weights.

flow_curvature

Numeric curvature for trajectory lines. Use 0 for straight lines, positive values for one bend direction, and negative values for the opposite direction.

flow_n

Number of points used to approximate each curved trajectory.

flow_arrow

Should interactive flow trajectories include arrowheads?

flow_arrow_size

Arrowhead length in projected map units. If NULL, a size is derived from the donut radii.

flow_colour

Flow line colour used when flow_group is not supplied.

flow_colours

Optional colours for flow_group. Use a named vector for stable group-colour matching. If omitted and flow groups match donut categories, colours is reused.

flow_legend

Should a separate flow colour legend be shown when flow_group is supplied?

flow_legend_position

Position of the flow colour legend.

flow_opacity

Flow line opacity.

provider_tiles

Leaflet provider tiles. Use NULL to skip tile layers.

popup

Should popups be attached to donut segments and flow lines?

label

Should hover labels be attached to donut segments and flow lines?

prefer_canvas

Should Leaflet prefer Canvas over SVG for vector rendering? The default FALSE gives crisper small donut separators.

map_fill, map_colour

Background map fill and outline colours.

map_weight

Background map border weight.

map_opacity

Background map opacity.

donut_colour

Donut segment border colour.

donut_weight

Donut segment border weight.

donut_opacity

Donut segment fill opacity.

donut_smooth_factor

Leaflet path simplification factor for donut polygons. The default 0 preserves the donut vertices so arcs and sector separators are not simplified by Leaflet.

Value

A leaflet htmlwidget.

Examples

demo <- data.frame(
  place = rep(c("A", "B", "C"), each = 3),
  lon = rep(c(-71.35, -71.2, -71.05), each = 3),
  lat = rep(c(46.75, 46.82, 46.73), each = 3),
  category = rep(c("x", "y", "z"), times = 3),
  value = c(10, 20, 5, 5, 15, 10, 12, 4, 9)
)

donut_leaflet(demo, place, category, value, lon = lon, lat = lat)