Agenda


  • add custom text
  • modify color
  • modify size
  • modify fontface
  • modify angle

Libraries


library(ggplot2)

Annotate


Use annotate() to add custom text to a plot. It takes the following arguments:

  • geom : specify text
  • x : x axis location
  • y : y axis location
  • label : custom text
  • color : color of text
  • size : size of text
  • fontface : fontface of text
  • angle : angle of text

Add Text


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text')

Color


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', color = 'red')

Size


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', size = 6)

Font


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', fontface = 'bold')

Angle


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', angle = 25)

Annotate


ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text',
           color = 'red', size = 6, fontface = 'bold', angle = 25)