mtext()
stands for ‘marginal text’, and it is commonly used to add text to the margins of a plot in R. With mtext()
, you can add text on any side of the plot and format it according to your needs. In this article, we will explore the mtext()
function in detail, going over its parameters, how to use it, and how to use it in different situations.
The Basics of mtext()
In the simplest form, you can use the mtext()
function as follows:
mtext(text = "Your Text Here", side = 3)
In this example, “Your Text Here” will be added to the top margin (since side = 3
refers to the top margin) of the current plot.
Parameters of mtext()
The mtext()
function has several parameters that allow you to customize its usage. These parameters are:
text
: This parameter specifies the text to be written.side
: This parameter specifies on which side of the plot the text should be written. It takes an integer between 1 and 4: 1 refers to the bottom, 2 to the left, 3 to the top, and 4 to the right.line
: This specifies the line in which to place the text. By default, it is 0. The larger the number, the farther from the plot the text is placed.outer
: Ifouter = TRUE
, the text is placed in the margin of the outer plot (if any). Ifouter = FALSE
(the default), the text is placed in the plot margin.adj
: This parameter adjusts the text’s position horizontally. The values range from 0 (left justified) to 1 (right justified).padj
: This parameter adjusts the text’s position vertically. The values range from 0 (bottom) to 1 (top).cex
: This specifies the text’s relative size. The default value is 1.col
: This parameter specifies the color of the text.font
: This parameter sets the font style of the text. 1 = plain text, 2 = bold, 3 = italic, 4 = bold and italic.at
: This specifies the position (in user coordinates) where the text should be placed.
Now that you know the parameters, you can start to use the mtext()
function more flexibly.
Customizing the mtext() Function
Let’s demonstrate how to use these parameters to customize your marginal text.
# Create a basic plot
plot(1:10, 1:10, main = "")
# Add marginal text with customization
mtext(text = "This is my plot", side = 3, line = 2, outer = FALSE, adj = 0, padj = 1, cex = 1.5, col = "red", font = 2)

In this example, we have created a simple plot of 1:10
against 1:10
. We’ve added a marginal text “This is my plot” at the top of the plot (side = 3
). The text is written on the second line (line = 2
), inside the plot margin (outer = FALSE
). It is left-justified (adj = 0
) and positioned at the top of the line (padj = 1
). The text size is 1.5 times the default size (cex = 1.5
), colored red (col = "red"
), and bold (font = 2
).
Using mtext() in Different Situations
With Multiple Plots
When you have multiple plots in one figure (using functions like par(mfrow = c(n,m))
or layout()
), you might want to add a common title or labels to the whole figure rather than to individual plots. In such cases, you can use the outer = TRUE
parameter.
Here’s an example:
# Create a layout for 2 plots
par(mfrow = c(2, 1))
# Create the first plot
plot(1:10, 1:10, main = "Plot 1")
# Create the second plot
plot(10:1, 10:1, main = "Plot 2")
# Add common title
mtext("Common Title", side = 3, line = 2, outer = TRUE, font = 2, cex = 1.5)

In this example, the mtext()
function adds a common title to the two plots.
For Math Expressions
The mtext()
function supports math expressions. This can be useful when you want to add mathematical symbols or equations to your plots.
# Create a basic plot
plot(1:10, 1:10, main = "")
# Add math expression in marginal text
mtext(expression(paste("y = ", beta[0], " + ", beta[1], "x")), side = 3, line = 2, cex = 1.2)

In this example, the text is a math expression representing a simple linear equation.
For Dynamic Texts
In R, you can use the paste()
or paste0()
function to concatenate strings. By using these functions with mtext()
, you can create dynamic marginal texts based on variables.
# Create a basic plot
plot(1:10, 1:10, main = "")
# Variables
a <- 1
b <- 10
# Add dynamic text
mtext(paste("This plot shows a range from", a, "to", b), side = 3, line = 2, cex = 1.2)

In this example, the marginal text is created dynamically based on the values of variables a
and b
.
Conclusion
In this article, we covered how to use the mtext()
function in R, from its parameters and basic usage to its application in different situations. By learning to effectively use mtext()
, you can add rich, informative text to your plots, making your data visualizations more understandable and engaging.