# Annotate Text Outside of ggplot2 Plot in R (Example)

In this tutorial, I’ll explain how to add text outside of the plot area of a ggplot2 graph in R.

The tutorial looks as follows:

Here’s the step-by-step process…

## Example Data, Packages & Basic Graphic

We use the following data as basement for this R programming tutorial:

```data <- data.frame(x = 1:5, # Example data frame y = 1:5) data # Inspect data frame # x y # 1 1 1 # 2 2 2 # 3 3 3 # 4 4 4 # 5 5 5```

The previous output of the RStudio console shows that our example data contains two simple numeric variables.

We also need to install and load the ggplot2 package, if we want to use the functions that are included in the package:

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

As next step, we can plot our data:

```ggp <- ggplot(data, aes(x, y, label = "my text")) + # Create ggplot2 plot geom_point() + geom_text(x = 5.5, size = 5) ggp # Draw ggplot2 plot```

As shown in Figure 1, the previous R programming code created a typical ggplot2 scatterplot. However, you can also see that our text was not annotated to the plot.

## Example: Increasing Margins & Allow Text Outside of Plot

In this Example, I’ll explain how to add text elements at the right side of the plot borders using the coord_cartesian function. Have a look at the following R code:

```ggp + # Annotate text theme(plot.margin = unit(c(1, 5, 1, 1), "lines")) + coord_cartesian(clip = "off")```

As shown in Figure 2, the previous code created a ggplot2 graph with multiple text elements outside the plotting area.

## Video, Further Resources & Summary

Would you like to learn more about the annotation of text elements? Then you might watch the following video of my YouTube channel. In the video, I’m explaining the R programming codes of this article.

Furthermore, you may read the related articles of https://statisticsglobe.com/:

In this R tutorial you learned how to annotate a character string outside of a ggplot2 plot. If you have any further questions, please let me know in the comments section below.