ggplot2 title : main, axis and legend titles
The aim of this tutorial is to describe how to modify plot titles (main title, axis labels and legend titles) using R software and ggplot2 package.
The functions below can be used :
ggtitle(label) # for the main title
xlab(label) # for the x axis label
ylab(label) # for the y axis label
labs(...) # for the main title, axis labels and legend titles
The argument label is the text to be used for the main title or for the axis labels.
Related Book:
GGPlot2 Essentials for Great Data Visualization in R
Prepare the data
ToothGrowth data is used in the following examples.
# convert dose column from a numeric to a factor variable
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
Make sure that the variable dose is converted as a factor using the above R script.
Example of plot
library(ggplot2)
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot()
p
Change the main title and axis labels
Change plot titles by using the functions ggtitle(), xlab() and ylab() :
p + ggtitle("Plot of length \n by dose") +
xlab("Dose (mg)") + ylab("Teeth length")
Note that, you can use \n to split long title into multiple lines.
Change plot titles using the function labs() as follow :
p +labs(title="Plot of length \n by dose",
x ="Dose (mg)", y = "Teeth length")
It is also possible to change legend titles using the function labs():
# Default plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose))+
geom_boxplot()
p
# Modify legend titles
p + labs(fill = "Dose (mg)")
Change the appearance of the main title and axis labels
Main title and, x and y axis labels can be customized using the functions theme() and element_text() as follow :
# main title
p + theme(plot.title = element_text(family, face, colour, size))
# x axis title
p + theme(axis.title.x = element_text(family, face, colour, size))
# y axis title
p + theme(axis.title.y = element_text(family, face, colour, size))
The arguments below can be used for the function element_text() to change the appearance of the text :
- family : font family
- face : font face. Possible values are “plain”, “italic”, “bold” and “bold.italic”
- colour : text color
- size : text size in pts
- hjust : horizontal justification (in [0, 1])
- vjust : vertical justification (in [0, 1])
- lineheight : line height. In multi-line text, the lineheight argument is used to change the spacing between lines.
- color : an alias for colour
# Default plot
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + geom_boxplot() +
ggtitle("Plot of length \n by dose") +
xlab("Dose (mg)") + ylab("Teeth length")
p
# Change the color, the size and the face of
# the main title, x and y axis labels
p + theme(
plot.title = element_text(color="red", size=14, face="bold.italic"),
axis.title.x = element_text(color="blue", size=14, face="bold"),
axis.title.y = element_text(color="#993333", size=14, face="bold")
)
Remove x and y axis labels
It’s possible to hide the main title and axis labels using the function element_blank() as follow :
# Hide the main title and axis titles
p + theme(
plot.title = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank())
Infos
This analysis has been performed using R software (ver. 3.1.2) and ggplot2 (ver. )
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