Open In App

Add Titles to a Graph in R Programming – title() Function

In R Programming Language the title() function is often used to add annotations, titles, and labels to specific regions of a plot. The function allows customization of the main title, subtitle, and axis labels. Here’s a brief explanation of the parameters commonly used with the title() function.

Syntax:



title(main = NULL, sub = NULL, xlab = NULL, ylab = NULL, …)

Parameters:



main: Main title of the graph

sub: Defines subtitles

Parameters used in the title function()

There are some common parameters that are used in title function.

Adding title to the plot using title function()




# R program to add title to a Graph
 
# Specifying axis values
x<-1:5; y<-x*x
 
# Creating a plot
plot(x, y, main = "", xlab = "", ylab = "",
    col.axis = "darkgreen")
 
# Calling title() function
title(main = "Graph ", sub = "Geeksforgeeks article",
    xlab = "X axis", ylab = "Y axis",
    cex.main = 4, font.main = 3, col.main = "darkgreen",
    cex.sub = 2, font.sub = 3, col.sub = "darkgreen",
    col.lab ="black"
    )

Output:

In above example main title and sub titles are added to the plot. The arguments that can be used to change the font size are as follows:

Adding title to the plot using title function()




barplot(c(1, 10) )
title(main = "PLOT ", sub = "Geeksforgeeks article",
    xlab = "X axis", ylab = "Y axis",
 
# Change the colors
col.main="darkgreen", col.lab="black", col.sub="darkgreen",
 
# Titles in italic and bold
font.main = 4, font.lab = 4, font.sub = 3,
 
# Change font size
cex.main = 3, cex.lab = 1.7, cex.sub = 2
)

Output:

Adding title to the plot using title function()




# R program to add title to a Graph
 
# Specifying axis values
x <- 1:5
y <- x * x
 
# Creating a colorful and customized plot
plot(x, y,
     main = "",           # Main title
     xlab = "",           # X-axis label
     ylab = "",           # Y-axis label
     col.axis = "darkgreen"  # Color of the axis
)
 
# Calling title() function with customization
title(main = "Graph",
      sub = "Geeksforgeeks article",
      cex.main = 2.5,      # Character expansion for the main title
      font.main = 2,       # Font style for the main title
      col.main = "purple", # Color of the main title
      cex.sub = 1.5,       # Character expansion for the subtitle
      font.sub = 3,        # Font style for the subtitle
      col.sub = "orange"# Color of the subtitle
      xlab = "X axis",     # X-axis label
      cex.lab = 1.2,       # Character expansion for the X-axis label
      font.lab = 4,        # Font style for the X-axis label
      col.lab = "blue"     # Color of the X-axis label
)
 
# Adding some points for better visualization
points(x, y, col = "red", pch = 16, cex = 1.5)
 
# Adding a legend
legend("topright", legend = "Data Points", col = "red", pch = 16,
       cex = 1.5, bg = "white", box.lwd = 0)

Output:

Add Titles to a Graph in R Programming – title() Function

  1. Data Setup: Defines x and y as values for the x and y axes.
  2. Plot Creation: Uses the plot() function to create a colorful plot with specified axes and axis color.
  3. Title Customization: Calls title() to add a main title, subtitle, and customize fonts and colors for both titles and axis labels.
  4. Data Points: Adds red data points to the plot using points() for enhanced visualization.
  5. Legend Inclusion: Places a legend in the top-right corner to label the red data points with appropriate styling.
  6. Visualization: The final result is a visually appealing and customized plot with titles, labeled axes, data points, and a legend.


Article Tags :