Text Annotations

Introduction

Agenda


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

Overview

Introduction


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

Introduction


Example


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

Instructions


  • add 'New Text' to the plot
  • set x coordinate to 300
  • set y coordinate to 40
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = ___, y = ___, label = _______)
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'New Text')

Color

Introduction


Example


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

Instructions


  • add 'New Text' to the plot
  • set x coordinate to 300
  • set y coordinate to 40
  • set color to 'blue'
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = ___, y = ___, label = _______, color = _____)
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', color = 'blue')

Size

Introduction


Example


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

Instructions


  • add 'New Text' to the plot
  • set x coordinate to 300
  • set y coordinate to 40
  • set size to 9
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = ___, y = ___, label = _______, size = ___)
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', size = 9)

Font

Introduction


Example


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

Instructions


  • add 'New Text' to the plot
  • set x coordinate to 300
  • set y coordinate to 40
  • set font to 'italic'
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = ___, y = ___, label = _______, fontface = ___)
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', fontface = 'italic')

Angle

Introduction


Example


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

Instructions


  • add 'New Text' to the plot
  • set x coordinate to 300
  • set y coordinate to 40
  • set angle to '45'
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = ___, y = ___, label = _______, angle = ___)
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'Sample Text', angle = 25)

Summary

Introduction


Instructions


  • set label to ‘New Text’
  • set x coordinate to 400
  • set y coordinate to 40
  • set color to 'green'
  • set size to 8
  • set fontface to 'bold'
  • set angle to 40
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = __, y = ___, label = ___________,
           color = _____, size = ____, fontface = ____, angle = ___)
ggplot(mtcars) +
  geom_point(aes(disp, mpg)) +
  annotate('text', x = 200, y = 30, label = 'New Text',
           color = 'green', size = 8, fontface = 'bold', angle = 40)