Use ggpie() when a single composition is the main subject. Counts can come from one row per observation or a frequency column. A named palette ties colours to stable category IDs. Set hole to make a ring. Text can show counts, percentages or author-provided labels.
library(ggplot2)
library(ggforma)
orders <- data.frame(kind = c("Walk", "Cycle", "Rail"), trips = c(24, 36, 40))
colours <- setNames(forma_palette(5)[c(2, 3, 4)], orders$kind)
ggpie(orders, "kind", "trips", label = "percent", .legend = TRUE, palette = colours)
ggpie(orders, "kind", "trips", hole = 0.55, center_label = "100 trips",
label = "percent", .legend = TRUE, palette = colours)
More than one centre
geom_pie() computes each pie inside its own panel and centre. The radii use data units, so give the axes equal physical scales. stat_pie() makes the same sector summaries available to other geoms. In this example text uses the statistic’s own label positions and percent labels.
travel <- data.frame(centre = rep(c(0, 3), each = 3), kind = rep(orders$kind, 2),
trips = c(24, 36, 40, 35, 40, 25))
ggplot(travel, aes(slice = kind, weight = trips, x0 = centre)) +
geom_pie(aes(fill = kind), r = 1, r0 = 0.25) +
stat_pie(aes(label = after_stat(percent_label)), geom = "text", r = 1,
r0 = 0.25, size = 3) +
scale_fill_manual(values = colours) + coord_fixed() + theme_void()
A sector of zero weight has zero span. Each pie requires a positive total. Check missing categories and nonfinite or negative weights before plotting. For crowded labels, consider a legend, min_fraction or the device-aware overlap policy on geom_pie_label().