Table of Contents
Introduction to the mtext() Function in R
The effective communication of statistical findings hinges on the quality and precision of data visualization. In the R programming environment, where graphical output is central to analysis, the ability to add clean, targeted annotations is paramount. While standard functions handle titles and axis labels, specialized tools are required for placing supplementary text outside the immediate plotting region. This is precisely the role of the mtext() function, a highly versatile component of R’s base graphics system designed for meticulous control over text placement within the plot margins.
The name mtext() is derived from “margin text,” clearly signifying its exclusive focus on the area surrounding the main data visualization. This function grants users the critical capability to add contextual information, source citations, descriptive footnotes, or secondary titles without encroaching upon the visual representation of the data itself. Unlike the main title, which typically resides far from the data, text placed using mtext() remains close enough to the plot to maintain relevance yet distant enough to avoid clutter.
Understanding how to leverage mtext() is fundamental for creating professional-grade statistical graphics. This functionality is particularly vital when dealing with complex visualizations that require multiple layers of explanatory text—such as noting the methodology, the date of data extraction, or specific warnings about interpretation. Throughout this comprehensive guide, we will meticulously examine the core structure of the function, delve into its indispensable placement parameters, and provide practical, reproducible examples demonstrating its full potential for enhancing the clarity and informational richness of your R plots.
Deconstructing the mtext() Syntax and Core Arguments
To master the strategic placement of text in R plots, a thorough understanding of the mtext() syntax is essential. The function is deliberately designed to be simple yet powerful, allowing control over content, placement, and aesthetics through a concise set of arguments. By default, mtext() operates within R’s standard graphical system, ensuring consistent behavior across various plotting commands.
The fundamental structure governing text placement is defined by the following basic syntax:
mtext(text, side=3, line=0, …)
The interaction between the first three arguments—text, side, and line—determines the exact position of the annotation:
- text: This is the mandatory argument specifying the character string that will be rendered in the plot margin. It must be enclosed in quotes and can contain any textual information relevant to the visualization.
- side: This crucial numerical argument controls which of the four plot margins the text will be placed on. The assignments are fixed: 1 corresponds to the bottom (X-axis), 2 to the left (Y-axis), 3 to the top (default position), and 4 to the right margin. Accurate selection of the
sidevalue is the primary step in positioning marginal text. - line: The
lineargument specifies the exact distance of the text from the plotting region, measured in margin lines. Line 0 is the margin line closest to the data. Positive integer values (e.g.,line=1,line=2) push the text further into the exterior margin. Conversely, utilizing negative values (e.g.,line=-1) pulls the text inward, potentially overlapping the main plotting area if a high absolute value is used.
The ellipsis (…) signifies that mtext() seamlessly accepts a wide array of additional graphical parameters that govern appearance. These options grant fine-grained control over the visual presentation of the text. Key among these are cex (character expansion factor for scaling text size), col (color specification), font (font style, such as bold or italic), and adj (adjustment or justification, controlling whether the text is centered, left-justified, or right-justified within its margin). By combining these aesthetic controls with precise positional arguments, users can ensure annotations perfectly complement the main visualization.
Preparatory Steps: Defining Sample Data for Visualization
To effectively illustrate the functionality of mtext(), we must first establish a reproducible data foundation and a basic visualization canvas. For all subsequent examples in this guide, we will employ a straightforward data frame that facilitates the creation of a simple scatterplot. This approach minimizes complexity related to data manipulation, allowing us to maintain a sharp focus exclusively on the placement and customization of marginal text annotations.
Our sample data frame, named df, is constructed using two numeric vectors, x and y, each containing seven hypothetical data points. This structure is archetypal for bivariate analysis and provides an unadorned plotting area. The consistency of this data set across all examples ensures that the visual impact of each mtext() command can be directly compared and understood.
The R code provided below demonstrates the creation of the data frame and its display. You should execute this code in your R console before proceeding to the practical examples, as it establishes the necessary variables for plot generation. The resulting output shows the structure of our simple seven-observation data set, which will be the basis for our visual demonstrations of marginal text placement.
#create data frame df <- data.frame(x=c(1, 2, 3, 4, 5, 6, 7), y=c(3, 4, 4, 8, 6, 10, 14)) #view data frame df x y 1 1 3 2 2 4 3 3 4 4 4 8 5 5 6 6 6 10 7 7 14
Application 1: Placing Primary Annotations Using Default Settings
The most basic and frequently used application of the mtext() function involves adding a single line of text using the function’s default settings. This method is ideal for quick, supplementary notes or for adding a secondary title when the main plot title area is already occupied. This initial example provides a clear understanding of the function’s behavior when minimum arguments are specified.
In the absence of explicit instructions regarding placement, mtext() defaults to using side=3 (the top margin) and line=0 (the line closest to the plot area). Our procedure involves first generating the scatterplot of df$x versus df$y. Immediately following the plot command, we call mtext() and supply only the desired text string. This simple execution results in the annotation being centered on the top margin, consistent with the default behavior of R’s base graphics system.
Review the code snippet below and observe the resulting image. The code generates the visualization, and the subsequent mtext() call places the descriptive text “Here is some text” precisely above the plotting region. This demonstrates the seamless integration of marginal text without requiring complex coordinate specification, making it perfect for adding unobtrusive, quick notes or meta-information to your visualizations.
#create scatterplot plot(df$x, df$y) #add text above plot mtext("Here is some text")

