Learn R Programming

munch (version 0.0.3)

element_chunks: Chunks Theme Element for 'ggplot2'

Description

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,
  size = NULL,
  margin = NULL,
  debug = FALSE,
  inherit.blank = FALSE,
  width = NULL
)

Value

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

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

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.

Examples

Run this code
if (FALSE) { # require("ggiraph", quietly = TRUE, character.only = TRUE)
library(ggplot2)
library(flextable)
library(ggiraph)

library(gdtools)
font_set_liberation()
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
}

Run the code above in your browser using DataLab