Skip to contents

Applies a font family - and, optionally, a size, colour, bold or italic setting - to all of a chart's text at once: the chart title, axis titles, axis (tick) labels, the legend, the data table and data labels.

The original workflow requires threading an officer::fp_text() through every slot of mschart_theme() (main_title, axis_title_x, axis_title_y, axis_text_x, axis_text_y, legend_text, table_text) and, separately, the per-series data-label fonts. chart_font() collapses that into a single call, which is the usual intent when a report needs, for example, "everything in Calibri, not Arial".

Only the properties you supply are changed; the others are preserved, so you can switch the font family while keeping the theme's relative sizing (a larger title, smaller axis labels, and so on).

Call chart_font() after chart_title() and chart_footer() so that the title, subtitle and footer fonts are restyled too.

This works for the classical chart types (ms_barchart(), ms_linechart(), ms_scatterchart(), ms_areachart(), ms_piechart()) as well as the chartEx types (box plot, histogram, pareto, ...).

Usage

chart_font(
  x,
  family = NULL,
  size = NULL,
  color = NULL,
  bold = NULL,
  italic = NULL
)

Arguments

x

an ms_chart object.

family

font family (typeface) to apply to every text element, for example "Calibri", "Segoe UI" or "Times New Roman". NULL (default) leaves the family unchanged. Names containing spaces are fully supported.

size

font size in points to apply to every text element. NULL (default) preserves each element's existing size (recommended, so the title stays larger than the axis labels). Hidden data labels (size 0) are never un-hidden by this argument.

color

font colour (for example "gray20" or "#1F4E79"). NULL leaves colours unchanged.

bold, italic

optional logicals to set bold / italic on every text element. NULL leaves them unchanged.

Value

An updated ms_chart object.

Examples

library(officer)

dat <- data.frame(
  name = c("A", "B", "C"),
  value = c(3, 8, 5),
  stringsAsFactors = FALSE
)

bc <- ms_barchart(data = dat, x = "name", y = "value")
bc <- chart_labels(bc, ylab = "Collaboration hours", xlab = "Organization")

# Switch every text element to Calibri, keeping the theme's relative sizes.
bc <- chart_font(bc, family = "Calibri")

# Or set a uniform size and colour as well.
bc <- chart_font(bc, family = "Segoe UI", size = 12, color = "gray20")