Skip to contents

A theme element that renders 'flextable' chunks directly in plot titles, subtitles, captions, and other text positions. This allows using superscripts, subscripts, and other formatting not available in markdown.

The format of each chunk comes from the chunk itself, for example as_chunk(props = fp_text_lite(font.size = 14)); what the chunk does not set comes from the theme (size, font family, colour and face, as for other texts of the plot, or size of the element), and what the theme does not set comes from flextable::set_flextable_defaults().

Usage

element_chunks(
  chunks,
  hjust = NULL,
  vjust = NULL,
  angle = NULL,
  lineheight = NULL,
  size = NULL,
  margin = NULL,
  debug = FALSE,
  inherit.blank = FALSE,
  width = NA_real_
)

Arguments

chunks

A paragraph object created with flextable::as_paragraph() containing chunks such as flextable::as_chunk(), flextable::as_sup(), flextable::as_sub(), etc.

hjust

Horizontal justification (0-1).

vjust

Vertical justification (0-1).

angle

Text rotation angle in degrees.

lineheight

Line height multiplier.

size

Font size in points of the chunks that do not set theirs. By default, the size of the theme.

margin

Margins around the text (use ggplot2::margin()).

debug

Show debug rectangles.

inherit.blank

Should this element inherit blank elements.

width

Maximum width of the text in inches, for titles, subtitles and captions, which wrap to their available width by default. With 'patchwork' or a fixed aspect ratio, the available width is not known when the height of the title is computed: the text may need more lines than the space kept for it, and setting width solves this. It is ignored for rotated text and for text with an explicit position. NA, the default, sets no width.

Value

An S7 element of class element_chunks, which extends ggplot2::element_text().

Examples

library(ggplot2)
library(flextable)
library(ggiraph)

library(gdtools)
font_set_liberation()
#> Font set
#>   sans:    Liberation Sans [liberation]
#>   serif:   Liberation Serif [liberation]
#>   mono:    Liberation Mono [liberation]
#>   symbol:  Liberation Sans [liberation]
#>   4 HTML dependencies
flextable::set_flextable_defaults(
  font.size = 12,
  font.family = "Liberation Sans"
)

# Caption with subscript (H2O)
p <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  theme_minimal(base_family = "Liberation Sans") +
  theme(
    plot.caption = element_chunks(
      as_paragraph(
        as_chunk("Chemical formula: "),
        as_chunk("H"),
        as_sub("2"),
        as_chunk("O")
      )
    )
  )

x <- girafe(
  ggobj = p,
  options = list(opts_sizing(rescale = FALSE)),
  dependencies = list(
    liberationsansHtmlDependency()
  )
)
x
# Title with superscript p <- ggplot(mtcars, aes(mpg, wt)) + geom_point() + theme_minimal(base_family = "Liberation Sans") + theme( plot.title = element_chunks( as_paragraph( as_chunk("Model fit: R"), as_sup("2"), as_chunk(" = 0.87") ) ) ) x <- girafe( ggobj = p, options = list(opts_sizing(rescale = FALSE)), dependencies = list( liberationsansHtmlDependency() ) ) x