In this article, we will show you how to use the text() function in R. The text() function helps you add text to a plot. Let’s follow this article to learn more about it with the explanation and examples below.
The text() Function In R
What does text() do in R?
The text() function in R can help you draw text to a plot. You can add multiple texts and you can also edit font, font-size, font-weight, color, … of a text.
With this function, you can decorate the graphs more diversely and creatively. Let’s take a look at the syntax of this function below.
Syntax:
text(latitude, longitude, labels, adj, pos , offset, vfont, cex, col, font, …)
Parameters:
- latitude, longitude: The position (latitude, longitude) of the text will be written in the graphs.
- labels: The character vector of the expression.
- adj: The default is NULL. One or two values: 0,1.
- pos: The default is NULL. The position specified for the label.
- offset: The default is 0.5. To control the distance of the label.
- vfont: The default is NULL. The font family of the label.
- cex: The default is 1. The numeric character expansion factor.
- col: The default is NULL. The color of the label.
- font: The default is NULL. The font of the labels.
After learning the usage and the syntax of the text() function, you will learn how to use it in the next title below.
How to use the text() function?
You can use the text() function to add a label or multiple labels with the specified font, color, … to your graph.
Add text to a plot
You can add text to your plot by the text() function.
Look at the example below.
# Create the plot plot(1:7, xlab='Latitude', ylab ='Longitude') # Add the content 'LearnShareIT' to a position x = 2, y = 5 text(x=2,y=5, 'LearnShareIT')
Output
Add multiple text elements to a plot
You can add multiple text elements to a plot by the text() function.
Look at the example below.
# Create the plot plot(1:7, xlab='Latitude', ylab ='Longitude') # Add the content 'LearnShareIT' to a position x = 2, y = 5 text(x=2,y=5, 'LearnShareIT') # Add the content 'Author: crvt4722' to a position x = 2, y = 6 text(x=2,y=6, 'Author: crvt4722')
Output
Add the text elements to a plot with the specified color and font
You can add text elements to a plot with the specified color and font by the text() function.
Look at the example below.
# Create the plot plot(1:7, xlab='Latitude', ylab ='Longitude') # Add the content 'LearnShareIT' to a position x = 2, y = 5 text(x=2,y=5, 'LearnShareIT', col = "red", cex = 3) # Add the content 'Author: crvt4722' to a position x = 2, y = 6 text(x=2,y=6, 'Author: crvt4722', col = "yellow", cex = 2)
Output
Summary
You have learned the usage, the syntax, and how to use the text() function in R. The text() function in R can help you add a text label, multiple text labels with the specified color, font, … to the plot. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:
My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R