Annotate Multiple Lines of Text to ggplot2 Plot in R (Example)


In this R programming tutorial you’ll learn how to add a text element with two or more lines to a ggplot2 graph.

The article consists of this information:

Let’s jump right to the example!


Example Data, Packages & Basic Graphic

The first step is to create some data that we can use in the following examples:

data <- data.frame(x = 8:3,         # Example data
                   y = 4:9)
data                                # Print example data


table 1 data frame annotate multiple lines text ggplot2 r


Have a look at the previous table. It shows that the example data contains six rows and two columns.

For the following tutorial, I’ll also need to install and load the ggplot2 package:

install.packages("ggplot2")         # Install ggplot2 package
library("ggplot2")                  # Load ggplot2

Now, we can create a plot of our data as follows:

ggp <- ggplot(data, aes(x, y)) +    # Create ggplot2 plot without text
ggp                                 # Draw ggplot2 plot without text


r graph figure 1 annotate multiple lines text ggplot2 r


As shown in Figure 1, the previous code has created a scatterplot created by the ggplot2 add-on package. At this point, we have not added any text elements.


Example: Annotate Multiple Lines of Text to ggplot2 Plot Using “\n”

This example explains how to add a text element in multiple lines to our ggplot2 graphic.

First, I’ll show how to annotate text in one line to a ggplot2 scatterplot using the annotate function:

ggp +                               # Add text in one line
           x = 4.5,
           y = 6,
           label = "This is my text!")


r graph figure 2 annotate multiple lines text ggplot2 r


Figure 2 illustrates the output of the previous R syntax – A ggplot2 plot with one line of text.

Now, we can add the “\n” separator at each position of our text label, where we want to start a new line. Have a look at the following R code:

ggp +                               # Add text in multiple lines
           x = 4.5,
           y = 6,
           label = "This is\nmy text!")


r graph figure 3 annotate multiple lines text ggplot2 r


In Figure 3 you can see that we have created a ggplot2 graph containing a text element with several lines.


Video & Further Resources

Do you need further information on the topics of this article? Then you may want to have a look at the following video of my YouTube channel. In the video, I’m explaining the topics of this tutorial in the R programming language:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


Furthermore, you may want to read the other posts on this homepage. I have published several tutorials about topics such as variables, text elements, ggplot2, and graphics in R:


In summary: In this R programming post you have learned how to annotate text in several lines to a ggplot2 plot.

Please note that the “\n” separator can also be used to wrap other text elements such as titles, subtitles, and axis labels over multiple lines.

If you have any further questions, let me know in the comments section. Furthermore, don’t forget to subscribe to my email newsletter for regular updates on new tutorials.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.