Skip to contents

Override the data label of specific points in a series: hide selected labels, or give them their own number format, position or text formatting. This exposes per-point c:dLbl elements (with c:delete or point-level styling), which the original mschart declined to implement (ardata-fr/mschart issues #62 and #67, "I will not implement this").

Usage

chart_labels_at(
  x,
  index,
  series = NULL,
  delete = FALSE,
  num_fmt = NULL,
  position = NULL,
  text = NULL
)

Arguments

x

an ms_chart object.

index

integer vector of 1-based point positions (categories) whose labels should be overridden.

series

character vector of series names to target. Use NULL (default) to apply to every series. For an ungrouped chart the single series is named after the y column.

delete

logical(1): if TRUE, hide the labels at index (emitting c:delete). When TRUE, the styling arguments are ignored.

num_fmt

optional number format code for the selected labels (see the num_formats helpers). Inherits the series/plot format when NULL.

position

optional data label position for the selected labels (one of the positions accepted by chart_data_labels()). Only applied for chart types that support explicit label positions.

text

an optional officer::fp_text() controlling the font, colour, size and weight of the selected labels.

Value

An ms_chart object.

Details

Restyling (as opposed to deleting) only changes the appearance of labels that are already displayed, so enable the labels first with chart_data_labels() (for example show_val = TRUE). Repeated calls accumulate: overriding the same point again replaces its previous override.

Examples

library(officer)

dat <- data.frame(
  name = c("A", "B", "C", "D", "E"),
  value = c(3, 8, 5, 9, 4),
  stringsAsFactors = FALSE
)
bc <- ms_barchart(data = dat, x = "name", y = "value")
bc <- chart_data_labels(bc, show_val = TRUE, position = "outEnd")

# hide the label on the first bar, and highlight the peak (4th) in red
bc <- chart_labels_at(bc, index = 1, delete = TRUE)
bc <- chart_labels_at(
  bc, index = 4,
  text = fp_text(color = "red", bold = TRUE, font.size = 14)
)

doc <- read_pptx()
doc <- add_slide(doc, "Title and Content", "Office Theme")
doc <- ph_with(doc, bc, location = ph_location_fullsize())
print(doc, target = tempfile(fileext = ".pptx"))