Add text labels with geom_text


This document is dedicated to text annotation with ggplot2. It provides several examples showing how to use function like geom_label and geom_text.

Adding text with geom_text()


This example demonstrates how to use geom_text() to add text as markers. It works pretty much the same as geom_point(), but add text instead of circles. A few arguments must be provided:

  • label: what text you want to display
  • nudge_x and nudge_y: shifts the text along X and Y axis
  • check_overlap tries to avoid text overlap. Note that a package called ggrepel extends this concept further

Add labels with geom_label()


geom_label() works pretty much the same way as geom_text(). However, text is wrapped in a rectangle that you can customize (see next example).

Add one text label only


Of course, you don’t have to label all dots on the chart. You can also add a piece of text on a specific position. Since we’re here, note that you can custom the annotation of geom_label with label.padding, label.size, color and fill as described below:

Add labels for a selection of marker


Last but not least, you can also select a group of marker and annotate them only. Here, only car with mpg > 20 and wt > 3 are annotated thanks to a data filtering in the geom_label() call.




Contact

This document is a work of the statistics team in the Biostatistics and Medical Information Department at Saint-Louis Hospital in Paris (SBIM).
Based on The R Graph Gallery by Yan Holtz.

SBIM