How to annotate a plot in ggplot2


Once your chart is done, annotating it is a crucial step to make it more insightful. This post will guide you through the best practices using R and ggplot2.

Adding text with geom_text() or geom_label()


Text is the most common kind of annotation. It allows to give more information on the most important part of the chart.

Using ggplot2, 2 main functions are available for that kind of annotation:

Note that the annotate() function is a good alternative that can reduces the code length for simple cases.

Add shapes with annotate()


The annotate() function allows to add all kind of shape on a ggplot2 chart. The first argument will control what kind is used: rect or segment for rectangle, segment or arrow.

Add ablines with geom_hline() and geom_vline()


An abline is a segment that goes from one chart extremity to the other. ggplot2 offers the geom_hline() and geom_vline() functions that are dedicated to it.

Add a point and a range with.. pointrange()


Last kind of annotation, add a dot and a segment directly with pointrange().




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