Skip to contents

Each panel is split by mapped pie ID or centre. The stat aggregates repeated category IDs and normalizes the resulting sectors inside each pie. Use equal axis units, e.g. coord_fixed(), for circular pies. The high-level ggpie() sets equal units automatically.

Usage

geom_pie(
  mapping = NULL,
  data = NULL,
  stat = StatFormaPie,
  position = "identity",
  ...,
  x0 = 0,
  y0 = 0,
  r = 1,
  r0 = 0,
  start = 0,
  direction = 1,
  order = "levels",
  explode = 0,
  gap = 0,
  resolution = 180L,
  label_radius = 0.5,
  lang = NULL,
  dictionary = NULL,
  variable = "slice",
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

stat_pie(
  mapping = NULL,
  data = NULL,
  geom = GeomFormaPie,
  position = "identity",
  ...,
  x0 = 0,
  y0 = 0,
  r = 1,
  r0 = 0,
  start = 0,
  direction = 1,
  order = "levels",
  explode = 0,
  gap = 0,
  resolution = 180L,
  label_radius = 0.5,
  lang = NULL,
  dictionary = NULL,
  variable = "slice",
  na.rm = FALSE,
  show.legend = NA,
  inherit.aes = TRUE
)

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.

stat

A stat object or "identity" for one row per sector with mapped start and end in radians. Identity mode accepts optional centre and radii.

position

Layer position adjustment. The supported value is "identity".

...

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

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.

resolution

Number of line segments used for a full circle (8 to 4096).

label_radius

Label position as a fraction of the distance from the inner radius to the outer radius. Values above one place labels outside.

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.

na.rm

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

show.legend, inherit.aes

Standard ggplot2 legend and mapping settings.

geom

A geom object or name, e.g. "text" using aes(label = after_stat(percent_label)).

Value

A ggplot2 layer object.

Computed variables

after_stat() can access count, total, prop (0 to 1), percent (0 to 100), percent_label, count_label, total_label, start, end, mid, label_x, label_y, r and r0. The stat also sets x and y to the label anchors. Zero-weight sectors inside a positive-total pie have zero angle. A pie with a zero total raises an error. The grouping aesthetic does not define pie totals. Supply pie_id or separate centres for multiple pies.

Examples

d <- data.frame(kind = c("A", "B", "A", "C"), centre = c(0, 0, 3, 3))
ggplot2::ggplot(d, ggplot2::aes(x0 = centre, fill = kind)) +
  geom_pie(r = 0.8) + ggplot2::coord_fixed()

ggplot2::ggplot(d, ggplot2::aes(x0 = centre, slice = kind)) +
  stat_pie(ggplot2::aes(label = ggplot2::after_stat(percent_label)), geom = "text") +
  ggplot2::coord_fixed()