Working with Labels and Annotations



July 26, 2022

View slides in full screen | Run the code yourself


  • Labels + theme()
    • Working with Labels
    • Customize Labels via theme()
  • Labels + scale_*()
    • Format Labels via scale_*
  • Labels + ggtext::element_markdown
    • Styling Labels with {ggtext}
  • Labels + facet_*()
    • Facet Labellers
  • Handling Long Labels
    • Handling Long Labels with {stringr}
    • Handling Long Labels with {ggtext}
    • Handling Long Titles
  • Annotations with annotate()
    • Add Single Text Annotations
    • Style Text Annotations
    • Add Multiple Text Annotations
    • Add Boxes (Rectangles)
    • Add Lines (Segments)
    • Add Curves
    • Add Arrows
  • Annotations with geom_*()
    • Highlight Hot Periods
    • Annotations with geom_text()
    • Annotations with geom_label()
    • Annotations with {ggrepel}
    • Annotations with {ggforce}
  • Adding Images
    • Load and Modify Image
    • Add Background Image
    • Adjust Position
    • Place Image Outside


  • style labels such as title, axis and legend texts with theme()
  • format data-related labels with the labels argument of scale_*()
  • adjust strip text with the facet_*(labeller) functionality
  • add data-related annotations with geom_text|label()
  • … and data-unrelated annotations with annotate()
  • annotation_custom(rasterGrob()) is a basic way to add images
  • {ggtext} allows to render labels with markdown and basic html
  • {ggtext} also allows to add dynamic linebreaks and images
  • {ggrepel} ensures clever placement of annotations
  • ggforce::geom_mark_*() provide a set of advanced annotations


Exercise 1

  • {ggtext} also comes with some new geom’s. Explore those and other options on the package webpage: wilkelab.rg/ggtext.
  • Open the script exercises/05_labels_ex1.qmd (or the .rmd).
  • Create the following visualization, as close as possible, with the penguins dataset which is provided by the {palmerpenguins} package:

    • For the species labels, you likely have to create a summary data set.
    • Use the {ggtext} geometries and theme elements to format the labels.
    • Also, make use of the other components such as scales, complete themes, and theme customization.

→ Solution

Exercise 2

  • Open the script exercises/05_labels_ex2.qmd (or the .rmd).
  • Create the logo in exercises/5-2-logo with the image file exercises/img/rstudioconf-washington-bg.png for the skyline:

→ Solution