MyNixOS website logo
Description

Rich Inline Text for 'grid' Graphics and 'flextable'.

Renders rich inline text (bold, italic, code, links, images) in grid graphics and 'ggplot2', from markdown or 'flextable' chunks. Provides grobs, theme elements, and geometry layers for styled text rendering. Only works with graphics devices that support 'systemfonts', such as those provided by 'ragg', 'svglite', or 'ggiraph'. The 'cairo_pdf' device is also supported when fonts are installed at the system level.

munch

R-CMD-check

‘munch’ crunches markdown text and ‘flextable’ paragraphs into grid graphics.

‘munch’ started as internal code in ‘flextable’ for rendering rich text with ‘grid’. It was extracted into its own package to also serve ‘ggiraph’ and to provide unified formatting between ‘ggplot2’ graphics and ‘flextable’ tables.

‘munch’ lets you produce this annotated graphic:

The title, subtitle, caption and axis labels above are all built with element_chunks() and ‘flextable’ paragraph objects, bold, italic, colors, highlights, superscripts, subscripts, strikethrough and an embedded image.

FunctionPurposeInput
element_md()Markdown in theme elementsCharacter string
element_chunks()Advanced formatting in theme elementsas_paragraph() object
geom_text_md()Markdown text annotationslabel aesthetic
geom_label_md()Markdown labels with backgroundlabel aesthetic

Installation

# install.packages("pak")
pak::pak("ardata-fr/munch")

Supported graphics devices

‘munch’ requires graphics devices that support ‘systemfonts’: ‘ragg’, ‘svglite’, or ‘ggiraph’. ‘cairo’ devices is also supported when fonts are installed at the system level. See vignette("font-recipes") for font configuration, alternative devices, and troubleshooting.

Quick start

ggplot2

Use element_md() for markdown in theme elements, and geom_text_md() or geom_label_md() for annotations:

library(ggplot2)

ggplot(mtcars, aes(mpg, wt)) +
 geom_point() +
 labs(title = "**Fuel consumption** vs *Weight*") +
 theme(plot.title = element_md(size = 16))

For advanced formatting (superscripts, subscripts, colors), use element_chunks() with ‘flextable’ paragraph objects:

title_chunks <- as_paragraph(
 as_chunk("Model: R"), as_sup("2"), as_chunk(" = 0.75")
)

ggplot(mtcars, aes(mpg, wt)) +
 geom_point() +
 theme(plot.title = element_chunks(title_chunks))

Syntax comparison

Syntaxelement_md()element_chunks()
Bold / Italic**text** / *text*as_b() / as_i()
Inline code`code`x
Strikethrough~~text~~as_strike()
Superscriptxas_sup()
Subscriptxas_sub()
Custom colorsxcolorize()
Highlightingxas_highlight()

Patchwork integration

Use munch_annotation() as a drop in replacement for patchwork::plot_annotation() which accepts element_chunk() and element_md() to add rich-text titles, subtitles, and captions — including auto-wrapping — to combined plots:

library(ggplot2)
library(patchwork)

p1 <- ggplot(mtcars, aes(mpg, wt)) +
  geom_point() +
  labs(x = "MPG", y = "Weight")

p2 <- ggplot(mtcars, aes(hp, wt)) +
  geom_point() +
  labs(x = "Horsepower", y = "Weight")

title_chunks <- as_paragraph(
  as_b("Motor Trend"), as_chunk(" — engine performance, 1974")
)

caption_chunks <- as_paragraph(
  as_chunk("R"), as_sup("2"), as_chunk(" = 0.75 · "),
  as_i("Lorem ipsum dolor sit amet, ut platea nullam odio dui in. "),
  as_i("Placerat gravida. Pretium phasellus non, dolor suscipit at aliquet, "),
  as_i("mollis venenatis per. Mus leo ante ligula.")
)

wrap_plots(p1, p2) +
  munch_annotation(
    title   = element_chunks(title_chunks),
    caption = element_chunks(caption_chunks, lineheight = 1.5, hjust = 0)
  )

For plain markdown text, pass the content as a string and use element_md() in the theme argument — the same pattern as regular ggplot2 theming:

wrap_plots(p1, p2) +
  munch_annotation(
    title   = "**Motor Trend** — engine performance, 1974",
    caption = "R^2^ = 0.75 · *Lorem ipsum dolor sit amet, ut platea nullam odio dui in. Placerat gravida pretium phasellus non, dolor suscipit at aliquet.*",
    theme   = theme(
      plot.title   = element_md(),
      plot.caption = element_md(lineheight = 1.5, hjust = 0)
    )
  )

The caption auto-wraps to the full figure width at render time, so it works correctly regardless of output size. title, subtitle, and caption each accept an element_chunks() or element_md() object, a bare as_paragraph() object, or a plain character string.

flextable

Use as_paragraph_md() for markdown in table cells:

ft <- flextable(head(iris, 3))
ft <- mk_par(ft, j = 1, part = "header",
 value = as_paragraph_md("*Sepal* **Length**"))
autofit(ft)

Markdown to grob

library(munch)
library(grid)

gr <- md_grob("This is **bold** and *italic* text.")
grid.newpage()
grid.draw(gr)

For superscripts, subscripts, or colors not available in markdown, use ‘flextable’ chunks with chunks_grob():

library(flextable)

chunks <- as_paragraph(as_chunk("E = mc"), as_sup("2"))
gr <- chunks_grob(chunks)
grid.newpage()
grid.draw(gr)

Learn more

Comparisons

‘gridtext’

‘gridtext’ by Claus O. Wilke (used by ‘ggtext’) renders formatted text via a subset of HTML/CSS. ‘munch’ takes a different approach: it uses markdown or ‘flextable’ chunks as input and relies on ‘systemfonts’ for text measurement.

munchgridtext
Input formatMarkdown, flextable chunksHTML / CSS subset
Superscript / subscriptYes (as_sup(), as_sub())Yes (<sup>, <sub>)
Custom colors / highlightYes (via colorize(), as_highlight())Yes (via inline CSS)
Inline imagesYesYes
Text wrappingYesYes (textbox_grob())
Text measurementTBDTBD
Integration with ‘flextable’Yes (shared chunk model)No
Supported devices‘ragg’, ‘svglite’, ‘ggiraph’, cairoMost R graphic devices
Compiled codeNo (pure R)Yes (C++ via ‘Rcpp’)

with ‘marquee’

‘marquee’ by Thomas Lin Pedersen offers more markdown features (headings, lists, code blocks, custom spans). ‘munch’ focuses first on integration with ‘flextable’ chunks and advanced formatting not available in markdown.

  • ‘munch’ works with ‘ragg’, ‘svglite’, ‘ggiraph’, cairo_pdf(), png(type = "cairo"), eventually pdf() with few cautions.
  • ‘marquee’ works on a wide range of R devices but not yet with ‘svglite’ or ‘ggiraph’.
  • If working with markdown, ‘munch’ expect only a single paragraph, ‘marquee’ can handle many markdown blocks in one call.
Metadata

Version

0.0.2

License

Unknown

Platforms (80)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows