Skip to contents

Professional Graphics and Typography for ggplot2

ggforma helps R users draw statistical charts, combine styled text with equations, and display labels in a reader’s language. It works with ordinary ggplot2 plots and uses local fonts and graphics devices. Mosaic and Pie charts retain the original data identifiers when the displayed language changes.

Three ggforma examples: a signed residual Mosaic, a donut showing frequencies, and styled text with a vector equation.
Three ggforma examples: a signed residual Mosaic, a donut showing frequencies, and styled text with a vector equation.

Installation

Requires R 4.3.0 or later. Get the version used in these tutorials from GitHub Release v0.2.2.

Install the tagged version

install.packages("remotes")
remotes::install_github("shanlong-who/ggforma@v0.2.2")

If the dependencies are already installed, download the verified source archive and run this command from its download folder.

install.packages("ggforma_0.2.2.tar.gz", repos = NULL, type = "source")

Start with an ordinary ggplot

library(ggplot2)
library(ggforma)

cars <- transform(mtcars, cylinders = factor(cyl))
ggplot(cars, aes(wt, mpg, colour = cylinders, shape = cylinders)) +
  geom_point(size = 2.5) + scale_colour_forma() +
  labs(title = "Fuel economy by engine size",
       x = "Weight (1,000 lb)", y = "Miles per gallon") +
  theme_forma(preset = "report", grid = "y")

Mosaic and Pie

ggmosaic() divides a contingency table into areas. With zero gaps each tile’s area equals its observed share. Model residuals add a signed measure of departure from the expected counts. ggpie() accepts raw rows or a frequency column. A named palette or fixed scale limits preserve the same category colours across views.

eyes <- marginSums(HairEyeColor, c("Hair", "Eye"))
ggmosaic(eyes, shade = "hcl", cuts = c(2, 4), labeling = "border", label = NULL)

visits <- data.frame(site = c("N", "S"), count = c(60, 40))
ggpie(visits, "site", "count", hole = 0.5, center_label = "100",
      .legend = TRUE, label = "percent")

Text, equations and language

Opt in to inline plotmath with format = "mixed". The renderer measures each equation on the drawing device and places it on the paragraph baseline. Markdown remains the default for styled labels without inline equations.

ggplot(mtcars, aes(wt, mpg)) + geom_point() +
  labs(title = "**Fuel economy** and $hat(beta)$") +
  theme(plot.title = element_markup(format = "mixed"))

terms <- data.frame(variable = "site", id = c("N", "S"),
                    lang = "fr", label = c("Nord", "Sud"))
ggpie(visits, "site", "count", .legend = TRUE) +
  locale_forma("fr", dictionary = terms)

Choose theme_forma(preset = "journal") for compact print figures or match mode = "dark" on both theme and scale for slides. Use font_check() for the actual text and local family and forma_device_info() for the active device. Windows RStudio users can select the AGG graphics backend for positioned glyphs.

Scope and acknowledgements

The package uses a documented subset of Markdown, inline HTML/CSS and plotmath. Complex scripts require suitable installed fonts. Glyph searchability and font embedding vary by device and document reader. The source package passed checks on Windows, Linux and macOS. Native-reader review of the language samples is pending. The optional ggplotly conversion exposes a smaller set of layout and typography controls.

The MIT-licensed package builds on ggplot2, grid, systemfonts and textshaping and uses vcd’s public conditional simulation interface for optional calibrated shading. vcd informed the Mosaic design. ggtext and ggtypst informed the review of rich text and mathematical graphics. Included Unicode locale data have separate attribution and terms in the installed COPYRIGHTS and UNICODE-LICENSE files. The maintainer supplied the double-cat logo.

Open the Getting started vignette locally, followed by the articles on Mosaic, Pie, typography, multilingual visualization, themes and graphics devices.

vignette("ggforma", package = "ggforma")