Skip to contents

Creation of a piechart object that can be inserted in a 'Microsoft' document.

Pie charts show the proportion of each category as a slice of a circle. Doughnut charts are similar but have a hole in the centre. Use chart_settings(x, hole_size = ...) to control the hole size: 0 produces a pie chart, values above 0 produce a doughnut chart.

Data must be pre-aggregated: one row per slice, no grouping column.

Usage

ms_piechart(data, x, y, labels = NULL)

Arguments

data

a data.frame

x

column name for categories (slices).

y

column name for values (slice sizes).

labels

column names of columns to be used as custom data labels displayed next to data points (not axis labels). Optional. If more than one name is provided, only the first one will be used as a label, but all labels (transposed if a group is used) will be available in the Excel file associated with the chart.

Value

An ms_chart object.

Examples

library(officer)
library(mschart)

dat <- data.frame(
  browser = c("Chrome", "Firefox", "Safari", "Edge", "Other"),
  value = c(64, 12, 8, 5, 11)
)

# Pie chart
pie <- ms_piechart(data = dat, x = "browser", y = "value")
pie <- chart_labels(pie, title = "Browser share")

# Doughnut chart
donut <- ms_piechart(data = dat, x = "browser", y = "value")
donut <- chart_settings(donut, hole_size = 50)
donut <- chart_labels(donut, title = "Browser share (donut)")