Learn R Programming

ggpubr (version 0.1.1)

ggtext: Text

Description

Add text to a plot.

Usage

ggtext(data, x, y, color = "black", palette = NULL, size = 2, point = TRUE, label = NULL, font.label = c(12, "plain"), label.select = NULL, repel = FALSE, label.rectangle = FALSE, ggp = NULL, ggtheme = theme_classic2(), ...)

Arguments

data
a data frame
x, y
x and y variables for drawing.
color
point colors.
palette
the color palette to be used for coloring or filling by groups. Allowed values include "grey" for grey color palettes; brewer palettes e.g. "RdBu", "Blues", ...; or custom color palette e.g. c("blue", "red"); and scientific journal palettes from ggsci R package, e.g.: "npg", "aaas", "lancet", "jco", "ucscgb", "uchicago", "simpsons" and "rickandmorty".
size
Numeric value (e.g.: size = 1). change the size of points and outlines.
point
logical value. If TRUE, show points.
label
the name of the column containing point labels. Can be also a character vector with length = nrow(data).
font.label
a vector of length 3 indicating respectively the size (e.g.: 14), the style (e.g.: "plain", "bold", "italic", "bold.italic") and the color (e.g.: "red") of point labels. For example font.label = c(14, "bold", "red"). To specify only the size and the style, use font.label = c(14, "plain").
label.select
character vector specifying some labels to show.
repel
a logical value, whether to use ggrepel to avoid overplotting text labels or not.
label.rectangle
logical value. If TRUE, add rectangle underneath the text, making it easier to read.
ggp
a ggplot. If not NULL, points are added to an existing plot.
ggtheme
function, ggplot2 theme name. Default value is theme_pubr(). Allowed values include ggplot2 official themes: theme_gray(), theme_bw(), theme_minimal(), theme_classic(), theme_void(), ....
...
other arguments to be passed to geom_point and ggpar.

Details

The plot can be easily customized using the function ggpar(). Read ?ggpar for changing:
  • main title and axis labels: main, xlab, ylab
  • axis limits: xlim, ylim (e.g.: ylim = c(0, 30))
  • axis scales: xscale, yscale (e.g.: yscale = "log2")
  • color palettes: palette = "Dark2" or palette = c("gray", "blue", "red")
  • legend title, labels and position: legend = "right"

See Also

ggpar

Examples

Run this code
# Load data
data("mtcars")
df <- mtcars
df$cyl <- as.factor(df$cyl)
df$name <- rownames(df)
head(df[, c("wt", "mpg", "cyl")], 3)

# Textual annotation
# +++++++++++++++++
ggtext(df, x = "wt", y = "mpg",
   color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
   label = "name", repel = TRUE)

# Add rectangle around label
ggtext(df, x = "wt", y = "mpg",
   color = "cyl", palette = c("#00AFBB", "#E7B800", "#FC4E07"),
   label = "name", repel = TRUE,  label.rectangle = TRUE)


Run the code above in your browser using DataLab