Add Titles to a Graph in R Programming – title() Function
Last Updated :
24 Nov, 2023
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
x<-1:5; y<-x*x
plot (x, y, main = "" , xlab = "" , ylab = "" ,
col.axis = "darkgreen" )
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:
- 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 = "Geeksforgeeks article" ,
xlab = "X axis" , ylab = "Y axis" ,
col.main= "darkgreen" , col.lab= "black" , col.sub= "darkgreen" ,
font.main = 4, font.lab = 4, font.sub = 3,
cex.main = 3, cex.lab = 1.7, cex.sub = 2
)
|
Output:
Adding title to the plot using title function()
R
x <- 1:5
y <- x * x
plot (x, y,
main = "" ,
xlab = "" ,
ylab = "" ,
col.axis = "darkgreen"
)
title (main = "Graph" ,
sub = "Geeksforgeeks article" ,
cex.main = 2.5,
font.main = 2,
col.main = "purple" ,
cex.sub = 1.5,
font.sub = 3,
col.sub = "orange" ,
xlab = "X axis" ,
cex.lab = 1.2,
font.lab = 4,
col.lab = "blue"
)
points (x, y, col = "red" , pch = 16, cex = 1.5)
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
- Data Setup: Defines
x
and y
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...