The text() Function with R

The text() Function

Basic Usage

The text() function adds text to an existing plot at specified coordinates.

Basic Syntax: 

text(x, y, labels, pos, offset, cex, col, font)
  • x: The x-coordinate where the text will be placed.
  • y: The y-coordinate where the text will be placed.
  • labels: The text to display.
  • pos: The position of the text relative to the coordinates (1 for below, 2 for left, 3 for above, 4 for right).
  • offset: Offset of the text from the specified coordinates.
  • cex: Expansion factor to adjust the text size.
  • col: Color of the text.
  • font: Font style (1 for normal, 2 for bold, 3 for italic, 4 for underline).

Example: 

# Create a basic plot
x <- 1:10
y <- x^2
plot(x, y, main = "Text Annotation Example", xlab = "X", ylab = "Y")
# Add text
text(5, 25, "Important Point", cex = 1.2, col = "red", pos = 4)
  • text(5, 25, “Important Point”, …): Places the text “Important Point” at coordinates (5, 25).
  • cex = 1.2: Increases the text size.
  • col = “red”: Sets the text color to red.
  • pos = 4: Places the text to the right of the coordinates.

Adding Text to Specific Points

You can use text() to label specific points on the plot.

Example: 

# Create a basic plot
plot(x, y, main = "Text on Specific Points", xlab = "X", ylab = "Y")
# Add text for each point
text(x, y, labels = paste("(", x, ",", y, ")", sep = ""), pos = 1, cex = 0.8)
  • labels = paste(“(“, x, “,”, y, “)”, sep = “”): Creates labels with the coordinates of the points.
  • pos = 1: Places the text below the coordinates of the points.

Adjusting Text Size and Position

Use cex and pos to adjust the text size and position.

Example: 

# Create a basic plot
plot(x, y, main = "Adjust Text Size and Position", xlab = "X", ylab = "Y")
# Add text with different sizes and positions
text(5, 25, "Large Text", cex = 2, col = "blue", pos = 3) # Larger text above
text(7, 50, "Small Text", cex = 0.6, col = "green", pos = 1) # Smaller text below
  • cex = 2: Increases the text size.
  • cex = 0.6: Decreases the text size.

Using Text for Annotations

Add text annotations to highlight specific parts of the plot, such as points of interest or key observations.

Example: 

# Create a basic plot
plot(x, y, main = "Annotations with Text", xlab = "X", ylab = "Y")
# Add an annotation
text(8, 64, "Maximal Here", cex = 1.5, col = "darkred", pos = 3, font = 2)
  • font = 2: Makes the text bold.
  • pos = 3: Places the text above the specified point.

Adding Text in Relative Coordinates

You can place text using relative coordinates for proportional positioning.

Example: 

# Create a basic plot
plot(x, y, main = "Text in Relative Coordinates", xlab = "X", ylab = "Y")
# Add text with relative coordinates
text(x = max(x) * 0.5, y = max(y) * 0.5, "Central Text", cex = 1.2, col = "purple")
  • x = max(x) * 0.5: Places the text at the midpoint of the x-axis.
  • y = max(y) * 0.5: Places the text at the midpoint of the y-axis.

Summary

  • Basic Usage: Use text() to add text at specified coordinates on a plot.
  • Text on Points: Label specific points with coordinates.
  • Adjust Size and Position: Modify text size (cex) and position (pos).
  • Annotations: Use text() to annotate parts of the plot or highlight key points.
  • Relative Coordinates: Place text using proportional coordinates for dynamic positioning.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *

Facebook
Twitter
LinkedIn
WhatsApp
Email
Print