GG_text: Add text to a ggplot

View source: R/plotting.R

GG_textR Documentation

Add text to a ggplot

Description

Convenient function to add text to a ggplot object

Usage

GG_text(text, text_pos = "tl", font_size = 11, font_color = "black", ...)

Arguments

text

(chr) A character scalar with text to use.

text_pos

(chr) An abbreviation of the easy placement to use. If you want to set these manually, use "manual".

font_size

(int) Font size.

font_color

(various) Font color. Can be of various types, see details.

...

Extra arguments passed to various functions, see details.

Details

Builds a grob using grid::grobTree() and grid::textGrob(). This text can then be positioned wherever the user wants it. It is more convenient than using ggplot2::geom_text() because positioning is difficult to get right with that as it depends on the data ranges. This function instead uses fractional positioning, e.g. .95 is the 95th centile of x/y, whatever that happens to be in the data. On top of this, 6 premade positions are made for common placements: top/bottom left/middle/right abbreviated as tl, tm, tr, bl, bm, br, respectively.

Value

The outfrom from ggplot2::annotation_custom().

Examples

#make a plot with a point at 0,0, and large red text in middle
ggplot(tibble(x = 0, y = 0), aes(x, y)) +
  geom_point() +
  GG_text("red test", text_pos = "mm", font_color = "red", font_size = 20)

#manually making the text position
ggplot(tibble(x = 0, y = 0), aes(x, y)) +
  geom_point() +
  GG_text("blue test", text_pos = "manual", font_color = "blue", font_size = 20, x = .25, y = .75)

Deleetdk/kirkegaard documentation built on May 8, 2024, 12:27 a.m.