The text function is used to add text to a chart, just specify the corresponding x and y coordinates, and the text content that you want to add.
Basic usage:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n") text (x = 3, y = 3, labels = "text")
As follows:
Supports simultaneous creation of multiple text labels
code example:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n") text (x = C (3, 3), y = C (3, 5), labels = c ("text", "text"))
As follows:
Parameter adjustment:
Col: Setting the color of text
code example:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n")
Text (x = C (3, 3), y = C (3, 5), labels = c ("text", "text"), col = C ("Red", "Blue"))
As follows:
CeX: Setting the size of text
code example:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n") text (x = C (3, 3), y = C (3, 5), labels = c ("text", "text"), CeX = C (0.5, 2))
As follows:
Sort: Rotate a file
code example:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n") text (x = 3, y = 3, labels = "text", SRT = 45)
As follows:
Adj: Adjust the position of the text, a value is adjusted to the position of the x-axis, if two values, the first adjustment is the position of the x-axis, the second adjustment is the y-axis position, the optional range is [0, 1]
To adjust the x-axis, the code example:
Par (Mfrow = C (1,3)) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "adj = 0") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", adj = 0) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "adj = 0.5") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", adj = 0.5) plot (1:5, 1:5, XL im = C (0,6), Ylim = C (0,6), type = "N", main = "adj = 1") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, L Abels = "text", adj = 1)
The x-axis is adjusted as follows:
To adjust the y-axis, the code example:
Par (Mfrow = C (1,3)) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "adj = 0") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", adj = C (0.5, 0)) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = " N ", main =" adj = 0.5 ") abline (h = 3, V = 3, col =" Gray ", lty = 3) text (x = 3, y = 3, labels =" text ", adj = C (0.5, 0.5)) p Lot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "adj = 1") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", adj = C (0.5, 1))
The y-axis is adjusted as follows:
POS: Also adjust the position of the text, can not be used in conjunction with the adj parameter, the optional value is 1, 2, 3, 4, respectively, corresponding to the next, upper, left, right 4 directions
code example:
Par (Mfrow = C (1, 4)) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "pos = 1") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", pos = 1) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n", Mai n = "pos = 2") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", pos = 2) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "pos = 3") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, Labe ls = "text", pos = 3) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "adj = 4") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", pos = 4)
As follows:
Offset: It only works with the POS function and adjusts the text.
code example:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "pos = 1")
Abline (h = 3, V = 3, col = "Gray", lty = 3)
Text (x = 3, y = 3, labels = "text", pos = 1, offset = 1, col = "Red")
Text (x = 3, y = 3, labels = "text", pos = 1, offset =-1, col = "Blue")
As follows:
Font: Format text, 1 is the default value, is the normal text, 2 for bold, 3 for italic, 4 for Bold + italic, 5 for Adobe devices only useful
code example:
Par (Mfrow = C (1, 5)) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "Font = 1") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", Font = 1) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n", M Ain = "Font = 2") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", Font = 2) plot (1:5, 1:5, X Lim = C (0,6), Ylim = C (0,6), type = "N", main = "Font = 3") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, Labels = "text", Font = 3) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "Font = 4") abline (h = 3, V = 3 , col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", Font = 4) plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "N", main = "Font = 5") abline (h = 3, V = 3, col = "Gray", lty = 3) text (x = 3, y = 3, labels = "text", Font = 5)
As follows:
The common use and parameters of the text function refer to the example above, and when we want to print mathematical expressions or some special symbols on the image, we need to make special settings for the lables parameter.
For a data expression, it is better to display it when it is converted to an expression by using the expression function
Print an open-squared expression, code example:
Plot (1:5, 1:5, Xlim = C (0,6), Ylim = C (0,6), type = "n") text (x = 3, y = 3, labels = expression (sqrt (x)))
As follows:
For the printing of expressions, refer to the Help document of the Plotmath function for specific reference
R language Low-level drawing function-text