Table of Contents
Adding text outside of a plot in R involves using the “text” function to place text at specified coordinates in the plot. This can be done by specifying the x and y coordinates, as well as the text to be added. Additionally, the “mtext” function can be used to add text in the margins of the plot. It is important to note that the text added outside of the plot will not be affected by any changes made to the plot, such as zooming or resizing. This feature is useful for labeling the plot or providing additional information to the viewer.
Add Text Outside of a Plot in R
You can use the following basic syntax to add text outside of a plot in R:
text(x=8, y=-0.5, 'Some Text', xpd=NA)
This particular example adds the text ‘Some Text’ to the (x, y) location of (8, -0.5).
Note that the xpd argument takes on three potential values for where to place your text:
- FALSE: Inside the plot only
- TRUE: In the outer plotting area
- NA: Anywhere on plotting device
By specifying xpd=NA, we’re able to add text outside of our plot.
The following examples show how to use this syntax in practice.
Example 1: Add One Text Element Outside of Plot
The following code shows how to add one text element outside of the plot in the bottom right corner:
#define variables x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) y <- c(4, 5, 5, 4, 6, 8, 12, 15, 19, 22) #create scatterplot plot(x, y) #add text outside of plot text(x=8, y=-0.5, 'Some Text', xpd=NA)

Notice that our text element has been added to the (x, y) coordinates of (8, -0.5) in the plot.
Since the value for the y-coordinate is less than the lower limit of the y-axis, the text element ends up being placed below the plot.
Example 2: Add Multiple Text Elements Outside of Plot
The following code shows how to add multiple text elements outside of the plot by using the text() function multiple times:
#define variables x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) y <- c(4, 5, 5, 4, 6, 8, 12, 15, 19, 22) #create scatterplot plot(x, y) #add multiple text elements outside of plot text(x=8, y=-0.5, 'Below Plot', xpd=NA) text(x=8, y=25, 'Above Plot', xpd=NA)

Feel free to play around with the x and y arguments within the text() function to place text in the exact position you’d like.
Cite this article
stats writer (2024). How can I add text outside of a plot in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-add-text-outside-of-a-plot-in-r/
stats writer. "How can I add text outside of a plot in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-add-text-outside-of-a-plot-in-r/.
stats writer. "How can I add text outside of a plot in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-add-text-outside-of-a-plot-in-r/.
stats writer (2024) 'How can I add text outside of a plot in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-add-text-outside-of-a-plot-in-r/.
[1] stats writer, "How can I add text outside of a plot in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can I add text outside of a plot in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
