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 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
.
In base graphics, the primary function for adding text is text()
.
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)
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.In ggplot2
, the primary function for adding text to a plot is geom_text()
.
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.
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).
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.
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()
.
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
.
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.
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)
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)
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)
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)
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")
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")
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)
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)
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")
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")