GG_text | R Documentation |
Convenient function to add text to a ggplot object
GG_text(text, text_pos = "tl", font_size = 11, font_color = "black", ...)
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. |
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.
The outfrom from ggplot2::annotation_custom()
.
#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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.