Gives fine-grained control over a chart's title block: set the title text, add a subtitle line, control the horizontal alignment of the text, overlay the title on the plot area, rotate it, position it manually, and style the title and subtitle independently.
The subtitle is rendered as a second native paragraph inside the same
c:title element (the way 'Microsoft Office' shows a title with a subtitle
line), so it stays a real, editable chart title rather than a drawn shape.
This complements chart_labels(), which only sets the plain title text.
chart_title() supersedes any title previously set with chart_labels().
Usage
chart_title(
x,
title = NULL,
subtitle = NULL,
align = c("center", "left", "right"),
overlay = FALSE,
rotation = NULL,
position = NULL,
title_fp = NULL,
subtitle_fp = NULL
)Arguments
- x
an
ms_chartobject.- title
title text. Use
NULLto keep an existing title (for example one set withchart_labels()) while changing other options, or""/character(0)behaviour is not allowed - pass a single string.- subtitle
optional subtitle text shown on a second line under the title. Use
NULL(default) for no subtitle.- align
horizontal alignment of the title (and subtitle) text, one of
"center"(default),"left"or"right". This closes the long-standing request for left-aligned titles.- overlay
if
TRUE, the title overlays the plot area instead of reserving space above it. DefaultFALSE.- rotation
optional text rotation in degrees, between -90 and 90. Use
NULL(default) for horizontal text.- position
optional manual position for the title as a numeric vector
c(x, y)of fractions of the chart area (0-1) giving the top-left corner. UseNULL(default) to let Office place the title automatically.- title_fp
an
officer::fp_text()controlling the title font. WhenNULL(default), the theme'smain_titlestyle is used.- subtitle_fp
an
officer::fp_text()controlling the subtitle font. WhenNULL(default), a smaller, lighter style derived from the title is used.
Examples
library(officer)
dat <- data.frame(
name = c("A", "B", "C", "D"),
value = c(3, 8, 5, 9),
stringsAsFactors = FALSE
)
bc <- ms_barchart(data = dat, x = "name", y = "value")
bc <- chart_title(
bc,
title = "Quarterly revenue",
subtitle = "in millions of euros",
align = "left"
)
