Add titles to a plot in R software
The aim of this article is to show how to modify the title of graphs (main title and axis titles) in R software. There are two possible ways to do that :
- Directly by specifying the titles to the plotting function (ex :
plot()
). In this case titles are modified during the creation of plot. - the title() function can also be used. It adds titles on an existing plot.
Change main title and axis labels
The following arguments can be used :
- main: the text for the main title
- xlab: the text for the x axis label
- ylab: the text for y axis title
- sub: sub-title; It’s placed at the bottom of x-axis
# Simple graph
barplot(c(2,5))
# Add titles
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title")
title colors
The following parameters can be used to change the colors :
- col.main: color the main title
- col.lab: color of the axis titles (x and y axis)
- col.sub: color of the sub-title
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title",
col.main="red", col.lab="blue", col.sub="black")
Note that, the different colors available in R software are described here.
The font style for the text of the titles
The graphical parameters to use for customizing the font of the titles are :
- font.main: font style for the main title
- font.lab: font style for the axis titles
- font.sub: font style for the sub-title
The value of these arguments should be an integer.
The possible values for the font style are :
- 1: normal text
- 2: bold
- 3: italic
- 4: bold and italic
- 5 : Symbol font
Use the R code below to create a plot title with bold and italic font style.
# Titles in bold and italic
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title",
font.main=4, font.lab=4, font.sub=4)
Change the font size
font size can be modified using the graphical parameter : cex. The default value is 1. If cex value is inferior to 1, then the text size is decreased. Conversely, any value of cex greater than 1 can increase the font size.
The following arguments can be used to change the font size :
- cex.main : text size for main title
- cex.lab : text size for axis title
- cex.sub : text size of the sub-title
An example is shown below :
# Increase the size
barplot(c(2,5), main="Main title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub-title",
cex.main=2, cex.lab=1.7, cex.sub=1.2)
Use the title() function
title() can be also used to add titles to a graph.
A simplified format is :
title(main = NULL, sub = NULL,
xlab = NULL, ylab = NULL, ...)
Example of usage
x<-1:10; y<-x*x
plot(x,y, main = "", xlab="", ylab="",
col.axis="blue")
title(main = "Main title", sub = "Sub-title",
xlab = "X axis", ylab = "Y axis",
cex.main = 2, font.main= 4, col.main= "red",
cex.sub = 0.75, font.sub = 3, col.sub = "green",
col.lab ="darkblue"
)
Customize the titles using par() function
Note that, the R par() function can be used to change the color, font style and size for the graph titles. The modifications done by the par() function are called ‘permanent modification’ because they are applied to all the plots generated under the current R session.
Read more on par() by clicking here.
par(
# Change the colors
col.main="red", col.lab="blue", col.sub="black",
# Titles in italic and bold
font.main=4, font.lab=4, font.sub=4,
# Change font size
cex.main=2, cex.lab=1.7, cex.sub=1.2
)
barplot(c(2,5), main="TMain title",
xlab="X axis title",
ylab="Y axis title",
sub="Sub title")
Infos
This analysis has been performed using R statistical software (ver. 3.1.0).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains best data science and self-development resources to help you on your path.
Coursera - Online Courses and Specialization
Data science
- Course: Machine Learning: Master the Fundamentals by Standford
- Specialization: Data Science by Johns Hopkins University
- Specialization: Python for Everybody by University of Michigan
- Courses: Build Skills for a Top Job in any Industry by Coursera
- Specialization: Master Machine Learning Fundamentals by University of Washington
- Specialization: Statistics with R by Duke University
- Specialization: Software Development in R by Johns Hopkins University
- Specialization: Genomic Data Science by Johns Hopkins University
Popular Courses Launched in 2020
- Google IT Automation with Python by Google
- AI for Medicine by deeplearning.ai
- Epidemiology in Public Health Practice by Johns Hopkins University
- AWS Fundamentals by Amazon Web Services
Trending Courses
- The Science of Well-Being by Yale University
- Google IT Support Professional by Google
- Python for Everybody by University of Michigan
- IBM Data Science Professional Certificate by IBM
- Business Foundations by University of Pennsylvania
- Introduction to Psychology by Yale University
- Excel Skills for Business by Macquarie University
- Psychological First Aid by Johns Hopkins University
- Graphic Design by Cal Arts
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet