In this article, you’ll learn how to add text to a plot. We will also be creating a plot and will try various aesthetic manipulations in the same plot, like changing the text color.
Add texts within the plot
text() function can be used to draw text inside the plotting area. A simplified format of the function is :
text(x, y, labels, adj = NULL, pos = NULL, offset = 0.5, vfont = NULL, cex = 1, col = NULL, font = NULL ...)
|numeric vectors specifying the x coordinates of the text to a plot|
|numeric vectors specifying the y coordinates of the text to a plot|
|a character vector indicating the text to be written|
|one or two values in [0, 1] which specify the x (and optionally y) adjustment (‘justification’) of the labels, with 0 for left/bottom, 1 for right/top, and 0.5 for centered. On most devices values outside [0, 1] will also work.|
|a position specifier for the text. If specified this overrides any |
|numeric character expansion factor; multiplied by |
Firstly, we need a plot so that we can add text to that plot. For this example, we are gonna
mtcars dataset plot.
# Creating a plot > plot(mtcars$wt, mtcars$mpg, main = " Car Weight vs. Mileage ", xlab = "Miles", ylab = " Weight", pch = 17, col = "red")
Let’s add text to the plot, adding car name to each marker in the plot.
# Adding text to the plot > text(mtcars$wt, mtcars$mpg, row.names(mtcars), cex = 0.88, pos = 2, col = "grey")
Add mathematical annotation to a plot
We can also add a mathematical annotation in the
text() function, Simply by writing the expression in the
expression(…) is.expression(x) as.expression(x, …)
|an arbitrary R object.|
# Creating a expression > equation <- expression(y == x^2 + 2*x + 8) # Creating a plot > plot(x=10, y=10, main=equation)
In above example the the
text() function is used to add mathematical annotation to a plot.
Add text to the margins of the plot
To write text into the margins of a Plot we need to use to
mtext() function. Text is written in one of the four margins of the current figure region or one of the outer margins of the device region.
> mtext(text, side = 3, line = 0, outer = FALSE, at = NULL, adj = NA, ...)
|one or more character strings or expressions.|
|on which side of the plot (1=bottom, 2=left, 3=top, 4=right).|
|on which MARgin line, starting at 0 countings outwards.|
|use outer margins if available.|
|give location in user-coordinates. If |
|adjustment for each string. For strings parallel to the axes, |
|color to use.|
|font for text.|
In the same way, let’s create a plot and give it some margin text.
# Creating a plot > plot(x=0:10, y=20:30, main="mtext example", col.main="blue") > mtext("this is added using mtext()", side=3, adj=0, col="red")
Here, in the above example, the side specifies the side of the plot such as the bottom, left, top, right. And in the given the
side=3 i.e the top part of the plot and
adj=1 means right or top alignment.
Congratulations, you learn how to add text in a plot, with how to adjust those text along with margin text with
mtext() function and how to write expressions using
This brings the end of this Blog. We really appreciate your time.
Hope you liked it.
Do visit our page www.zigya.com/blog for more informative blogs on Data Science
Keep Reading! Cheers!