Skip to content
Improved text rendering support for grid graphics in R
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
R
man
tests
.Rbuildignore
.gitignore
DESCRIPTION
ISSUE_TEMPLATE.md
LICENSE
NAMESPACE
NEWS
README.Rmd
README.md
TODO
gridtext.Rproj

README.md

gridtext

Improved text rendering support for grid graphics in R, written by Claus O. Wilke

This is an experiment. Most features are broken. The API is not stable. Please do not use this in any context where you need your code to work. No user support will be provided.

Installation

devtools::install_github("clauswilke/gridtext")

Examples

Labels grob

The function labels_grob() serves as a replacement for textGrob(). It is vectorized and can draw multiple labels with one call. Labels can be drawn with padding, margins, and at arbitrary angles. Graphical parameters are provided as a data frame/tibble for simplicity and efficiency.

library(grid)
library(gridtext)
library(tibble)

label_data <- tibble(
  label = c("Descenders: pgqjy", "This is a label\nwith two lines", "Hello!"),
  x = unit(c(.3, .8, .5), "npc"),
  y = unit(c(.9, .5, .3), "npc"),
  box_hjust = 0,
  box_vjust = 0.5,
  hjust = 1,
  vjust = 1,
  angle = c(0, 45, -45),
  color = "blue",
  fill = "azure1",
  fontsize = 10,
  fontfamily = "Comic Sans MS",
  padding = list(mar(5, 5, 3, 5)),
  margin = list(mar(5, 5, 5, 5))
)

grid.newpage()
g <- labels_grob(label_data)
grid.draw(g)

Also, the boxes enclosing the labels can be all made the same size by setting align_frames = TRUE. This may be useful when using this grob to provide labels for an x or y axis:

label_data <- tibble(
  label = c("This", "is", "an", "example", "rotated", "x axis"),
  x = unit(.15*1:6, "npc"),
  y = unit(0.8, "npc"),
  box_hjust = 1,
  box_vjust = 0.5,
  hjust = 0.5,
  vjust = 1,
  angle = 45,
  fontsize = 10, fontfamily = "Comic Sans MS",
  padding = list(mar(5, 5, 3, 5)),
  margin = list(mar(5, 5, 5, 5))
)
grid.newpage()
g <- labels_grob(label_data, align_frames = TRUE, debug = TRUE)
grid.draw(g)

(The setting debug = TRUE shows the margin, padding, and the reference point used for rotation.)

The same example without aligned frames:

grid.newpage()
g <- labels_grob(label_data, debug = TRUE)
grid.draw(g)

Rich-text grob

The function rich_text_grob() can be used to draw simple html-like text. It is currently not vectorized, but the ultimate goal is to write a variant of labels_grob() that can make use of it.

library(grid)
library(gridtext)
text1 <- "<span style='font-family:\"Comic Sans MS\"; font-size:20; color:orange'>Comic Sans! Yay!</span>"
text2 <- "Some <span style='color:red'>red</span> text <b>in bold.</b><br>And <i>more</i> text.<br>And some <span style='font-size:18'>large</span> text."
text3 <- "5<i>x</i><sup>2</sup><span style='color:blue'> + 7<i>x</i></span> - <i>α<sub>i</sub></i>"

grid.newpage()
grid.draw(rich_text_grob(text1, x = 0.25, y = 0.9))
grid.draw(rich_text_grob(text2, x = 0.3, y = 0.3, hjust = 0, angle = 45))
grid.draw(rich_text_grob(text2, x = 0.8, y = 0.7, hjust = 1, angle = -90))
grid.draw(rich_text_grob(text3, x = 0.1, y = 0.1))

You can’t perform that action at this time.