The data used in the examples below is the following:
text
function
Adding text
You can add texts to base R plots with the text
function. You just need to pass the coordinates and the text you want to add.
Text customization
The added text can be customized with the graphical parameters, such as col
, for the color, font
for the font style or cex
for the text size.
Text rotation
You can also rotate the text setting a degree or rotation with the srt
argument.
Text splitting
Finally, it is worth to mention that you can split the text in several lines with "\n"
.
If you have a grouping variable you can pass it to the labels
argument of the text
function to label the observations. You can set the position specifying the variables and fine-tuning them.
mtext
function
The mtext
function can be used to add text to all sides of a plot, which can be specified using the side
argument.
side | Position |
---|---|
1 | bottom |
2 | left |
3 | top |
4 | right |
Bottom text
Left text
Top text
Right text
mtext
adjustment
There are several arguments that you can customize. The most relevant are:
line
, to set the margin line where to set the text. Default value is 0.adj
, to adjust the text in the reading direction from 0 to 1 (default value is 0.5).at
, to indicate where to draw the text based on the corresponding axis.col
, cex
, family
and font
.Top-right text
Top-center text, line 3
Top-right text, lines 0, 1, 2, 3
Left text at 50, line 2
mtext
does not support rotation, only horizontal adjustment with las = 1
for the vertical axis and vertical adjustment with las = 3
for the X-axis. If you need to rotate the text you can use text
function with srt
argument instead.
See also