R Tutorial

Fundamentals of R

Variables

Input and Output

Decision Making

Control Flow

Functions

Strings

Vectors

Lists

Arrays

Matrices

Factors

DataFrames

Object Oriented Programming

Error Handling

File Handling

Packages in R

Data Interfaces

Data Visualization

Statistics

Machine Learning with R

Adding Text to Plots in R

Adding textual annotations to plots can help clarify data points, indicate specific regions, or add context. In this tutorial, we'll explore how to add text to plots in R using both base graphics and ggplot2.

1. Base Graphics:

In base graphics, the primary function for adding text is text().

1.1 Basic Usage of text():

You can add a text label to a specific point in a plot using the text() function:

plot(1:5, 1:5)  # Basic scatter plot
text(3, 3, "Center")  # Adds "Center" at (3,3)

1.2 Adding Multiple Labels:

You can use vectors to add multiple labels:

x <- 1:5
y <- 1:5
labels <- c("A", "B", "C", "D", "E")
plot(x, y)
text(x, y, labels, pos=4, offset=0.5, col="red")
  • pos: Specifies the position of the text relative to the point. It can take values 1 (below), 2 (left), 3 (above), and 4 (right).
  • offset: Specifies the distance between the point and the text.
  • col: Specifies the text color.

2. ggplot2:

In ggplot2, the primary function for adding text to a plot is geom_text().

2.1 Basic Text Annotation:

library(ggplot2)

data <- data.frame(x=1:5, y=1:5)
ggplot(data, aes(x, y)) +
  geom_point() +
  geom_text(aes(label=sprintf("Point %d", 1:5)), vjust=-1)

Here, vjust is used for vertical adjustment. A value of -1 places the text below the points.

2.2 Text with Expressions (e.g., Math Notation):

ggplot(data, aes(x, y)) +
  geom_point() +
  annotate("text", x=3, y=3, label=expression(paste(alpha, " = 0.05")), parse=TRUE)

The annotate() function is used here to add a single text annotation. The parse=TRUE argument indicates that the label should be parsed (useful for mathematical notation).

3. Additional Tips:

  1. Positioning Text: For precise text positioning, especially to avoid overlapping, you can use the ggrepel package in conjunction with ggplot2. It provides the geom_text_repel() function which helps in auto-adjusting overlapping text labels.

  2. Labeling with Variables: In ggplot2, you can easily label points using a column from your dataframe. Just set aes(label=your_column_name) in geom_text().

  3. Custom Fonts and Sizes: Both text() in base graphics and geom_text() in ggplot2 allow customization of font, size, face, and angle using arguments like cex, fontface, and angle.

Conclusion:

Annotating plots with text can enhance clarity and provide important context to the viewer. Whether you're working with base graphics or ggplot2, R offers robust capabilities for textual annotations in visualizations.

  1. R plot add text example: Adding text annotations to a plot in R using text():

    # R plot add text example
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    text(5, 15, "Annotation", col = "red", cex = 1.2)
    
  2. How to annotate plots in R: Annotating plots in R using text() for custom annotations:

    # How to annotate plots in R
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    text(3, 10, "Custom Annotation", col = "blue", font = 2)
    
  3. Adding labels to data points in R plot: Adding labels to specific data points in an R plot using text():

    # Adding labels to data points in R plot
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    text(5, 15, labels = "Data Point", col = "green", cex = 1.2)
    
  4. Customizing text appearance in R plots: Customizing text appearance in R plots with text():

    # Customizing text appearance in R plots
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    text(5, 15, "Custom Text", col = "purple", font = 3, cex = 1.5)
    
  5. Positioning text in R plot: Controlling the position of text in an R plot using text():

    # Positioning text in R plot
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    text(x = x, y = y, labels = x, pos = 3, col = "orange")
    
  6. Adding titles and subtitles to R plots: Adding titles and subtitles to an R plot using title():

    # Adding titles and subtitles to R plots
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    title(main = "Main Title", sub = "Subtitle", col.main = "brown", col.sub = "blue")
    
  7. Annotations with ggplot2 in R: Using ggplot2 for annotations in R plots:

    # Annotations with ggplot2 in R
    library(ggplot2)
    df <- data.frame(x = 1:10, y = 2 * (1:10) + 3)
    ggplot(df, aes(x, y)) + geom_point() + annotate("text", x = 5, y = 15, label = "Annotation", color = "red", size = 5)
    
  8. Rotate text in R plot: Rotating text in an R plot using text():

    # Rotate text in R plot
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y)
    text(5, 15, "Rotated Text", col = "purple", srt = 45)
    
  9. Adding legend text in R plot: Adding legend text to an R plot using legend():

    # Adding legend text in R plot
    x <- 1:10
    y <- 2 * x + 3
    plot(x, y, legend.text = "Legend Text")
    
  10. Interactive text in R plots: Adding interactive text to plots using packages like plotly:

    # Interactive text in R plots
    library(plotly)
    x <- 1:10
    y <- 2 * x + 3
    plot_ly(x = x, y = y, type = "scatter", mode = "markers+text", text = "Interactive Text")