Application 2: Comprehensive Marginal Annotation on All Four Sides
While simple default placement is useful, the true flexibility of mtext() shines when annotating multiple sides of a visualization simultaneously. This capability is essential for plots that require distinct types of contextual information placed strategically in different margins—for instance, source data on the bottom, methodology notes on the left, a secondary title on the top, and data quality flags on the right. Achieving this complexity requires explicitly calling mtext() multiple times within the plotting sequence, utilizing the numerical designation of the side argument for each annotation.
In this application, we begin by generating the same base plot. We then follow this with four sequential calls to mtext(). Each call provides a unique text string and specifies a different value for the side argument: side=1 for the bottom, side=2 for the left, side=3 for the top, and side=4 for the right. This comprehensive annotation strategy demonstrates maximal utilization of the available marginal space around the visualization.
The following code generates the plot and adds the four distinct labels. Notice how this approach allows for precise segmentation of information, preventing any single margin from becoming overly crowded. This technique is invaluable for scientific reporting where comprehensive metadata must accompany the graphical representation of the data.
#create scatterplot
plot(df$x, df$y)
#add text on each side of plot
mtext("Text on the bottom", side=1)
mtext("Text on the left", side=2)
mtext("Text on the top", side=3)
mtext("Text on the right", side=4)

Application 3: Advanced Customization of Text Aesthetics and Position
Moving beyond basic placement, mtext() offers powerful mechanisms for customizing both the appearance and the exact sub-positioning of annotations. These advanced controls, leveraged through the additional graphical parameters (denoted by the ellipsis …), allow the user to achieve highly specific visual effects, such as drawing attention to a particular note or incorporating text directly over the plotting area.
The key to fine-tuned positioning is the line argument. While positive line values push text into the outer margins, using negative values pulls the text inward toward the data. For instance, setting line=-3 on the top margin will place the text significantly below the conventional title area, potentially overlapping the data points themselves. Furthermore, aesthetic parameters like cex, col, and font enable visual distinction: cex controls the character expansion (size), col specifies the color, and font allows selection of styles such as bolding or italics.
The following example demonstrates how to place text conspicuously inside the top portion of the plot. We utilize side=3 (top margin), line=-3 (pulling the text inward), cex=3 (tripling the font size), and col='blue' (setting a distinct color). This combination transforms the text from a simple marginal note into an integral, visually emphasized element of the graphic, suitable for large internal titles or watermarks.
#create scatterplot
plot(df$x, df$y)
#add customized text inside top of plot
mtext("Text on the top", side=3, line=-3, cex=3, col='blue')
As the resulting plot clearly illustrates, the text is dramatically customized in size, color, and position. Mastering these customization options provides the analyst with unparalleled control over plot annotation. Experimenting with different values for line, cex, and adj allows for the creation of annotations that are perfectly tailored to the presentation context, whether they need to be subtly placed in the far margin or boldly integrated into the plotting region itself.
Summary and Recommendations for Effective Plot Annotation
The mtext() function represents a cornerstone of professional visualization within the R environment. Its specialized focus on marginal text placement fills a critical gap, enabling analysts to enrich their graphical output with crucial context and metadata without compromising the integrity or clarity of the underlying data representation. We have demonstrated how the function’s core arguments—text, side, and line—provide comprehensive control over positioning, while supplementary graphical parameters such as cex and col offer robust customization for visual appeal and emphasis.
For effective application of this tool, we recommend a strategic approach to marginal annotation. Reserve the top margin (side=3) for secondary titles or overall context, the bottom margin (side=1) for source information or date stamps, and the side margins (side=2 and side=4) for detailed methodology notes or specific data caveats. Always use the line argument judiciously; while negative values are powerful for internal placement, ensure that the text does not obscure important data points.
We strongly encourage further exploration of R’s base graphics suite. Functions such as text() (for placing text anywhere within the plot area using coordinates), title() (for main titles and subtitles), and the general graphical parameters controlled via par() can be combined with mtext() to build highly sophisticated and informative visualizations. By integrating these tools, you can move beyond standard graphics and create plots that communicate data stories with maximum efficiency and professionalism.
Additional Resources
The following tutorials explain how to use other common functions in R:
Cite this article
Mohammed looti (2026). Learning to Add Text Annotations to R Plots with mtext(). PSYCHOLOGICAL STATISTICS. Retrieved from https://statistics.arabpsychology.com/use-mtext-function-in-r-with-examples/
Mohammed looti. "Learning to Add Text Annotations to R Plots with mtext()." PSYCHOLOGICAL STATISTICS, 3 Mar. 2026, https://statistics.arabpsychology.com/use-mtext-function-in-r-with-examples/.
Mohammed looti. "Learning to Add Text Annotations to R Plots with mtext()." PSYCHOLOGICAL STATISTICS, 2026. https://statistics.arabpsychology.com/use-mtext-function-in-r-with-examples/.
Mohammed looti (2026) 'Learning to Add Text Annotations to R Plots with mtext()', PSYCHOLOGICAL STATISTICS. Available at: https://statistics.arabpsychology.com/use-mtext-function-in-r-with-examples/.
[1] Mohammed looti, "Learning to Add Text Annotations to R Plots with mtext()," PSYCHOLOGICAL STATISTICS, vol. X, no. Y, ص Z-Z, March, 2026.
Mohammed looti. Learning to Add Text Annotations to R Plots with mtext(). PSYCHOLOGICAL STATISTICS. 2026;vol(issue):pages.