ggformaEnglish

在同一面板绘制一个或多个饼图

函数索引 / 饼图与环形图

geom_pie() · stat_pie()

geom_pie() 绘制扇区,stat_pie() 在每个面板内按饼图标识或圆心计算各自的比例和角度。

怎么用

把类别映射给 slice,汇总数据映射 weight。x0、y0 指定圆心,r 和 r0 控制外、内半径;添加 coord_fixed() 得到圆形。

返回结果

返回 ggplot2 图层。可通过 after_stat() 使用 count、total、prop、扇区起止角和标签位置等计算结果。

调用方式

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
)

参数

mapping、data、stat、position、...、x0, y0、r, r0、start、direction、order、explode、gap、resolution、label_radius、lang、dictionary、variable、na.rm、show.legend, inherit.aes、geom

逐项参数说明(英文)

R 示例

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()

查看完整英文文档