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.
xlab
: Label for the X-axis.ylab
: Label for the Y-axis.line
: Line number where the title and subtitle start. Default is 3.outer
: If TRUE, titles are plotted in the outer margin.col.main
,col.sub
,col.lab
: Colors for the main title, subtitle, and axis labels.cex.main
,cex.sub
,cex.lab
: Character expansion for the main title, subtitle, and axis labels.font.main
,font.sub
,font.lab
: Font style for the main title, subtitle, and axis labels.
Adding title to the plot using title function()
r
# 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 = "w3wiki 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:
- cex.main: size for main title
- cex.lab: text size for axis title
- cex.sub: text size of the sub-title
Adding title to the plot using title function()
r
barplot ( c (1, 10) ) title (main = "PLOT " , sub = "w3wiki 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
# 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 = "w3wiki 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:
- Data Setup: Defines
x
andy
as values for the x and y axes. - Plot Creation: Uses the
plot()
function to create a colorful plot with specified axes and axis color. - Title Customization: Calls
title()
to add a main title, subtitle, and customize fonts and colors for both titles and axis labels. - Data Points: Adds red data points to the plot using
points()
for enhanced visualization. - Legend Inclusion: Places a legend in the top-right corner to label the red data points with appropriate styling.
- Visualization: The final result is a visually appealing and customized plot with titles, labeled axes, data points, and a legend.