ggtext/ 0000755 0001762 0000144 00000000000 13766621302 011562 5 ustar ligges users ggtext/NAMESPACE 0000644 0001762 0000144 00000000562 13764527133 013011 0 ustar ligges users # Generated by roxygen2: do not edit by hand
S3method(element_grob,element_markdown)
S3method(element_grob,element_textbox)
export(GeomRichText)
export(GeomRichtext)
export(GeomTextBox)
export(element_markdown)
export(element_textbox)
export(element_textbox_simple)
export(geom_richtext)
export(geom_textbox)
import(ggplot2)
import(grid)
import(gridtext)
import(rlang)
ggtext/README.md 0000644 0001762 0000144 00000024037 13764505404 013051 0 ustar ligges users
ggtext: Improved text rendering support for ggplot2
===================================================
[](https://github.com/wilkelab/ggtext/actions)
[](https://codecov.io/github/wilkelab/ggtext?branch=master)
[](https://cran.r-project.org/package=ggtext)
[](https://www.tidyverse.org/lifecycle/#maturing)
The ggtext package provides simple Markdown and HTML rendering for
ggplot2. Under the hood, the package uses the
[gridtext](https://CRAN.R-project.org/package=gridtext) package for the
actual rendering, and consequently it is limited to the feature set
provided by gridtext.
Support is provided for Markdown both in theme elements (plot titles,
subtitles, captions, axis labels, legends, etc.) and in geoms (similar
to `geom_text()`). In both cases, there are two alternatives, one for
creating simple text labels and one for creating text boxes with word
wrapping.
Importantly, the gridtext package that provides the rendering support
**implements only an extremely limited subset of Markdown/HTML/CSS.** It
currently can make text bold or italics, can change the font, color, or
size of a piece of text, can place text as sub- or superscript, and has
extremely rudimentary image support. No other features are currently
supported. As a general rule, any Markdown, HTML, or CSS feature that
isn’t shown in any of the ggtext or gridtext documentation likely
doesn’t exist.
Installation
------------
You can install the latest stable release from CRAN via
`install.packages()`:
install.packages("ggtext")
To install the latest development version of this package, please run
the following line in your R console:
remotes::install_github("wilkelab/ggtext")
Markdown in theme elements
--------------------------
The ggtext package defines two new theme elements, `element_markdown()`
and `element_textbox()`. Both behave similarly to `element_text()` but
render the provided text as markdown/html. `element_markdown()` is meant
as a direct replacement for `element_text()`, and it renders text
without word wrapping. To start a new line, use the `
` tag or add
two spaces before the end of a line.
As an example, we can mix regular, italics, and bold text, and we can
also apply colors to axis tick labels. This particular example was
inspired by [this stackoverflow
post.](https://stackoverflow.com/questions/39282293/r-ggplot2-using-italics-and-non-italics-in-the-same-category-label)
library(tidyverse)
library(ggtext)
library(glue)
data <- tibble(
bactname = c("Staphylococcaceae", "Moraxella", "Streptococcus", "Acinetobacter"),
OTUname = c("OTU 1", "OTU 2", "OTU 3", "OTU 4"),
value = c(-0.5, 0.5, 2, 3)
)
data %>% mutate(
color = c("#009E73", "#D55E00", "#0072B2", "#000000"),
name = glue("{bactname} ({OTUname})"),
name = fct_reorder(name, value)
) %>%
ggplot(aes(value, name, fill = color)) +
geom_col(alpha = 0.5) +
scale_fill_identity() +
labs(caption = "Example posted on **stackoverflow.com**
(using made-up data)") +
theme(
axis.text.y = element_markdown(),
plot.caption = element_markdown(lineheight = 1.2)
)

Very basic support for the `
` tag exists, and it can be used, for
example, to employ images as axis labels.
labels <- c(
setosa = "
*I. setosa*",
virginica = "
*I. virginica*",
versicolor = "
*I. versicolor*"
)
ggplot(iris, aes(Species, Sepal.Width)) +
geom_boxplot() +
scale_x_discrete(
name = NULL,
labels = labels
) +
theme(
axis.text.x = element_markdown(color = "black", size = 11)
)

`element_textbox()` offers support for rendering larger amounts of text
that require word wrapping. Unlike `element_markdown()`, it cannot be
used for axis tick labels, and it cannot draw text at arbitrary angles,
only at fixed orientations corresponding to 0, 90, 180, and 270 degrees.
In practice, you will usually want to use `element_textbox_simple()`
instead of `element_textbox()`, as it sets useful defaults for many
parameters not usually defined in ggplot2 themes.
ggplot(mtcars, aes(disp, mpg)) +
geom_point() +
labs(
title = "Fuel economy vs. engine displacement
Lorem ipsum *dolor sit amet,*
consectetur adipiscing elit, **sed do eiusmod tempor incididunt** ut
labore et dolore magna aliqua. Ut enim
ad minim veniam, quis nostrud exercitation ullamco laboris nisi
ut aliquip ex ea commodo consequat.",
x = "displacement (in3)",
y = "Miles per gallon (mpg)
A measure of
the car's fuel efficiency."
) +
theme(
plot.title.position = "plot",
plot.title = element_textbox_simple(
size = 13,
lineheight = 1,
padding = margin(5.5, 5.5, 5.5, 5.5),
margin = margin(0, 0, 5.5, 0),
fill = "cornsilk"
),
axis.title.x = element_textbox_simple(
width = NULL,
padding = margin(4, 4, 4, 4),
margin = margin(4, 0, 0, 0),
linetype = 1,
r = grid::unit(8, "pt"),
fill = "azure1"
),
axis.title.y = element_textbox_simple(
hjust = 0,
orientation = "left-rotated",
minwidth = unit(1, "in"),
maxwidth = unit(2, "in"),
padding = margin(4, 4, 2, 4),
margin = margin(0, 0, 2, 0),
fill = "lightsteelblue1"
)
)

Another example, replacing facet strips with text boxes.
library(cowplot)
ggplot(mpg, aes(cty, hwy)) +
geom_point() +
facet_wrap(~class) +
theme_half_open(12) +
background_grid() +
theme(
strip.background = element_blank(),
strip.text = element_textbox(
size = 12,
color = "white", fill = "#5D729D", box.color = "#4A618C",
halign = 0.5, linetype = 1, r = unit(5, "pt"), width = unit(1, "npc"),
padding = margin(2, 0, 1, 0), margin = margin(3, 3, 3, 3)
)
)

Geoms
-----
The geom `geom_richtext()` provides markdown/html labels. Unlike
`geom_label()`, the labels can be rotated.
df <- tibble(
label = c(
"Some text **in bold.**",
"Linebreaks
Linebreaks
Linebreaks",
"*x*2 + 5*x* + *C**i*",
"Some blue text **in bold.**
And *italics text.*
And some large text."
),
x = c(.2, .1, .5, .9),
y = c(.8, .4, .1, .5),
hjust = c(0.5, 0, 0, 1),
vjust = c(0.5, 1, 0, 0.5),
angle = c(0, 0, 45, -45),
color = c("black", "blue", "black", "red"),
fill = c("cornsilk", "white", "lightblue1", "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 = 2) +
scale_color_identity() +
scale_fill_identity() +
xlim(0, 1) + ylim(0, 1)

Labels without frame or background are also possible.
ggplot(df) +
aes(
x, y, label = label, angle = angle, color = color,
hjust = hjust, vjust = vjust
) +
geom_richtext(
fill = NA, label.color = NA, # remove background and outline
label.padding = grid::unit(rep(0, 4), "pt") # remove padding
) +
geom_point(color = "black", size = 2) +
scale_color_identity() +
xlim(0, 1) + ylim(0, 1)

The geom `geom_textbox()` can draw boxes with word-wrapped text. It does
not support arbitrary rotation angles, only fixed orientations, just
like `element_textbox()`.
df <- tibble(
label = rep("Lorem ipsum dolor **sit amet,** consectetur adipiscing elit,
sed do *eiusmod tempor incididunt* ut labore et dolore magna
aliqua.", 2),
x = c(0, .6),
y = c(1, .6),
hjust = c(0, 0),
vjust = c(1, 0),
orientation = c("upright", "right-rotated"),
color = c("black", "blue"),
fill = c("cornsilk", "white")
)
ggplot(df) +
aes(
x, y, label = label, color = color, fill = fill,
hjust = hjust, vjust = vjust,
orientation = orientation
) +
geom_textbox(width = unit(0.4, "npc")) +
geom_point(color = "black", size = 2) +
scale_discrete_identity(aesthetics = c("color", "fill", "orientation")) +
xlim(0, 1) + ylim(0, 1)

Acknowledgments
---------------
This project is receiving [financial
support](https://www.r-consortium.org/projects/awarded-projects) from
the [R consortium.](https://www.r-consortium.org)
ggtext/man/ 0000755 0001762 0000144 00000000000 13621121126 012322 5 ustar ligges users ggtext/man/geom_textbox.Rd 0000644 0001762 0000144 00000013743 13664751167 015352 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/geom-textbox.R
\name{geom_textbox}
\alias{geom_textbox}
\title{Draw boxes containing text}
\usage{
geom_textbox(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
nudge_x = 0,
nudge_y = 0,
box.padding = unit(c(5.5, 5.5, 5.5, 5.5), "pt"),
box.margin = unit(c(0, 0, 0, 0), "pt"),
box.r = unit(5.5, "pt"),
width = unit(2, "inch"),
minwidth = NULL,
maxwidth = NULL,
height = NULL,
minheight = NULL,
maxheight = NULL,
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link[ggplot2:aes]{aes()}} or
\code{\link[ggplot2:aes_]{aes_()}}. If specified and \code{inherit.aes = TRUE} (the
default), it is combined with the default mapping at the top level of the
plot. You must supply \code{mapping} if there is no plot mapping.}
\item{data}{The data to be displayed in this layer. There are three
options:
If \code{NULL}, the default, the data is inherited from the plot
data as specified in the call to \code{\link[ggplot2:ggplot]{ggplot()}}.
A \code{data.frame}, or other object, will override the plot
data. All objects will be fortified to produce a data frame. See
\code{\link[ggplot2:fortify]{fortify()}} for which variables will be created.
A \code{function} will be called with a single argument,
the plot data. The return value must be a \code{data.frame}, and
will be used as the layer data. A \code{function} can be created
from a \code{formula} (e.g. \code{~ head(.x, 10)}).}
\item{stat}{The statistical transformation to use on the data for this
layer, as a string.}
\item{position}{Position adjustment, either as a string, or the result of
a call to a position adjustment function. Cannot be jointy specified with
\code{nudge_x} or \code{nudge_y}.}
\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are
often aesthetics, used to set an aesthetic to a fixed value, like
\code{colour = "red"} or \code{size = 3}. They may also be parameters
to the paired geom/stat.}
\item{nudge_x, nudge_y}{Horizontal and vertical adjustment to nudge text boxes by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with \code{position}.}
\item{box.padding}{Unit vector of length four specifying the padding
inside the text box.}
\item{box.margin}{Unit vector of length four specifying the margin
outside the text box.}
\item{box.r}{Unit vector of length one specifying the radius of the
box.}
\item{width, height}{Unit values specifying the width and height of
the text box (including margins!). If \code{height = NULL} (the default),
the height is chosen automatically to accommodate all the text.}
\item{minwidth, maxwidth, minheight, maxheight}{Unit values specifying
the minimum and maximum values for \code{width} and \code{height}, respectively.
If set to \code{NULL}, are not enforced.}
\item{na.rm}{If \code{FALSE}, the default, missing values are removed with
a warning. If \code{TRUE}, missing values are silently removed.}
\item{show.legend}{logical. Should this layer be included in the legends?
\code{NA}, the default, includes if any aesthetics are mapped.
\code{FALSE} never includes, and \code{TRUE} always includes.
It can also be a named logical vector to finely select the aesthetics to
display.}
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. \code{\link[ggplot2:borders]{borders()}}.}
}
\value{
A ggplot2 layer that can be added to a plot created with
\code{\link[ggplot2:ggplot]{ggplot2::ggplot()}}.
}
\description{
Draw boxes of defined width and height containing word-wrapped text. Multiple
boxes can be drawn at once. Most styling parameters can be used as aesthetics
and can be applied separately to each text box drawn. The exception is styling
parameters that are specified as grid units (e.g., \code{box.padding} or \code{box.r}),
which can only be specified for all text boxes at once. See examples for details.
}
\section{Aesthetics}{
\code{geom_textbox()} understands the following aesthetics (required
aesthetics are in bold; select aesthetics are annotated):
\itemize{
\item \strong{\code{x}}
\item \strong{\code{y}}
\item \strong{\code{label}}
\item \code{alpha}
\item \code{box.colour} Color of box outline. Overrides \code{colour}.
\item \code{box.size} Width of box outline.
\item \code{colour} Default color of box text and box outline.
\item \code{family}
\item \code{fontface}
\item \code{fill} Default fill color of box background.
\item \code{group}
\item \code{halign} Horizontal alignment of text inside box.
\item \code{hjust} Horizontal alignment of box.
\item \code{lineheight}
\item \code{orientation} One of \code{"upright"}, \code{"left-rotated"},
\code{"right-rotated"}, \code{"inverted"}.
\item \code{size} Default font size of box text.
\item \code{text.colour} Color of box text. Overrides \code{colour}.
\item \code{valign} Vertical alignment of text inside box.
\item \code{vjust} Vertical alignment of box.
}
}
\examples{
library(ggplot2)
df <- data.frame(
label = rep("Lorem ipsum dolor **sit amet,** consectetur adipiscing elit,
sed do *eiusmod tempor incididunt* ut labore et dolore magna
aliqua.", 2),
x = c(0, .6),
y = c(1, .6),
hjust = c(0, 0),
vjust = c(1, 0),
orientation = c("upright", "right-rotated"),
color = c("black", "blue"),
fill = c("cornsilk", "white")
)
ggplot(df) +
aes(
x, y, label = label, color = color, fill = fill,
hjust = hjust, vjust = vjust,
orientation = orientation
) +
geom_textbox(width = unit(0.4, "npc")) +
geom_point(color = "black", size = 2) +
scale_discrete_identity(aesthetics = c("color", "fill", "orientation")) +
xlim(0, 1) + ylim(0, 1)
}
\seealso{
\code{\link[=geom_richtext]{geom_richtext()}}, \code{\link[=element_textbox]{element_textbox()}}
}
ggtext/man/geom_richtext.Rd 0000644 0001762 0000144 00000015062 13764525267 015504 0 ustar ligges users % Generated by roxygen2: do not edit by hand
% Please edit documentation in R/geom-richtext.R, R/geom-textbox.R
\docType{data}
\name{geom_richtext}
\alias{geom_richtext}
\alias{GeomRichText}
\alias{GeomRichtext}
\alias{GeomTextBox}
\title{Richtext labels}
\usage{
geom_richtext(
mapping = NULL,
data = NULL,
stat = "identity",
position = "identity",
...,
nudge_x = 0,
nudge_y = 0,
label.padding = unit(c(0.25, 0.25, 0.25, 0.25), "lines"),
label.margin = unit(c(0, 0, 0, 0), "lines"),
label.r = unit(0.15, "lines"),
na.rm = FALSE,
show.legend = NA,
inherit.aes = TRUE
)
}
\arguments{
\item{mapping}{Set of aesthetic mappings created by \code{\link[ggplot2:aes]{aes()}} or
\code{\link[ggplot2:aes_]{aes_()}}. If specified and \code{inherit.aes = TRUE} (the
default), it is combined with the default mapping at the top level of the
plot. You must supply \code{mapping} if there is no plot mapping.}
\item{data}{The data to be displayed in this layer. There are three
options:
If \code{NULL}, the default, the data is inherited from the plot
data as specified in the call to \code{\link[ggplot2:ggplot]{ggplot()}}.
A \code{data.frame}, or other object, will override the plot
data. All objects will be fortified to produce a data frame. See
\code{\link[ggplot2:fortify]{fortify()}} for which variables will be created.
A \code{function} will be called with a single argument,
the plot data. The return value must be a \code{data.frame}, and
will be used as the layer data. A \code{function} can be created
from a \code{formula} (e.g. \code{~ head(.x, 10)}).}
\item{stat}{The statistical transformation to use on the data for this
layer, as a string.}
\item{position}{Position adjustment, either as a string, or the result of
a call to a position adjustment function. Cannot be jointy specified with
\code{nudge_x} or \code{nudge_y}.}
\item{...}{Other arguments passed on to \code{\link[ggplot2:layer]{layer()}}. These are
often aesthetics, used to set an aesthetic to a fixed value, like
\code{colour = "red"} or \code{size = 3}. They may also be parameters
to the paired geom/stat.}
\item{nudge_x}{Horizontal and vertical adjustment to nudge labels by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with \code{position}.}
\item{nudge_y}{Horizontal and vertical adjustment to nudge labels by.
Useful for offsetting text from points, particularly on discrete scales.
Cannot be jointly specified with \code{position}.}
\item{label.padding}{Amount of padding around label. Defaults to 0.25 lines.}
\item{label.margin}{Unit vector of length four specifying the margin
outside the text label.}
\item{label.r}{Radius of rounded corners. Defaults to 0.15 lines.}
\item{na.rm}{If \code{FALSE}, the default, missing values are removed with
a warning. If \code{TRUE}, missing values are silently removed.}
\item{show.legend}{logical. Should this layer be included in the legends?
\code{NA}, the default, includes if any aesthetics are mapped.
\code{FALSE} never includes, and \code{TRUE} always includes.
It can also be a named logical vector to finely select the aesthetics to
display.}
\item{inherit.aes}{If \code{FALSE}, overrides the default aesthetics,
rather than combining with them. This is most useful for helper functions
that define both data and aesthetics and shouldn't inherit behaviour from
the default plot specification, e.g. \code{\link[ggplot2:borders]{borders()}}.}
}
\value{
A ggplot2 layer that can be added to a plot created with
\code{\link[ggplot2:ggplot]{ggplot2::ggplot()}}.
}
\description{
This geom draws text labels similar to \code{\link[ggplot2:geom_text]{ggplot2::geom_label()}}, but formatted
using basic markdown/html. Parameter and aesthetic names follow the conventions
of \code{\link[ggplot2:geom_text]{ggplot2::geom_label()}}, and therefore the appearance of the frame around
the label is controlled with \code{label.colour}, \code{label.padding}, \code{label.margin},
\code{label.size}, \code{label.r}, even though the same parameters are called \code{box.colour},
\code{box.padding}, \code{box.margin}, \code{box.size}, and \code{box.r} in \code{\link[=geom_textbox]{geom_textbox()}}. Most
styling parameters can be used as aesthetics and can be applied separately to
each text label drawn. The exception is styling parameters that are specified
as grid units (e.g., \code{label.padding} or \code{label.r}), which can only be specified
for all text labels at once. See examples for details.
}
\section{Aesthetics}{
\code{geom_richtext()} understands the following aesthetics (required
aesthetics are in bold; select aesthetics are annotated):
\itemize{
\item \strong{\code{x}}
\item \strong{\code{y}}
\item \strong{\code{label}}
\item \code{alpha}
\item \code{angle}
\item \code{colour} Default color of label text and label outline.
\item \code{family}
\item \code{fontface}
\item \code{fill} Default fill color of label background.
\item \code{group}
\item \code{hjust}
\item \code{label.colour} Color of label outline. Overrides \code{colour}.
\item \code{label.size} Width of label outline.
\item \code{lineheight}
\item \code{size} Default font size of label text.
\item \code{text.colour} Color of label text. Overrides \code{colour}.
\item \code{vjust}
}
}
\examples{
library(ggplot2)
df <- data.frame(
label = c(
"Some text **in bold.**",
"Linebreaks
Linebreaks
Linebreaks",
"*x*2 + 5*x* + *C**i*",
"Some blue text **in bold.**
And *italics text.*
And some large text."
),
x = c(.2, .1, .5, .9),
y = c(.8, .4, .1, .5),
hjust = c(0.5, 0, 0, 1),
vjust = c(0.5, 1, 0, 0.5),
angle = c(0, 0, 45, -45),
color = c("black", "blue", "black", "red"),
fill = c("cornsilk", "white", "lightblue1", "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 = 2) +
scale_color_identity() +
scale_fill_identity() +
xlim(0, 1) + ylim(0, 1)
# labels without frame or background are also possible
ggplot(df) +
aes(
x, y, label = label, angle = angle, color = color,
hjust = hjust, vjust = vjust
) +
geom_richtext(
fill = NA, label.color = NA, # remove background and outline
label.padding = grid::unit(rep(0, 4), "pt") # remove padding
) +
geom_point(color = "black", size = 2) +
scale_color_identity() +
xlim(0, 1) + ylim(0, 1)
}
\seealso{
\code{\link[=geom_textbox]{geom_textbox()}}, \code{\link[=element_markdown]{element_markdown()}}
}
\keyword{datasets}
ggtext/man/figures/ 0000755 0001762 0000144 00000000000 13764505403 014002 5 ustar ligges users ggtext/man/figures/README-unnamed-chunk-7-1.png 0000644 0001762 0000144 00000134043 13764505402 020506 0 ustar ligges users PNG
IHDR z4 iCCPkCGColorSpaceGenericRGB 8U]hU>sg#$Sl4t?
%
V46nI6"dΘ83OEP|1Ŀ (>/
% (>P苦;3ie|{g蹪X-2s=+WQ+]L6Ow[C{_F qbUvz?Zb1@/zcs>~if,ӈUSjF1_Mjbuݠpamhmçϙ>a\+5%QKFkm}ۖ?ޚD\!~6,-7SثŜvķ5Z;[rmS5{yDyH}r9|-ăFAJjI.[/]mK7KRDrYQO-Q||6
(0
MXd(@h2_f<:_δ*d>e\c?~,7?& ك^2Iq2"y