Skip to contents

This layer uses the same panel, centre, category and angle calculation as geom_pie(). Supply the same mappings and geometry settings to both layers. Its default label is a localized percentage for sectors or the localized total for each centre. A centre callback receives all sectors of one pie and returns one string. Mapped centre text styles must be constant per pie. Pass after-stat mappings to style or label the resulting rows.

Usage

geom_pie_label(
  mapping = NULL,
  data = NULL,
  ...,
  placement = c("inside", "outside", "center"),
  label = NULL,
  label_format = c("plain", "markdown", "formula"),
  min_fraction = 0,
  label_radius = NULL,
  check_overlap = FALSE,
  x0 = 0,
  y0 = 0,
  r = 1,
  r0 = 0,
  start = 0,
  direction = 1,
  order = "levels",
  explode = 0,
  gap = 0,
  lang = NULL,
  dictionary = NULL,
  variable = "slice",
  text_direction = "auto",
  show.legend = FALSE,
  inherit.aes = TRUE,
  na.rm = FALSE
)

Arguments

mapping

A mapping from ggplot2::aes(). Map the category to slice or to fill. Map optional nonnegative weight, pie_id, centre x0 and y0, outer radius r, inner radius r0 or per-sector explode. Each pie has one centre and pair of radii. Mapped values take precedence over corresponding numeric defaults. Without a weight each row counts once.

data

Layer data or NULL to inherit the plot data.

...

Other ggplot2 layer parameters, such as colour, linewidth or alpha.

placement

Where to place labels: inside sectors, outside sectors, or once per pie at its original centre.

label

Sector mode or callback as in ggpie(). For a centre, use "total", one literal string, or a function of the sector table.

label_format

Sector text syntax: "plain", "markdown", or "formula" with a callback returning one checked plotmath string per sector.

min_fraction

Hide labels for slices below this fraction of their pie total. The slices remain in the statistical table.

label_radius

Optional numeric fraction measured from inner to outer radius. Defaults to 0.5 inside or an outside anchor at 1.14 outer radii.

check_overlap

Hide overlapping labels in this layer and panel. Device-measured text boxes are considered in descending sector proportion, with ties resolved in table order. A centre uses its pie total as priority. The candidates remain available from ggplot2::layer_data().

x0, y0

Default centre in data units. Position scales must be continuous with the identity transform. Pie sectors use linear Cartesian coordinates. Supply centres and radii that keep both sides of each diameter finite and distinguishable from the centre in double precision.

r, r0

Default outer and inner radii in data units, with 0 <= r0 < r.

start

Starting angle in radians clockwise from 12 o'clock.

direction

Use 1 for clockwise or -1 for counterclockwise.

order

Sector order. "levels" uses factor order or first appearance, "data" uses first appearance, and "legacy" reverses factor or sorted character levels. An explicit character vector specifies the order of IDs.

explode

Radial displacement of the sector in data units.

gap

Angular space in radians between adjacent sectors. The geom trims half the gap from each end. A sector no wider than the gap has no visible polygon. Statistical shares and angles use the untrimmed geometry.

lang

Optional language tag used to format computed display labels.

dictionary

Optional local display dictionary accepted by forma_labels().

variable

Variable key for dictionary lookup. The stat retains original category IDs in slice and translated strings in category_label.

text_direction

Base reading direction, auto, ltr or rtl.

show.legend, inherit.aes

Standard ggplot2 legend and mapping settings.

na.rm

Passed to the geom. Invalid categories and weights raise errors.

Value

A ggplot2 layer. Centre rows contain x, y, total, total_label, and display_label; sector rows also contain the fields documented by stat_pie().

See also

Examples

d <- data.frame(site = c(1, 1, 4, 4), mode = c("A", "B", "A", "B"),
                n = c(9, 11, 12, 8))
ggplot2::ggplot(d, ggplot2::aes(x0 = site, slice = mode, fill = mode, weight = n)) +
  geom_pie(r0 = 0.5) + geom_pie_label(placement = "center", r0 = 0.5) +
  ggplot2::coord_fixed()