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.

Usage

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

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.

margin

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

debug

Show debug rectangles.

inherit.blank

Should this element inherit blank elements.

Value

An element of class c("element_chunks", "element_text", "element").

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