The ggtext
package in R is an
extension of ggplot2,
designed to improve text rendering in plots.
This
post showcases the key features of ggtext
and provides a set of graph examples using the
package.
{ggtext}
The ggtext
package in R extends the text rendering
capabilities of ggplot2,
allowing for rich text formatting in plot elements.
It provides functions to render markdown and HTML text in ggplot2 plots, enabling more flexible and visually appealing text elements.
✍️ author → Claus O. Wilke
📘 documentation → github
⭐️ more than 600 stars on github
To get started with ggtext
, you can install it from CRAN
using the install.packages
function:
The ggtext
package allows you to use markdown and HTML
formatting in text elements of ggplot2 plots. Here’s a basic
example:
In this example, we use HTML tags (span
) in the subtitle
to color specific words. The element_markdown()
function in
the theme()
call enables the rendering of this formatted
text.
You can use markdown and HTML to format text, including bold, italic, and colored text.
Example:
library(ggplot2)
library(ggtext)
ggplot(mtcars, aes(wt, mpg)) +
geom_point() +
labs(
title = "Car Weight vs. MPG",
subtitle = "**Bold text**, *italic text*, and <span style='color:#72874EFF;'>**colored text**</span>"
) +
theme(plot.subtitle = element_markdown())
ggtext
as a legendUsing ggtext
, we can change the axis labels so that they
are colored to match the bar colors:
color
defines bar and text colors.name
combines item_name
and
group
with HTML tags for styling:
<i style='color:{color}'>**{item_name}**</i> ({group})
.axis.text.y = element_markdown()
applies HTML/CSS
formatting to y-axis labels.This has direct benefits:
library(tidyverse)
library(ggtext)
library(glue)
data <- tibble(
item_name = c("Item A", "Item B", "Item C", "Item D"),
group = c("Group 1", "Group 2", "Group 3", "Group 4"),
value = c(-1.5, 1.0, 2.5, 4.0)
)
data %>%
mutate(
color = c("#FF5733", "#33FF57", "#3357FF", "#FF33FF"),
name = glue("<i style='color:{color}'>**{item_name}**</i> ({group})"),
name = fct_reorder(name, value)
) %>%
ggplot(aes(value, name, fill = color)) +
geom_col(alpha = 0.5) +
scale_fill_identity() +
labs(caption = "Adapted example from the ***{ggtext}*** documentation<br>(using made-up data)") +
theme(
axis.text.y = element_markdown(),
plot.caption = element_markdown(lineheight = 1.2)
)
ggtext
can be used to create boxes around your text in
different styles:
geom_richtext()
: Replaces the usual
geom_text()
to render the text in label
with
special formatting.label
entries use:
**bold**
for bold text.<br>
for line breaks.<span style='color:green'>
for colored text.<sup>
and <sub>
for
superscript and subscript.<span style='font-size:16pt'>
for larger
text.This setup allows the plot to display text with mixed styles (e.g., bold, colored, italicized) and structured formats (e.g., line breaks, superscripts) within a single text label.
library(ggplot2)
library(tibble)
library(ggtext)
df <- tibble(
label = c(
"Another text **in bold.**",
"Different linebreaks<br>Linebreaks<br><br>Double Linebreaks",
"*y*<sup>3</sup> + 7*y* + *D*<sub>*j*</sub>",
"More <span style='color:green'>green text **in bold.**</span><br>And *italicized text.*<br>
And some <span style='font-size:16pt; color:purple'>large</span> text."
),
x = c(.3, .2, .6, .8),
y = c(.7, .3, .2, .4),
hjust = c(0.5, 0, 1, 0.5),
vjust = c(0.5, 1, 0.5, 0.5),
angle = c(0, 10, 30, -30),
color = c("black", "green", "black", "purple"),
fill = c("ivory", "white", "lightgreen", "white")
)
ggplot(df) +
aes(
x, y,
label = label, angle = angle, color = color, fill = fill,
hjust = hjust, vjust = vjust
) +
geom_richtext() +
geom_point(color = "black", size = 1) +
scale_color_identity() +
scale_fill_identity() +
xlim(0, 1) +
ylim(0, 1)
Unfortunatley, ggtext
only supports a limited
amount of features. Only the following will work:
**
→
**some text that will renders bold***
→
*some text that will renders italic*<span>
with a
color
value in style
→ <span
style=‘color:green’>green text</span><span>
with a
font-size
value in style
→ <span
style=‘font-size:30px’>large text</span><sub>
or
<sup>
→ <sub>subscript</sub> and
<sup>subscript</sup>If you want access to more text formatting functions, we strongly recommend that you check out and use the brand new marquee package.
The gallery is full of examples that use ggtext
to
improve their style. If you want to delve into some real-life
examples, take a look at them!
👋 After crafting hundreds of R charts over 12 years, I've distilled my top 10 tips and tricks. Receive them via email! One insight per day for the next 10 days! 🔥